sql约束(unique,primary key,check,default)

本文深入讲解数据库中的四种关键约束:唯一约束(unique)、主键约束(Primarykey)、检查约束(check)及默认约束(Default)。详细阐述了这些约束在创建表时及之后如何应用,以及如何撤销这些约束。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

unique

unique:约束唯一标识数据库表中的每条记录

创建表时创建约束

验证一个唯一字段 unique  字段

验证多个唯一字段:Constraint  uc_personID unique(字段,字段)

创建表后创建约束:

Alter table 表名 add unique (字段)

取消 alter table 表名 drop unique

Alter table 表名 add constraint uc_personID unique(字段,字段)

取消 alter table 表名 drop constraint uc_personID

 

Primary key:

Primary key约束主键唯一并不等于空

创建表时primary key(字段)

创建多个时constraint pk_personID primary key(字段,字段)

创建取消同上

 

check约束:用于限定列中的值的范围

创建表时:

约束一列:check  字段>0-------->该字段只能大于0

约束几列:constraint   chk_Person  check (字段>0  and 字段=‘值’)

创建表后创建约束同上

 

Default约束:用于向列中插入默认值

创建时:

约束一列: 字段 字段类型  default 默认值

创建后:alter table 表 add constraint ab_c defauly 值 for 字段

撤销约束:alter table 表 alter column 字段 drop default

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值