mysql中的约束的归纳总结
1.主键约束
– 方式1: 直接在主键字段后标识
create table t(
id int primary key ,
name varchar(10)
);
– 方式2: 后面专门定义主键约束,如果是复合主键(多列)只能使用这种方式
create table t(
id int,
name varchar(10) ,
primary key(id)
);
– 方式3:在已存在的表添加主键约束
alter table t modify id int primary key;
alter table t add constraint id_pri primary key (id)
– 删除主键约束
alter table t drop primary key;
特点:属性的值不重复,不允许为空;
非空约束
– 创建表的时候给指定列添加非空约束
create table t(
stu_name varchar(10) not null
);
– 在表已经存在的基础上添加非空约束
alter table t modify stu_name varchar(10) not null;
– 删除非空约束
alter table t modify stu_name varchar(10);
特点:该属性不为空
唯一约束
– 创建表的时候给指定列加上唯一约束
create table t(
stu_name varchar(10) unique
);
– 在表存在的前提下添加唯一约束
alter table t modify stu_name varchar(10) unique;
alter table t add constraint name_unique unique(stu_name);
– 删除列上的唯一约束
alter table t drop index stu_name
– 通过约束名删除列上的唯一约束
alter table t drop index name_unique;
特点:该属性唯一,不可重复,但是null值可以重复
默认约束
– 创建表的时候给指定列加上默认约束
create table t(
stu_age int default 18
);
– 在表存在的前提下添加默认约束
alter table t modify stu_age int DEFAULT 18;
– 删除默认约束
alter table t modify stu_age int
特点:给属性加上默认值,如果不赋值则为默认值
自动增长约束
– 创建表的时候直接定义自增长
create table student(
s_id int primary key auto_increment,
s_name varchar(10)
) auto_increment=10000 – 设置自动增长从10000开始
– 设置自动增长从10000开始
alter table student auto_increment=10000;
– 给已经存在的表字段添加自增长
alter table student s_id int auto_increment;
– 删除自增长
alter table student modify s_id int ;
外键约束
– 先创建学生表,父表
create table student(
s_id int primary key ,
s_name varchar(10)
);
– 再创建子表
create table score (
s_id int ,
s_name VARCHAR(10),
s_socre int ,
user_id int ,
– 指定字段user_id是外键,指向父表student表中的s_id字段,外键必须指向父表的主键
– constraint s_foreign 这里可以加上,给这个外键取个名字
FOREIGN KEY(user_id) references student(s_id)
);
– 创建表后再添加外键约束,score_foreign为给这个外键约束取的名字
alter table score add constraint score_foreign foreign key (user_id) references student(s_id)
– 删除外键约束,score_foreign为外键约束取的名字,如果没有手动取名则改为使用默认生成的约束即可。
alter table score drop foreign key score_foreign;
总结:
1. 约束的关键字:
.主键约束:primary key
非空约束 : not null
唯一约束 : unique
默认约束:default
外键约束 : foreign key
2.用法的总结:
- 主键,非空,唯一 默认约束创建时使用:
属性名 类型名 约束关键字;
例如:
id int primary key
id int not null
id int unique
id int default 0
2.在已创建的表中添加主键,非空,唯一 默认约束:
alter table 表名 modify 属性名 数据类型 约束关键字;
alter table t modify id int primary key;
alter table t modify id int not null;
alter table t modify id int unique;
alter table t modify id int default 0 ;
以上四种约束的用法相同。
外键的约束的用法
创建表时添加约束:
FOREIGN KEY(字表属性名) references 父表名(父表主键)
在已创建表中添加约束:
alter table 字表名 add constraint 约束名 foreign key (自表名) references 父表名(父表主键)
用法直接套公式就可以了
级联删除、级联更新
create table score (
s_id int ,
s_name VARCHAR(10),
s_socre int ,
user_id int ,
-- 设置外键时,级联删除跟级联更新
FOREIGN KEY(user_id) references student(s_id) on delete cascade on update cascade
);
-- 设置外键时,级联删除跟级联更新
alter table score add constraint score_foreign foreign key (user_id) references student(s_id) on update cascade on delete cascade
create table score (
s_id int ,
s_name VARCHAR(10),
s_socre int ,
user_id int ,
-- 设置外键时,set null删除跟set null更新
FOREIGN KEY(user_id) references student(s_id) on delete set null on update set null );
-- 设置外键时,set null删除跟set null更新
alter table score add constraint score_foreign foreign key (user_id) references student(s_id) on update set null on delete set null