mysql的约束条件是什么_MySQL_约束条件

八个约束条件

1.非空约束NOT NULL非空约束___创建表时添加约束create table 表名

(

a int not null,

b int not null,

);非空约束___修改表时添加约束alter table 表名

modify 字段名 类型 not null;非空约束___删除约束alter table 表名

modify 字段名 类型;

2.主键约束PRIMARY KEY主键约束___创建表时添加约束.create table 表名

(

a int,

b int,

c int,

primary key(字段1,字段2)

);create table 表名

(

a int,

b int,

c int,

constraint 约束名 primary key(字段1,...)

);create table 表名

(

a int primary key,

b int,

c int

);主键约束___修改表时添加约束alter tbale 表名

modify 字段名 类型 primary key;alter table 表名

primary key(字段1,字段2);alter table 表名

add constraint 约束名 primary key(字段1,字段2);主键约束___删除约束alter table 表名

drop primary key;

3.多字段联合主键(复合主键)多字段联合约束___创建多字段联合主键create table 表名

(

a int,

b int,

primary key(字段1,字段2)

);

4.唯一约束UNIQUE唯一约束___创建表时添加约束create table 表名

(

a int unqiue,

b int,

c int

);create table 表名

(

a int,

b int,

c int,

constraint usfz unique(字段名,...)

);create table 表名

(

a int,

b int,

c int,

unique(字段名,...)

);唯一约束___修改表时添加约束alter tbale 表名

modify 字段名 类型 unique;alter table 表名

add unique(字段1,字段2);alter table 表名

add constraint 约束名 unique(字段1,字段2);唯一约束___删除约束alter table 表名

drop index 约束名;alter table 表名

drop key 约束名称;

5.默认约束DEFAULT默认约束___创建表时添加约束create table 表名

(

a int primary key,

b int default '123456'

);默认约束___修改表时添加约束alter table 表名

modify 字段名 类型 default '1234';alter table 表名

alter column 字段名 set default '1234';默认约束___删除约束alter table 表名

modify 字段名 类型;alter table 表名

alter column 字段 drop default;

6.外键约束FOREIGN KEY外键约束___创建表时添加约束create table 从表名

(

a int,

b int,

constraint 外键约束名 foreign key (从表字段名)

references 主表名 (主表主键)

);外键约束___修改表时添加约束alter table 从表名

add foreign key(从表字段名)

references 主表名(主表字段);外键约束___删除约束alter table 从表名

drop foreign key 从表字段名;从父表删除或更新且自动删除或更新子表中匹配的行create able 从表名

(

a int,

b int,

consteraint fk_cid

foreign key(从表字段名)

references 主表表名(主表字段名) on delete cascade

);

7.自增约束 auto_increment

https://www.cnblogs.com/iforever/p/10071733.html自增列___创建表时添加约束create tbale 表名

(

a int primary key auto_increment,

b int

);create table 表名

(

a int primary key auto_increment,

b int

)auto_increment=9;自增列___修改表时添加约束alter table 表名

modify 字段名 字段类型 auto_increment;alter table 表名

auto_increment=9;自增列___删除约束alter table 表名

modify 字段名 字段类型;检查约束___创建表时添加约束create table 表名

(

a int,

b int check(字段名>0 and 字段名<100)

);create table 表名

(

a int,

check(字段名>100)

);检查约束___修改表时添加约束alter table 表名

add constraint 约束名 check(字段名>100);检查约束___删除约束alter table 表名

drop constraint 检查约束名;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值