Python学习day43-数据库(多表关系)字段的详细操作外键多表关系一对一一对多多对多
Python学习day43-数据库(多表关系)
字段的详细操作
前面我们学过添加字段以及修改的方式,今天我们主要需要了解的东西是通过alter来实现的.
xxxxxxxxxx
24
1
create table tf1(
2
id int primary key auto_increment,
3
x int,
4
y int
5
);
6
# ALTER TABLE 表名 MODIFY 字段名1 数据类型 FIRST|AFTER 字段名2
7
# 修改,modify可以修改一个已有字段的类型,位置以及默认值,change则是可以直接修改一个已有字段为一个全新的字段
8
alter table tf1 modify x char(4) default '';
9
alter table tf1 modify x char(4) first;# 把字段x这一列移到最开始
10
alter table tf1 modify x char(4) after y int;# 把字段x这一列移到y后面
11
alter table tf1 change y m char(4) default '';
12
13
# 增加
14
alter table 表名 add 新字段 类型[(长度) 约束条件];# 默认末尾增加
15
alter table tf1 add z int unsigned;
16
17
# 头部添加
18
alter table 表名 add 新字段 类型[(长度) 约束条件] first;# 头部添加
19
20
# 某字段后添加
21
alter table 表名 add 新字段 类型[(长度) 约束条件] after 旧字段名;
22
23
# 删除
24
alter table 表名 drop 字段名;
外键
上节中我们了解到,外键和主键一样,是创建表的时候一种约束条件.其主要作用也和主键比较类似,是索引的一种.但是其和主键不同的一点在于,MySQL会为表中所有的主键进行索引,但是外键字段不会自动索引,只有用户去明确定义这个索引,我们才可以使用.
xxxxxxxxxx
1
1
# 外键的语句如下
2
foreign key(所在表的外键字段) references 关联表(关联字段)
3
# 举个栗子:
4
foreign key(detail_id) references author_detail(id)
5
'''
6
注意:
7
1. 外键的字段名是自定义的,但为了索引方便通常将外键名字定义为 关联表_关联字段
8
2. 外键字段本身可以唯一或者不唯一,但是外键所关联的字段一定是唯一的,这点非常重要
9
'''
多表关系
多表关系,其实多个表之间的关系只有三种,即一对一,一对多和多对多,
举个栗子:
x
1
1. 一对一:外键在任何一方定义都可以,此时外键要设置唯一键,即unique
2
作者(author):id,name,gender,age,mobile,info,address
3
作者详情(author_detail):id,info,address,author_id
4
2. 一对多:外键必须放在多的那边,且外键值不唯一
5
书(book):id,name,price,publish_id
6
出版社(publish):id,name,address,phone
7
3. 多对多:一定要创建第三张关系表,每一个外键不唯一,但是多个外键可以建立联合唯一
8
作者(author):id,name,age
9
出版社(publish):id,name,address
10
关系表(author_publish):id,author_id,publish_id
接下来我们就根据上面的栗子来创建下面的列表.
一对一
x
1
'''
2
外键在任何一方定义都可以,此时外键要设置唯一键,即unique
3
'''
4
# 作者详情表
5
create table author_detail(
6
id int primary key auto_increment,
7
info varchar(256),
8
address varchar(256)
9
);
10
11
# 作者表
12
create table author(
13
id int primary key auto_increment,
14
name varchar(64) not NUll,
15
mobile char(11) unique not NUll,
16
sex enum('male','female')default 'male',
17
age int default 0,
18
detail_id int unique not null,# 建立外键字段detail_id,且有唯一值,即不能重复,但其实这只是个普通字段而已,如果没有下面的绑定语句的话.
19
foreign key(detail_id) references author_detail(id)
20
on update cascade
21
on delete cascade,# 创建外键并建立级联关系
22
);
23
24
25
# 需要先插入被关联表,然后再插入关联表,否则会插入数据失败
26
insert into author_detail(info,address) values('Tom_info','Tom_address');
27
insert into author(name,mobile,detail_id) values('Tom','123456789',1);
28
29
# 修改关联表
30
update author set detail_id=3 where detail_id=2;# 3不存在
31
update author set detail_id=1 where detail_id=2;# 1已经被关联
32
insert into author_detail(info,address) values('Tom_info_sup','Tom_address_sup');
33
update author set detail_id=2 where detail_id=2;
34
35
# 删除关联表
36
delete from author detail_id=3;# 直接删除
37
38
# 修改被关联表author_detail
39
update author_detail set id=10 where id=1;# 因为有级联关系,所以可以修改,同时关联表里面的detail_id也会修改,实际开发中几乎没有应用场景,因为这只是单纯修改了关联字段的值,关联的本身关系并没有改变,所以几乎没有意义
40
41
# 删除被关联表
42
delete from author where detail_id=10;# 可以删除,对被关联表没有影响
43
delete from author_detail where id=10;# 不止author_detail里面的数据删除了,把关联表author里面的记录也级联删除了
一对多
xxxxxxxxxx
1
51
1
'''
2
一对多:外键必须放在多的那边,且外键值不唯一
3
4
出版社(publish):id,name,address,phone
5
6
书(book):id,name,price,publish_id
7
'''
8
9
create table publish(
10
id int primary key auto_increment,
11
name varchar(64),
12
address varchar(256),
13
phone char(20)
14
);
15
16
# 书(book):id,name,price,publish_id,author_id
17
create table book(
18
id int primary key auto_increment,
19
name varchar(64) not null,
20
price decimal(5,2) default 0,
21
publish_id int,#一对多的外键不能设置唯一,因为同时有多本书对应出版社,一旦外键设置唯一值,出版社一个id只能出一本书,这是不符合实际情况的
22
foreign key(publish_id) references publish(id)
23
on update cascade
24
on delete cascade
25
);
26
27
# 增:先插入被关联表(publish)的数据,然后插入关联表(book)的数据
28
insert into publish(name,address,phone) values
29
('人民出版社','北京','010-110'),
30
('西交大出版社','西安','010-119'),
31
('老男孩出版社','上海','010-120');
32
33
insert into book(name,price,publish_id) values
34
('西游记',6.66,1),
35
('东游记',8.66,1),
36
('Python入门',9.66,2);
37
insert into book(name,price,publish_id) values
38
('西游记',6.66,4);# 报错,因为publish_id为4不存在
39
40
# 更新:直接更新被关联表(publish),关联表(book)外键会 级联更新
41
update publish set id=10 where id=1;
42
# 直接更新关联表(book)的外键,修改的值对应被关联表(publish)的主键如果存在,可以更新成功,否则会失败
43
update book set publish_id=2 where id=4;# 成功
44
update book set publish_id=1 where id=4;# 失败
45
46
# 删:
47
# 删被关联表,关联表会被级联删除
48
49
delete from publish where id=2;
50
# 删关联表,被关联表不会发生变化
51
delete from book where publish_id=3;
多对多
xxxxxxxxxx
1
66
1
'''
2
多对多:一定要创建第三张关系表,每一个外键不唯一,但是多个外键可以建立联合唯一
3
作者(author):id,name,age
4
出版社(publish):id,name,address
5
作者与出版社关系表:id,author_id,publish_id
6
7
关系表关联着作者和出版社两张表,在表结构上,作者与出版社量表键没有任何关系
8
9
10
'''
11
create table author(
12
id int primary key auto_increment,
13
name varchar(16),
14
age int unsigned default 0
15
);
16
17
create publish(
18
id int primary key auto_increment,
19
name varchar(64),
20
address varcher(256)
21
);
22
23
create author_publish(
24
id int primary key auto_increment,
25
# 关系表一定有多个外键,关联着多张表,还可以有着一些自定义的字段,比如作者和出版社的签约时间,这些字段通常与两者都有关系,且唯一
26
# 关联作者表
27
author_id int,
28
foreign key(author_id) refenrences author(id)
29
on undate cascade
30
on delete cascade,
31
# 关联出版社表
32
publish_id int,
33
foreign key(publish_id) refenrences publish(id)
34
on undate cascade
35
on delete cascade,
36
# 建立两个字段的联合唯一,即作者和出版社的绑定关系,同一时间一个作者只能绑定一个出版社,重复绑定会无效
37
unique(author_id,publish_id)
38
);
39
40
41
# 增:两张被关联表,没有前后关系,但关系表必须在两个表都提供数据后才能进行关系匹配
42
insert into author(name,age) values
43
('tom',67),
44
('marry',76),
45
('ffx',3);
46
insert into publish(name,address) values
47
('老孩出版社','上海'),
48
('小女孩出版社','北京');
49
50
# 操作关系表:
51
insert into author_publish(author_id,publish_id) values
52
(1,1),
53
(1,2),
54
(2,1),
55
(2,2),
56
(3,1);
57
58
# 关系表操作:增,删,改,只要两张被关系表有提供对应的操作数据,都可以操作成功,且对两张被关系的表没有影响
59
60
# 操作两张被关系表:
61
#增:两个表独立,不会互相影响
62
insert into publish(name,address) values('西交大出版社','西安');
63
# 改:关系表都会级联更新
64
update publish set id=10 where id=1;
65
# 删:关系表都会级联删除
66
delete from author where name='tom';