MySQL基础学习(二)表约束,表关系,简单易懂,巴巴适适

 目录

mysql约束条件

1.非空约束(NOT NULL)

2.唯一约束(unique key)

3.主键约束(primary key)

4.自增长约束(auto_increment)

5.默认约束(default)

6.外键约束(foreign key)

表关系

1.一对多

2.一对一

3.多对多


一个数据库中包含多个表,表中数据是需要约束条件的,才能使数据更稳定

mysql约束条件

约束条件默认非空唯一自增长主键外键
关键字defaultnot nullunique keyauto_incrementprimary keyforeign key

1.非空约束(NOT NULL)

#NOT NULL 限制一个字段的值不能为空,insert的时候必须赋值
#例:
create table tb if not exit(id int NOT NULL , name varchar(20));
insert into tb values(1,"非空约束");
#设置id默认值为0(modify修改默认值)
alter tanle tb modify id int not null default 0;
indert into tb(name) values("feikong");

2.唯一约束(unique key)

#约束字段值是唯一的,在表里值是没有重复的
#例如:身份证号码
create table tb(num int unique key ,name varchar(20));

create table tb (id int(20) primary key, name varchar(20) unique key);

3.主键约束(primary key)

#主键保证表格记录唯一性。表格标志
#primary key == not null + unique key
#创建表就创建主键
create table tb (id int primary key, name varchar(20));
#表创建好,添加主键
alter table tb add id int primary key auto_increment;#原表没有id字段
alter table tb modify id int primary key auto_increment;#原表有id字段

4.自增长约束(auto_increment)

#自动编号,和主键组合使用,一个表里面只能有一个自增长
#auto_increment只能用在主键上
create table tb (id int primary key auto_increment, name varchar(20));
insert into tb values(2,"自增长");
#默认情况下,起始值为1,每次增量为1
insert into tb(name) values("自增长");#默认id为1
#全字段插入,必须指定自增长值为default/null,插入记录时,从最大的id往后插入
insert into tb values(default , "666");
insert into tb values(null ,"888");
#如果最大值被删除了,插入新纪录是,依然会从最大值往后插入,例如:删除888后(888的id为3),插入一个(null,"川川")此时id为4,不为3,除非规定了id为3

5.默认约束(default)

#设置初始值,插入数据时,如果不赋值,使用默认值
create table tb(id int ,sex tinyint default 1);
insert into tb(id) values(1);
insert into tb values(2,default);#全字段插入时

6.外键约束(foreign key)

#学生和学生详情之间的关系(1:1):a表放置普通信息,b表放置敏感信息
create table a(id int primary key, name varchar(20));
create table b(id int primary key, phone int,a_id int,foreign key(a_id) references a(id));
#注意:b表中插入外键字段a_id,只能添加a表id中已有的数据
insert into a values(1,"何"),(2,"龙"),(3,"李");
insert into a values(1,1001,1),(2,1002,2),(1003,3);
#在删除是由于a表中的id是b表的外键字段,是被参照的数据,a表数据id不能修改和删除
#一定要删除,需要先删除b表中的对应id的数据
delete from b where id=1;
delete from a where id=1;

表关系

1.一对多

#一对多关系实现
#set null 表示删表会把外键约束的一块儿删除
#创建学生表
create table student(id int primary key auto_increment,name varchar(20) nor null,clg_id int,constraint c_s foreign key(clg_id) references college(id) on delete set null);
insert into student values(1,"何",1001),(2,"龙",1002),(3,"李",1004),(4,"张",1003);

2.一对一

#详情表
create table student_detail(id int primary key auto_increment, phone varchar(20),stu_id int unique key,constraint 'sd_s' foreign key(stu_id) references student(id));

3.多对多

#创建学院表
create table college(id int primary key auto_increment,name varchar(20) not null);
insert into college values(1001,"计算机学院"),(1002,"马克思学院"),(1003,"电信学院"),(1004,"师范学院");
#一对多关系实现
#set null 表示删表会把外键约束的一块儿删除
#创建学生表
create table student(id int primary key auto_increment,name varchar(20) nor null,clg_id int,constraint c_s foreign key(clg_id) references college(id) on delete set null);
insert into student values(1,"何",1001),(2,"龙",1002),(3,"李",1004),(4,"张",1003);
#实现多对多
#创建课程表
create table course(id int primary key auto_increment, name varchar(20) not null);
insert into course(name) values ("c++"),("java"),("高数");
#创建一个中间表吧学生表和课程表串起来
create table sel(stu_id int, cou_id int , primary key(stu_id,cou_id),constraint 's_s' foreign key(stu_id) references 's_c' student(id), foreign key(cou_id) references course(id));
insert into sel values(1,2),(2,1),(3,3);
#联合主键

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值