约束介绍:
在创建数据表的时候指定的对数据表的列的数据限制性的要求(对表的列的中的数据进行限制)
[scode type=”green”]给表中的列添加约束是为了确保数据的有效性,完整性与正确性。[/scode]
字段常见的约束有:
⾮空约束(not null):限制此列的值必须提供,不能为null。
唯⼀约束(unique):在表中的多条数据,此列的值不能重复。
主键约束(primary key):⾮空+唯⼀,能够唯⼀标识数据表中的⼀条数据。
外键约束(foreign key):建⽴不同表之间的关联关系。
除了这些以外还有默认约束。
[scode type=”yellow”]温馨提示:为方便查看,以下代码用的都是books数据表,使用时请自行替换,不要盲目复制粘贴。 ::aru:dead:: [/scode]
非空约束:
[scode type=”share”]限制数据表中的此列的值必须提供。[/scode]
创建表:设置图书表的 book_name 为非空约束使用 not null
create table books(
book_isbn char(4),
book_name varchar(10) not null,
book_author varchar(6)
);
唯一约束:
[scode type=”share”]在表中的多条数据,此列的值不能重复。[/scode]
创建表:设置图书表的 book_isbn 为唯一约束使用 unique
create table books(
book_isbn char(4) unique,
book_name varchar(10) not null,
book_author varchar(6)
);
主键约束:
[scode type=”share”]主键,就是数据库中记录的唯一标识,在一张表中只能有一个主键(主键可以是一个列,也可以是多个列的组合)当一个字段声明为主键之后,添加数据时字段数据不能为null且不能重复。[/scode]
创建表:设置图书表的 book_isbn 为主键使用 primary key
方法一:
create table books(
book_isbn char(4) primary key,
book_name varchar(10) not null,
book_author varchar(6)
);
方法二:
create table books(
book_isbn char(4),
book_name varchar(10) not null,
book_author varchar(6),
primary key(book_isbn)
);
外键约束:
[scode type=”share”]外键约束——将⼀个列添加外键约束与另⼀张表的主键(唯⼀列)进⾏关联之后,这个外键约束的列添加的数据必须要在关联的主键字段中存在[/scode]
案例:学⽣表与班级表。
一. 先创建班级表
create table classes(
class_id int primary key auto_increment,
class_name varchar(40) not null unique,
class_remark varchar(200)
);
二.再创建学⽣表(在学⽣表中添加外键与班级表的主键进⾏关联)
方法一:在创建表的时候,定义cid字段,并添加外键约束。
[scode type=”share”]由于cid列要与classes表的class_id进⾏关联,因此cid字段类型和⻓度要与class_id⼀致[/scode]
create table students(
stu_num char(8) primary key,
stu_name varchar(20) not null,
stu_gender char(2) not null,
stu_age int not null,
cid int,
constraint FK_STUDENTS_CLASSES foreign key(cid) references classes(class_id)
);
方法二:
1.先创建表。
create table students(
stu_num char(8) primary key,
stu_name varchar(20) not null,
stu_gender char(2) not null,
stu_age int not null,
cid int
);
2.在创建表之后,为cid添加外键约束。
alter table students add constraint FK_STUDENTS_CLASSES foreign key(cid) references classes(class_id);
3.删除外键约束:
alter table students drop foreign key FK_STUDENTS_CLASSES;
三.再向班级表添加班级信息:
insert into classes(class_name,class_remark)
values('Java14','2021');
insert into classes(class_name,class_remark)
values('HTML14','2022');
insert into classes(class_name,class_remark)
values('python14','2023');
insert into classes(class_name,class_remark)
values('MySQL14','2024');
四.最后向学⽣表中添加学⽣信息:
insert into students(stu_num,stu_name,stu_gender,stu_age,cid)
values('20210102','孟某某','⼥',18, 4 );
# 添加学⽣时,设置给cid外键列的值必须在其关联的主表classes的classs_id列存在
insert into students(stu_num,stu_name,stu_gender,stu_age,cid)
values('20210103','张某某','男',19, 6 );
默认约束:
[scode type=”share”]默认约束,顾名思义,就是指定某列的默认值。[/scode]
创建表:设置图书表的 book_author 为唯一约束使用 default 后面跟上默认值
create table books(
book_isbn char(4) primary key,
book_name varchar(10) not null,
book_author varchar(6) default'这里输入默认值',
删除数据表的主键约束:
alter table books drop primary key;
创建表之后添加主键约束:
## 创建表时没有添加主键约束
create table books(
book_isbn char(4),
book_name varchar(10) not null,
book_author varchar(6)
);
## 创建表之后添加主键约束
alter table books modify book_isbn char(4) primary key;
主键⾃动增⻓:
[scode type=”share”]在我们创建⼀张数据表时,如果数据表中有列可以作为主键(例如:学⽣表的学号、图
书表的isbn)我们可以直接这是这个列为主键;
当有些数据表中没有合适的列作为主键时,我们可以额外定义⼀个与记录本身⽆关的列
(ID)作为主键,此列数据⽆具体的含义主要⽤于标识⼀条记录,在mysql中我们可以
将此列定义为int,同时设置为 ⾃动增⻓ ,当我们向数据表中新增⼀条记录时,⽆需提供
ID列的值,它会⾃动⽣成[/scode]
定义主键⾃动增⻓:
定义int类型字段⾃动增⻓:auto_increment
create table books(
book_id int primary key auto_increment,
book_name varchar(20) not null,
book_remark varchar(100)
);
[scode type=”red”]注意:⾃动增⻓从1开始,每添加⼀条记录,⾃动的增⻓的列会⾃定+1,当我们把某条记录删
除之后再添加数据,⾃动增⻓的数据也不会重复⽣成(⾃动增⻓只保证唯⼀性、不保证连续性)[/scode]