mysql表字段操作、约束、表关系
课件:MySQL(三)--约束
目录
- 课件:MySQL(三)--约束
- 知识点 一:表字段的增删改查
- 知识点二:约束
- 1、default 默认约束
- 2、not null 非空约束
- 3、unique key 唯一约束
- 4、primary key 主键约束
- 5、auto_increment 自增长约束
- 6、foreign key 外键约束
- 知识点三:表关系
- 1、一对一
- 2、一对多
- 3、多对多
知识点 一:表字段的增删改查
mysql简介--数据库---数据表---增删改查 crud 增删改查
show tables;
create table old_tb (id int,name char(10),age char(4));
show create table tb_name;
# 修改表名
alter table old_tb rename to new_tb;
show tables;
# 改字段
alter table new_tb change column id new_id int;
改变 列 老id 新id 类型
# 修改字段类型
alter table new_tb modify column name varchar(10);
alter table new_tb add age int;
总结:字段的增删改查基本上用不到,一般在设计表格的时候就会确定,这个知识点了解即可
知识点二:约束
约束可以理解为限制,生而为人 就会有道德约束和法律约束 等等约束 也就是限制同样我们数据得有约束才能让其更加高效更加完美
1、default 默认约束
# desc new_tb; 查看
create table t1 (id int default 110,name varchar(10));
select * from t1;
insert into t1(name) values ('huya'),('douyu');
select * from t1;
insert into t1(id,name) values (1,'xiongmao'),(55,'yese');
2、not null 非空约束
数据不能为空
create table t2(id int not null,
name varchar(20));
show create table t2;
id null 变成了 notnull
insert into t2 value(1,'zhangwuji'); # ok
insert into t2 (id) value(2); # ok
insert into t2(name) value('zhouzhiruo'); # 报错
# Field 'id' doesn't have a default value 字段'id'没有默认值
你指定非空约束,插数据就必须给值
拓展
# 我值就给一个null 这个为空不?
3、unique key 唯一约束
create table t3(id int unique key,
name varchar(20) not null
);
desc t3; # 查看一下
insert into t3 value(1, 'xiaoming'); # 报错
#Duplicate entry '1' for key 'id'键'id'重复输入'1'
非空且唯一,第一个非空且唯一的字段会默认为主键(没设置主键)
4、primary key 主键约束
create table t4(id int primary key,
name varchar(20) not null unique key
);
insert into t4(id,name) values(1,'yy'); # 创建成功
insert into t4(id,name) values(1,'yy'); # 重复输入
# Duplicate entry '1' for key 'PRIMARY '键'PRIMARY' 重复输入'1'
insert into t4(id,name) values(null,'qiye') #
#Column 'id' cannot be null 列'id'不能为null
insert into t4(id,name) values(3,'yy'); # 会不会报错 -- 报错
5、auto_increment 自增长约束
# 这个玩意儿需要和主键配合使用
大家想过没有,设定一个字段非空且唯一,但是这个字段如果要我们来输入非常痛苦,
primary key(id)
create table t5(id int primary key auto_increment,
name varchar(20)
);
desc t5;
insert into t5(name) values('xiaowang'),('xiaoxin'); # 不会报错 默认为1 自增长为1
create table tb5(id int primary key auto_increment,
name varchar(20) not null
)auto_increment=100;
insert into tb5(name) values('wang'),('ming'),('li');
改的是自增长的起始值
6、foreign key 外键约束
表与表之间产生联系
表与表发生关系 都是通过外键约束
你小时候有钱 那你父母一定有钱,你父母没有钱,那你小时候也一定没钱
E--R 模型 e 实体 r 关系
外键关联到另外一张表的主键上
create table a(a_id int primary key auto_increment,
a_name varchar(20) not null
);
insert into a values(1,'a1'),(2,'a2');
select * from a;
create table c(b_id int primary key,
b_name varchar(20) not null,
fy_id int not null,
foreign key(fy_id) references a(a_id)
);
外键是相对而言的,意思就是说 他是哪张表的外键
insert into b values (1,'a1',1),(2,'a2',2); # 可以
insert into b values (3,'qq',3); # 报错
#Cannot add or update a child row: a foreign key constraint fails (`python3`.`b`, CONSTRAINT `AB_id` FOREIGN KEY (`fy_id`) REFERENCES `a` (`a_id`))
无法添加或更新子行:外键约束失败
insert into a values (3,'a3');
insert into a values (4,'qq'); # 我们的外键是fy_id 所以其他的不同没有关系
知识点三:表关系
1、一对一
学生表 姓名,学号,学院等基本信息
学生详情表 学号 阶段,住址,联系方式等详细信息
一对一 用外键的方式关联
学生表中的学生只对应详情表的一条数据
create table student(id int primary key,
name varchar(10)
);
create table stu_detail(s_id int primary key,
age int,sex char(5),
foreign key(s_id) references student(id)
);
2、一对多
多个学生表(学院信息)对应一个学院表(只存自己的东西不会存学员的信息)
3、多对多
学生选课 课程对多个学生 学生对多个课程
需要创建中间表 有 两个字段(学生编号,课程编号)
课程表有两个字段(课程编号,课程名字)
主键加主键来实现的(联合主键)
学生 课程
0 0 ok
1 1 ok
2 2 ok
0 1 也OK
0 0 不行
create table course(course_id int primary key auto_increment,
course_name varchar(20) not null
);
create table student(s_id int primary key auto_increment,
s_name varchar(20) not null
);
create table zhongjian(s_id int,
cours_id int,
primary key(s_id,course_id),
foreign key(s_id) references student(s_id),
foreign key(course_id) references course(course_id)
);
insert into course values(1, 'python'),(2,'java'),(3,'c++');
insert into student values(1,'qiye'),(2,'gg');
insert into zhongjian values(1,3);
insert into zhongjian values(2,3);
insert into zhongjian values(1,1);