外鍵:用來創建兩張表之間的關係mysql
研究表與表之間的關係:sql
一、定義一張員工部門表3d
二、將全部的數據都存放在一張表裏的問題code
三、全部用拆分表來解決以上問題blog
四、拆分表後須要給兩張表創建一種關係,這就須要用到外鍵來創建鏈接索引
如何肯定表與表之間的關係是:一對多、多對多、一對一rem
注意:在MySQL中沒有多對一,只有一對多it
站在兩張表的位置去思考,凡是單向的能 多對一 的表的關係,稱之爲 一對多 的外鍵關係table
外鍵語法:foregin key(當前表中創建關係的外鍵字段) references 被關聯表名(id)class
建立兩張表,必須先建立被關聯表,再建立關聯表(外鍵設置在關聯表中,一對多中的多的關聯表中)
被關聯表:dep
create table dep(id int primary key auto_increment, dep_name varchar(20), dep_desc varchar(255));
關聯表:emp
create table emp(id int primary key auto_increment, name varchar(16), age int, sex enum('male','female'), dep_id int, foreign key(dep_id) references dep(id));
插入數據,必須先插入被關聯表的數據,再插入關聯表的數據
被關聯表:dep
insert into dep(dep_name,dep_desc) values('財務部','發工資'),('人事部','招人才'),('技術部','幹活');
關聯表:emp
insert into emp(name,age,sex,dep_id) values('小李','40','female',1),('小王','35','male',3),('小張','28','male',3),('小王','24','female',2);
插入被關聯表id以外的數據,會報錯,插不進去
insert into emp(name,age,sex,dep_id) values('vicky','24','female',4);
更新數據與刪除數據
直接更新關聯表或被關聯表中的數據會報錯,須要先刪除被關聯表中的記錄
刪除:須要先刪除關聯表中的記錄再刪除被關聯表中的記錄
每次更新或刪除都很麻煩,因此在建立關聯表時用級聯更新,級聯刪除
級聯更新與級聯刪除
級聯更新:on update cascade
級聯刪除:on delete cascade
被關聯表:dep
create table dep1(id int primary key auto_increment, dep_name varchar(20), dep_desc varchar(255));
關聯表:emp
create table emp1(id int primary key auto_increment, name varchar(16), age int, sex enum('male','female'), dep1_id int, foreign key(dep1_id) references dep1(id) on update cascade on delete cascade);
被關聯表:dep
insert into dep1(dep_name,dep_desc) values('財務部','發工資'),('人事部','招人才'),('技術部','幹活');
關聯表:emp
insert into emp1(name,age,sex,dep1_id) values('小李','40','female',1),('小王','35','male',3),('小張','28','male',3),('小王','24','female',2);
更新數據:
update dep1 set id = 8 where id=1;
刪除數據:
delete from emp1 where id=2;
delete from dep1 where id=2;
也必須站在兩張表的位置去思考,凡是雙向的能 多對一 的表的關係,稱之爲 多對多 的外鍵關係
多對多直接創建兩張表,沒法知道,哪張表是被關聯表,利用第三張表爲兩張表創建「多對多的外鍵關係」
被關聯表:book
create table book(id int primary key auto_increment, title varchar(20), price int);
關聯表:author
create table author(id int primary key auto_increment, name varchar(18), age int);
第三方表:book_author
create table book_author(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);
插入數據:
insert into book(title, price) values('三體8',200),('活着8',48); insert into author(name,age) values('Mr沈',18),('tate',20);
插入關聯表數據:
insert into book_author(book_id,author_id) values(1,1),(1,2),(2,1),(2,2);
若是插入不在book id或author id以外的數會報錯
insert into book_author(book_id,author_id) values(3,2);
更新與刪除
更新:
update book set price=688 where id=1; update author set id=4 where id=2;
刪除:
delete from author where id=4;
將一張數據量比較大的表拆分爲兩張表
一 一對應的關係——》一對一的外鍵關係
foreign key應該創建在使用頻率較高的一方,關聯外鍵必須設置惟一
建立被關聯表:customer
create table customer(id int primary key auto_increment, name varchar(18), media varchar(40));
建立關聯表:student
create table student(id int primary key auto_increment, addr varchar(40), phone char(11), customer_id int unique, foreign key(customer_id) references customer(id) on update cascade on delete cascade);
插入數據
insert into customer(name,media) values('Mr沈','facebook'),('vicky','ig'),('tate','vk'); insert into student(addr,phone,customer_id) values('上海','13211934021',1),('南京','19322218302',2),('北京','18312873652',3);
插入的數據必須一一對應,不然會報錯
insert into student(addr,phone,customer_id) values('上海','12609217331',1);
更新和刪除數據
更新數據:
update customer set name='tank' where id=1;
刪除數據:
delete from student where id=1;
alter table 表名 rename 新表名;
添加到最後一列
alter table 表名 add 字段名 數據類型 [完整性約束條件],add 字段名 [完整性約束條件];
添加到第一列
alter table 表名 add 字段名 數據類型 [完整性約束條件] first;
添加到某一列以後
alter table 表名 add 字段名 數據類型 [完整性約束條件] after;
alter table 表名 drop 字段名;
修改數據類型
alter table 表名 modify 字段名 數據類型 [完整性約束條件];
修改字段名,保留字段類型
alter table 表名 change 舊字段名 新字段名 舊數據類型 [完整性約束條件];
修改字段名與字段類型
alter table 表名 change 舊字段名 新字段名 新數據類型 [完整性約束條件];
複製表結構 + 記錄(key不會被複制:主鍵、外鍵、索引)
create table 新表名 select * from 舊錶名;
只複製表結構
create table 新表名 select * from 舊錶名 where 1=2;