约束及其语法

约束:

非空约束:not null 保证列中所有数据不能有null

唯一约束: unique 保证列中所有数据各不相同

主键约束: primary key 主键是一行数据的唯一标识,要求非空且唯一

检查约束:check 保证列中的值满足某一条件

默认约束: default 保存数据时,未指定值则采用默认值

外键约束: foreign key 外键用来让两个表的数据之间建立连接,保证数据一致性和完整性

自动增长: auto_increment

添加约束: 创建表时添加非空约束

creat table 表名(

列名 数据类型 not null

...

);

建完表后添加非空约束

alter table 表名 modify 字段名 数据结构 not null

删除约束

alter table 表名 modify 字段名 数据类型

Constraint 外键名=>fk_表名1_表名2 foreign key (列名1refernces 主表(列名)

-- 删除外键

alter table emp add constaint 外键名 foreignkey 外键名

-- 添加外键(创完表后)

alter table emp add constaint 外键名 foreign key (列名) refernces 主表(列名)

posted @ 2022-10-12 23:23  代不动码  阅读(48)  评论(0)    收藏  举报