mysql表的约束

目录

一、表的约束分类

1.not null 非空 (输入的数据内容不能为空)2.unique key 唯一键 (输入的数据可以为null或者跳过赋予他的值,但是如果输入数据不能相同)3.primary key 主键 (每个表中必须有唯一的主键)4.foreign key 外键(用于和其他的表建立某种关系)5.check 检查 (给输入的表的数据进行限制)

二、建表的同时建约束

1.先建立class表,与student表建立联系

2.给class表插入数据,并检验

3.创建student表,并且建立约束

4.查看约束条件

三、检查约束条件是否生效

1.检查年龄约束是否成功,约束条件成功

2.检查姓名约束条件是否成功,约束条件成功

3.检查地址的约束条件是否成功,约束条件成功

4.我们发现地址的约束虽然设置为唯一值,但是null却不限制

四、先建表后建立约束

1.先建表的大体结构,后建立约束

五、先建完表后添加约束条件

1.先建立student3表

2.设置stu_id的约束条件为主键

3.设置stu_name约束不为空

4.设置stu_age的约束条件大于18岁

5.设置stu_gender的约束条件默认为m,值得范围为m和f

6.设置stu_addr的约束条件为唯一

7.设置stu_class的约束条件为外键

8.查看student3的约束条件

一、表的约束分类

1.not null 非空 (输入的数据内容不能为空)
2.unique key 唯一键 (输入的数据可以为null或者跳过赋予他的值,但是如果输入数据不能相同)
3.primary key 主键 (每个表中必须有唯一的主键)
4.foreign key 外键(用于和其他的表建立某种关系)
5.check 检查 (给输入的表的数据进行限制)

二、建表的同时建约束

1.先建立class表,与student表建立联系

create table class(
    ->  class_id int primary key auto_increment comment '班级编号',
    ->  class_name varchar(50) not null comment '班级名称'
    -> ) auto_increment=1001;

mysql表的约束_第1张图片

2.给class表插入数据,并检验

insert into class values (null,'计科2001');

insert into class values (null,'计科2002');

select * from class;

mysql表的约束_第2张图片

3.创建student表,并且建立约束

create table student(

->        stu_id int primary key auto_increment comment '学号',

->        stu_name varchar(30) not null comment '姓名',

->        stu_age tinyint check (stu_age >= 18) comment '年龄',

->        stu_gender char(1) default 'm' check(stu_gender in ('m','f')) comment '性别',

->        stu_addr varchar(200) unique comment '住址',

->        stu_class int references class(class_id)

) auto_increment=1001;

mysql表的约束_第3张图片

4.查看约束条件

show table student\G

mysql表的约束_第4张图片

三、检查约束条件是否生效

1.检查年龄约束是否成功,约束条件成功

insert into student values(null,'张三',16,null,'shanxi',1003);

mysql表的约束_第5张图片

2.检查姓名约束条件是否成功,约束条件成功

insert into student values(null,null,16,null,'shanxi',1003)

3.检查地址的约束条件是否成功,约束条件成功

insert into student values(null,'lisi',18,null,'shanxi',1003);

4.我们发现地址的约束虽然设置为唯一值,但是null却不限制

四、先建表后建立约束

1.先建表的大体结构,后建立约束

create table student2(

->        stu_id int auto_increment comment '学号',

->        stu_name varchar(30) not null comment '姓名',

->        stu_age tinyint not null comment '年龄',

->        stu_gender char(1) default 'm' not null comment '性别',

->        stu_addr varchar(200) not null,

->        stu_class int not null,

->        constraint student2_pk primary key(stu_id),

->        constraint student2_chk_1 check (stu_age >= 18),

->        constraint student2_chk_2 check (stu_gender in ('m','f')),

->        unique key stu_addr_u (stu_addr),

->        constraint student2_class_id_fk foreign key(stu_class) references class(class_id)

) auto_increment=1001;

mysql表的约束_第6张图片

五、先建完表后添加约束条件

1.先建立student3表

create table student3(
    ->         stu_id int comment '学号',
    ->         stu_name varchar(30),
    ->         stu_age tinyint,
    ->         stu_gender char(1),
    ->         stu_addr varchar(200),
    ->         stu_class int
    -> );

mysql表的约束_第7张图片

2.设置stu_id的约束条件为主键

alter table student3 add constraint student_id_pk primary key(stu_id);

3.设置stu_name约束不为空

alter table student3 modify stu_name varchar(30) not null;

4.设置stu_age的约束条件大于18岁

alter table student3 add constraint student3_chk_1 check(stu_age >= 18);

5.设置stu_gender的约束条件默认为m,值得范围为m和f

alter table student3 modify stu_gender char(1) default 'm'; 

alter table student3 add constraint student3_chk_2 check(stu_gender in ('m','f'));

6.设置stu_addr的约束条件为唯一

alter table student3 add constraint student3_un_1 unique key(stu_addr);

7.设置stu_class的约束条件为外键

alter table student3 add constraint student3_fo_1 foreign key(stu_class) references class(class_id);

8.查看student3的约束条件

show create table student3\G

mysql表的约束_第8张图片

你可能感兴趣的:(mysql,mysql,数据库)