1. Foreign key: used to establish the relationship between the two tables
- many
- many to many
- one
Study the relationship between tables and table:
1. Define a department employee table
id, name, gender, dep_name, dep_desc
- All data is stored in a table of drawbacks:
1. The structure is not clear ---> nonlethal
2. waste of space ---> lethal
3. Scalability poor ---> disadvantages can not be ignored
- like All py python code stored in a file, a strong coupling together ----> ---- decoupled> split table
- Split the table to solve the above problems.
- between two tables need to establish a strong relationship between the use of "foreign keys"
- How !!!!! ****** confirm the relationship between the table and the table is ( one to many, many to many, one to one )
- Note: To establish a relationship between two tables, must stand to think in two locations:
- location of the employees table: multiple employees can correspond to a department? can!!!
- Employees and departments: Many
- Many-sector employees to form table
- standing position of the department table: multiple departments can correspond to an employee? Can not !!!
Summary: Any way many-to-table relationships, called many of the foreign key relationships.
- Foreign key: Syntax: Foreign Key (current relationship table foreign key field) references are associated with the table name (id)
- many
# Create two tables
1. must first establish the association table, then association table
# 被关联表: dep: create table dep( id int primary key auto_increment, dep_name varchar(16), dep_desc varchar(255) ); # 关联表: emp: create table emp( id int primary key auto_increment, name varchar(16), age int, gender enum('male', 'female', 'others') default 'male', dep_id int not null, foreign key(dep_id) references dep(id) );
Note: KEY representative of a foreign key in MUL
# Insert data:
- 1 must be inserted in the data relation table (DEP), and then inserted into association table (EMP) data.
# DEP: INSERT INTO DEP (dep_name, dep_desc) values ( ' Nb_ Ministry of Foreign Affairs ' , ' international ambassador sector ' ), ( ' Sb_ Teaching Department ' , ' making programmers department !!!! ' ), ( ' technology ' , ' technology limited sector ' ); # EMP: INSERT INTO EMP (name, Age, Gender, the dep_id) values ( ' Tank ' ,. 17, ' MALE ' ,. 1 ), ( 'jason', 70, 'male', 2), ('sean', 50, 'male', 2), ('egon', 88, 'male', 2), ('owen', 95, 'female', 3); # 报错, insert into emp(name, age, gender, dep_id) values('大饼', 100, 'others', 999);
- Cascade update and cascade delete (to change along with associated data)
- on update cascade
- on delete cascade
- Create a table
#被关联表: dep2: create table dep2( id int primary key auto_increment, dep_name varchar(16), dep_desc varchar(255) ); #关联表: emp2: create table emp2( id int primary key auto_increment, name varchar(16), age int, gender enum('male','female','others') default 'male', dep_id int not null, foreign key(dep_id) references dep2(id) on update cascade on delete cascade );
- insert data
# DEP: INSERT INTO dep2 (dep_name, dep_desc) values ( 'Nb_ Ministry of Foreign Affairs ' , ' international ambassador sector ' ), ( ' Sb_ Teaching Department ' , ' making programmers department !!!! ' ), ( ' technology ' , ' technology limited sector ' ); # EMP: INSERT INTO EMP2 (name, Age, Gender, the dep_id) values ( ' Tank ' ,. 17, ' MALE ' ,. 1 ), ( ' Jason ' , 70, ' MALE ',2), ('sean',50,'male',2), ('egon',88,'male',2), ('owen',95,'female',3); #报错 insert into emp(name,age,gender,dep_id)values('大饼',100,'others',999);
- updating data or deleting data
- update records Update dep2 the SET the above mentioned id = 200 the WHERE the above mentioned id = 1 ; - delete record the Delete from dep2 the WHERE the above mentioned id = 200;
Because cascade, data associated with the change of 200
Note: mysql is not a many, many only
- many to many
We must also stand to think about the position of two tables;
- Demonstration error:
#- 创建book表 create table book( id int primary key auto_increment, title varchar(20), price int, book_content varchar(255), author_id int, foreign key(author_id) references author(id) on update cascade on delete cascade ); #- 创建author表 create table author( id int primary key auto_increment, name varchar(16), age int, book_id int, foreign key(book_id) references book(id) on update cascade on delete cascade );
- 问题: 无法知道哪张表是被关联表
- 利用第三张表,为两张表建立“多对多外键关系”。
#-book: create table book( id int primary key auto_increment, title varchar(20), price int, book_content varchar(255) ); #-auther: create table author( id int primary key auto_increment, name varchar(16), age int ); #-book2author: create table book2author( id int primary key auto_increment, book_id int, author_id int, foreign key(book_id) references book(id) on update cascade on delete cascade, foreign key(author_id) references author(id) on update cascade on delete cascade );
- 插入数据
#- book insert into book(title, price, book_content) values ('金瓶mei', 199, '讲述朦胧时光的小故事'), ('python从入门到断气', 2000, '学习如何一夜秃头'), ('三体', 200, '跟着大佬进入宇宙奇幻世界') ; - author insert into author(name, age) values ('egon', 68), ('jason', 88); - book2author: insert into book2author(book_id, author_id) values (1, 1), (1, 2), (2, 2), (3, 1); # 报错, 插入的数据,book_id, author_id必须存在 insert into book2author(book_id,author_id) values(4, 4); # 更新或删除 #更新 update book set price =666 where id =1; update book set id=4 where id=1; #删除 delete from book where id=4; (会删除关联表内容)
- 一对一:
- user_info:
id, name, age, gender, hobby, id_card
- user:
id , name, age, detail_id(外键)
- detail:
id, gender, hobby, id_card
user与detail表建立了 一对一的外键 关系。
foreign key 应该建在 使用频率较高的一方。
-创建表
#被关联表 create table customer( id int primary key auto_increment, name varchar(16), media varchar(32) ); #关联表 create table student( id int primary key auto_increment, addr varchar(255), phone_char(11), id_card char(18), # 外键必须设置成唯一的 customer_id int unique, forign key(customer_id) references customer(id) on update cascade on delete cascade );
- 插入数据
insert into customer(name,media) values ('hcy','facebook'), ('zsb1','ig'), ('zsb2','vk'), ('hb','探探'); insert into student(addr,phone,id_card,customer_id) values ('上海', '15214546711', '440888888888888888', 1), ('北京', '18888888888', '440777777777777777', 2); # 报错,一对一,关系必须 一一对应 insert into student(addr, phone, id_card, customer_id) values ('上海', '15214546711', '440888888888888888', 1); - 插入数据: insert into
修改表的操作
- 语法: 注意: mysql 关键字不区分大小写
1. 修改表名
ALTER TABLE 表名 RENAME 新表名;
2. 增加字段
ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件…],ADD 字段名 数据类型 [完整性约束条件…]; # 添加到最后一列
ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件…] FIRST; # 添加到第一列
ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件…] AFTER 字段名; # 添加到某一列之后
3. 删除字段
ALTER TABLE 表名 DROP 字段名;
4. 修改字段
ALTER TABLE 表名 MODIFY 字段名 数据类型 [完整性约束条件…]; # 修改数据类型
ALTER TABLE 表名 CHANGE 旧字段名 新字段名 旧数据类型 [完整性约束条件…]; # 修改字段名,保留字段类型
ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型 [完整性约束条件…]; # 修改字段名与字段类型
- 复制表的操作:
复制表结构+记录 (key不会复制: 主键、外键和索引)
mysql> create table new_service select * from service;
只复制表结构
# 将select * from service where 1=2; ---> 不要真实数据,需要表结构
mysql> create table new_customer select * from customer where 1=2;