mysql書外來鍵 MySQL學習5 外來鍵續

2021-10-17 18:04:05 字數 3002 閱讀 3844

## 一對多補充

# 級聯刪除 (當一張表刪除記錄時,若繫結給了另一張表中的外來鍵,則一同刪除另一張表中關聯的記錄)

# 更新於刪除都需要考慮到關聯與被關聯的關係》同步更新與同步刪除

# 被關聯表

create table dep(

id int primary key auto_increment,

dep_name char(10),

dep_comment char(60)

# 關聯表

create table emp(

id int primary key auto_increment,

name char(16),

gender enum('male','female') not null default 'male',

dep_id int,

foreign key(dep_id) references dep(id)

# 級聯更新(同步更新)

on update cascade

# 級聯刪除(同步刪除)

on delete cascade

# 先插入被關聯表資料

insert into dep(dep_name,dep_comment) values

('教學部','教授python課程'),

('***','上海校區形象大使'),

('技術部','技術能力部門');

# 再插入關聯表資料

insert into emp(name,gender,dep_id) values

('a','male',1),

('b','male',2),

('c','male',1),

('d','male',1),

('e','female',3);

# 檢視兩張表資料

select * from emp;

select * from dep;

# 刪除部門後,對應的部門裡面的員工表資料對應刪除

delete from dep where id=3;

# 更新部門後,對應員工表中的標示部門的字段同步更新

update student set sid=3 where sid=2;

## 多對多

# 建立第三張表,該表中有乙個欄位fk左表的id,還有乙個欄位是fk右表的id

# 1) 先建立作者表

create table author(

id int primary key auto_increment,

name char(16)

# 2) 建立圖書表

create table book(

id int primary key auto_increment,

bname char(16),

price int

# 3) 建立第三張關聯表

create table author2book(

id int primary key auto_increment,

author_id int,

book_id int,

foreign key(author_id) references author(id)

on update cascade

on delete cascade,

foreign key(book_id) references book(id)

on update cascade

on delete cascade

## 一對一

# 左表的一條記錄唯一對應右表的一條記錄,反之也一樣

# 被關聯表

create table customer(

id int primary key auto_increment,

name char(20) not null,

qq char(10) not null,

phone char(16) not null

# 關聯表

create table student(

id int primary key auto_increment,

class_name char(20) not null,

customer_id int unique, # 注意: 該欄位一定要是唯一的

foreign key(customer_id) references customer(id) #外來鍵的字段一定要保證unique

on delete cascade

on update cascade

# 三種外來鍵關係都是用foreign key,區別在於如何使用以及其他條件限制即可做出三種關係

## 表的一些補充操作

1. 修改表名

alter table 表名 rename 新錶名;

2. 增加字段

alter table 表名 add 欄位名 資料型別 [完整性約束條件…],

add 欄位名 資料型別 [完整性約束條件…];

alter table 表名 add 欄位名 資料型別 [完整性約束條件…] first;

alter table 表名 add 欄位名 資料型別 [完整性約束條件…] after 欄位名;

3. 刪除字段

alter table 表名 drop 欄位名;

4. 修改字段 # modify只能改欄位資料型別完整約束,不能改欄位名,但是change可以!

alter table 表名 modify 欄位名 資料型別 [完整性約束條件…];

alter table 表名 change 舊欄位名 新欄位名 舊資料型別 [完整性約束條件…];

alter table 表名 change 舊欄位名 新欄位名 新資料型別 [完整性約束條件…];

5.複製表結構+記錄 (key不會複製: 主鍵、外來鍵和索引)

create table 新的表名 select * from 已存在的表名

6.只複製表結構

create table 新錶名 like 已存在的表名;

mysql 外來鍵 del 記錄 MySQL 外來鍵

在mysql中 1 mysql 資料表主要支援六種型別 分別是 bdb heap isam merge myisam innobdb。這六種又分為兩類,一類是 事務安全型 transaction safe 包括bdb和innodb 其餘都屬於第二類,稱為 非事務安全型 non transaction...

mysql學習之路 外來鍵

連線多張表到一起,不管記錄數如何,字段數一定會增加。分類 內連線,外連線。自然連線,交叉連線,交叉連線 cross join 笛卡爾積 內連線 inner join,左右兩張表有連線條件匹配 不匹配自動忽略 外連線 left right join 主表的一條記錄一定會存在匹配保留副表資料,否則置空 ...

MySQL學習4 外來鍵

把所有資料都存放於一張表的弊端 1.組織結構不清晰 2.浪費硬碟空間 3.擴充套件性極差 一對多 foreign key 注意 mysql中表與表的關係,只有 一對多 沒有多對一 foreign key的用法 1 在建立表時,先建被關聯的表dep,才能建關聯表emp 先建立被關聯表 相當於先建立被關...