2.五大约束

1.主键约束

(1).建表时添加带自增的主键约束

create table 表名(列名 类型 primary key auto_increment,列名 类型);

(2).修表时添加带自增的主键约束

alter table 表名 add primary key(列名);
alter table 表名 modify 主键列名 类型 auto_increment;

(3).删除主键约束
主键约束如果有自增长则要先删除自增长。

alter table 表名 modify 主键列名 类型;
alter table 表名 drop primary key;

2.外键约束

(1).建表时添加外键约束

create table 表名(列名 类型,列名 类型,constraint 外键名 foreign key(列名) 
references 表名(列名));

(2).修表时添加外键约束

alter table 表名 add constraint 约束名 foreign key(列名) references 表名(列名);

(3).删除外键约束

alter table 表名 drop  foreign key(列名);

3.唯一约束

(1).建表时添加唯一约束

create table 表名(列名 类型,列名 类型 unique );

(2).修表时添加唯一约束

alter table 表名 add constraint 约束名 unique(name);

(3).删除唯一约束

alter table 表名 drop key 约束名;

4.非空约束

(1).建表时添加非空约束

create table 表名(列名 类型,列名 类型 not null);

(2).修表时添加非空约束

alter table 表名 modify 列名 类型名 not null;

(3).删除非空约束

alter table 表名 modify 列名 类型名 null;

5.检查约束

MySQL不具备哦!!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值