为了保证数据的合法性与完整性,对字段进行了除了数据类型以外添加额外的约束。
not null是非空约束,数据不能为空
create table student (id int,name char(10) not null);#名字不能为空
default 默认值约束,可以指定字段的默认值
create table user (id int,name char(10) not null,sex char(5) default "woman");
#当在一个大多数的情况都为一个值的时候,就可以用默认约束
unique 唯一性约束,该字段的值不能重复。比如,身份证,手机号,学号
unique其实是一种索引,索引是一种数据结构,用于提高查询效率。
unique可以为空,一张表中可以有多个唯一约束
单列唯一约束
create table t5(idcard char(18) unique);
多列联合唯一约束
create table t6(idcard char(18),phonenumber char(11),unique(idcard,phonenumber));#意思: 身份证相同 并且 手机号相同 那就叫相同
primary key称之为主键约束,用于唯一标识表中一条记录。
如何能做到唯一标识?
该字段,只要是唯一的,并且不为空即可。也就是说,从约束的角度来看主键约束和非空加唯一约束没有区别
那它们之间的区别是什么?
唯一约束,是一种索引,必然存在硬盘上的某个文件中,是物理层面(实实在在存在的数据)
primary key,是一种逻辑意义上的数据(实际上不存在)
换句话说,主键就是由唯一约束和非空约束 组成的约束
有主键和没有主键的区别?
1.无法区分两个相同记录,比如班级里有两个人名字相同
2.有主键则意味有这索引,效率更高
3.可以建立关联关系
create table stu (stuid int primary key,name char(3));#create table stu(stuid int unique not null,name char(3));
多列联合主键:
create table t8(idcard char(18),phonenumber char(11),primary key(idcard,phonenumber));#注意复合主键必须同时建立或者同时删除
auto_increment ,自动增长,通常搭配主键字段使用,可以自动为你的数据分配逐渐
如何分配的?
添加一条就自动加1 计数从1开始
create table t9(id int primary key auto_increment,name char(3));
如果主键是自动增长的,当你执行insert操作时要注意,插入的时候可以跳过这个字段或者插入的时候为null
修改自动增长的起始位置
alter table t9 auto_increment = 7;
foreign key 专门用于为表和表之间建立物理关联
现在有两张表,员工表和部门表
1. 从员工出发 员工对于部门来说 时 多个员工对应一个部门
2. 从部门出发 一个部门对应多个员工
虽然有了关系,但是两个表之间还是没有任何物理联系,插一个不存在的部门也没问题。
所以就需要外键来是的员工表和部门表产生关联。
添加外键约束时: 产生的限制
被关联的表需要先被创建
部门数据(主表)应该先插入 员工数据(从表)后插入
在删除部门数据前(主表)前 要保证该部门的员工数据都删除了
在更新部门编号前 要先保证没有员工关联到这个部门
简单的说 外键指的是 另一张的主键
外键加上以后 主表中的数据 删除 和更新时 都受到限制
先创建部门表(主表)
create table dept(id int primary key auto_increment,name char(10),manager char(10));
在创建员工表(从表)
create table emp(id int primary key auto_increment,name char(10),dept_id int,foreign key(dept_id) references dept(id));
常用于外键,指的是就是主表与从表同步更新和删除
create table class(id int primary key auto_increment,namechar(10));create table student(id int primary key auto_increment,name char(10),c_id int,foreign key(c_id) references class(id)on update cascade#同步更新on delete cascade#同步删除);#对主表的id进行更新#以及删除某条主表记录 来验证效果
表:学校表和课程表关系:一个学校有多个课程1 创建学校表:有学校id,学校名称,地址create table school(id int primary key auto_increment,name char(10),address char(20));2 创建课程表:有课程id,课程名称,课程价格,课程周期,所属学校create table course(id int primary key auto_increment,name char(10),price int,period char(5),school_id int,foreign key(school_id)references school(id)on update cascadeon delete cascade);3.创建学校insert into school(name,address) values(‘oldboyBeijing‘,‘北京昌平‘),(‘oldboyShanghai‘,‘上海浦东‘);4.创建课程insert into course(name,price,period,school_id) values(‘Python全栈开发一期‘,20000,‘5个月‘,2),(‘Linux运维一期‘,200,‘2个月‘,2),(‘Python全栈开发20期‘,20000,‘5个月‘,1);
表:学生表和客户表关系:一个学生对应一个客户#一定是student来foreign key表customer,这样就保证了:#1 学生一定是一个客户,#2 客户不一定是学生,但有可能成为一个学生1.创建客户表create table customer(id int primary key auto_increment,name varchar(20) not null,qq varchar(10) not null,phone char(16) not null);2.创建学生表create table student(id int primary key auto_increment,class_name varchar(20) not null,customer_id int unique, #该字段一定要是唯一的foreign key(customer_id) references customer(id) #外键的字段一定要保证uniqueon delete cascadeon update cascade);3.增加客户insert into customer(name,qq,phone) values(‘李飞机‘,‘31811231‘,13811341220),(‘王大炮‘,‘123123123‘,15213146809),(‘守榴弹‘,‘283818181‘,1867141331),(‘吴坦克‘,‘283818181‘,1851143312),(‘赢火箭‘,‘888818181‘,1861243314),(‘战地雷‘,‘112312312‘,18811431230);4.增加学生insert into student(class_name,customer_id) values(‘脱产3班‘,3),(‘周末19期‘,4),(‘周末19期‘,5);
表:学生表和老师表关系:一个学生上过多个老师的课,一个老师教过多个学生1.创建老师表create table teacher (id int primary key auto_increment,name char(10));2.创建学生表create table student (id int primary key auto_increment,name char(10));#为了避免重复无用的关系数据 关系表加上关联的主键约束3.创建关联表create table t_s (t_id int,s_id int,foreign key(t_id) references teacher(id),foreign key(s_id) references student(id),primary key(t_id,s_id));4插入数据insert into student value(null,"lxx");insert into teacher value(null,"exx");insert into t_s value(1,1);