SQL Server 约束 修改表

在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是没有输入值,它有一个默认值!
)


表的修改:






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值