在sql server中,有:
1、not null 非空
当插入数据时候,必须为该列提供数值
例如我们创建一张表:
create table test1
(
testId int primary key identity(1,1),
testname varchar(30) not null , --不为空
testpass varchar(30) not null,
testage int
)
2、unique 唯一
该列值不能重复的,但是可以为null,也最多只能有一个null
3、primary key 主键
用唯一的表行数据,当定义主键约束后,该列不但不能重复,而且不能为null
复合主键:
primary key ( testId,testname)
只有当testId和testname都相同时候才算不唯一。
NOTE:一张表最多只能有一个主键,但是可以有多个not null或者unique约束
外键:从表可以多条记录同时指向主表单条记录;但主表多条记录不能同时被从表单条记录指向!
create table other
(
otherId int nvarchar(500) foreign key references main ( mainId) --这个就是让other成为main表的外键,并指向mainId
)
check:
例如我定义一个
create table test1
(
testId int primary key identity(1,1),
testname varchar(30) not null , --不为空
testpass varchar(30) not null,
testsal int check (testsal >= 5000 and testsal <= 5000 ) --规定了testsal的区间 NOTE:这是列定义
)
4、default使用
create table mes
(
mesId int primary key identity(1,1), --列值自增长
mescon varchar(4000) not null,
mesdate datetime default getdate() --default是没有输入值,它有一个默认值!
)
表的修改:
SQL Server 约束 修改表
最新推荐文章于 2024-03-04 14:18:03 发布
