非空约束:
--建表时设置
if exists(select name from sysobjects where name='tablename' and xtype='u')
drop table tablename
go
create table tablename
(
rowname1 int not null,
rowname2 int,
)
go
--此类约束不适合在建表后追加
alter table tablename
alter rowname2 int not null
go
--另外如果该列已经存在了数据,并且没有主键约束与唯一性约束,最好在追加not null约束时增加默认约束
主键约束:
自增属性:
添加自增属性的列,其数据类型必须是 int、bigint、smallint、tinyint 或 decimal,或者是小数位数为 0 的 numeric 数据类型,并且约束为不可为空
if exists(select name from sysobjects where name='tablename' and xtype='u')
drop table tablename
go
create table tablename
(
rowname1 int identity not null,
)
go
identity(seed,growth)
即初始值为seed,之后每次加growth作为该列的值,默认不指定的话为(1,1)
主键列的几种形式:
首先主键列不能为空,不能有重复值——唯一、非空
1、 字符型主键列数据,其内容需要用户输入,无论是否包括中文;
a) 如果不包括中文,则使用单字节字符编码
b) 如果数据中有中文存在,则一定要使用双字节编码
c) 如果是变长的数据,也以按照以上两点执行。
--建表时设置
if exists(select name from sysobjects where name='tablename' and xtype='u')
drop table tablename
go
create table tablename
(
rowname1 char(2) not null/*constraint pk_name*/ primary key,--不加非空约束也可执行,系统会为主键字段添加not null约束
)
go
--建表后追加
if exists(select name from sysobjects where name='tablename' and xtype='u')
drop table tablename
go
create table tablename
(
rowname1 char(2) not null,--必须设置非空约束,否则追加主键约束时报错
)
go
alter table tablename
add
--constraint pk_name
primary key(rowname1)
go
2、 字符型主键列数据,其内容需要用户输入,无论是否包括中文;
a) 如果不包括中文,则使用单字节字符编码
b) 如果数据中有中文存在,则一定要使用双字节编码
c) 如果是变长的数据,也以按照以上两点执行。
--建表时设置
if exists(select name from sysobjects where name='tablename' and xtype='u')
drop table tablename
go
create table tablename
(
rowname1 int identity /*constraint pk_name*/ primary key,
)
go
--建表后追加
if exists(select name from sysobjects where name='tablename' and xtype='u')
drop table tablename
go
create table tablename
(
rowname1 int identity,
)
go
alter table tablename
add
--constraint pk_name
primary key(rowname1)
go
3、用全局唯一性标识符类型作主键:
--建表时设置
if exists(select name from sysobjects where name='tablename' and xtype='u')
drop table tablename
go
create table tablename
(
rowname1 uniqueidentifier /*constraint pk_name*/ primary key,
)
go
--建表后追加
if exists(select name from sysobjects where name='tablename' and xtype='u')
drop table tablename
go
create table tablename
(
rowname1 uniqueidentifier not null
)
go
alter table tablename
add
--constraint pk_name
primary key(rowname1)
go
数据类型为该类型的字段插入值一般使用newid()函数,不建议自己生成
从另一角度对主键进行分类:
1、 自然主键,该列与其他列没有依赖关系;
2、 业务主键,该列数据与其他列数据之间有着依赖的关系;
默认约束:
--建表时设置
if exists(select name from sysobjects where name='tablename' and xtype='u')
drop table tablename
go
create table tablename
(
rowname1 int /*constraint pk_name*/ default 1,
)
go
--建表后追加
if exists(select name from sysobjects where name='tablename' and xtype='u')
drop table tablename
go
create table tablename
(
rowname1 int
)
go
alter table tablename
add
--constraint pk_name
default 1
for rowname1
go
唯一性约束:
--建表时设置
if exists(select name from sysobjects where name='tablename' and xtype='u')
drop table tablename
go
create table tablename
(
rowname1 int /*constraint pk_name*/ unique,
)
go
--建表后追加
if exists(select name from sysobjects where name='tablename' and xtype='u')
drop table tablename
go
create table tablename
(
rowname1 int
)
go
alter table tablename
add
--constraint pk_name
unique(rowname1)
go
检查约束:
--建表时设置
if exists(select name from sysobjects where name='tablename' and xtype='u')
drop table tablename
go
create table tablename
(
rowname1 int /*constraint pk_name*/ check(rowname1>0),--第一种形式 做比较来判断
rowname2 int check(rowname2 between 1 and 100),--第二种形式 限定区间
rowname3 nchar(1) check(rowname3 in ('男','女')),--限定范围
rowname4 nchar(1) check(rowname4 like '[0-9]'),--第三种形式 使用正则验证
)
go
删除约束:
删除所有的约束方法都一样
alter table student drop constraint pk_name
go
alter table student drop constraint pk_name1,pk_name2,..
go