標籤:arch har 建立使用者 auto 關聯表 一對一 value 雙向 log
本節重點:
一、介紹
因為有foreign key的約束,使得兩張表形成了三種了關係:
二、重點理解如果找出兩張表之間的關係
分析步驟:#1、先站在左表的角度去找是否左表的多條記錄可以對應右表的一條記錄,如果是,則證明左表的一個欄位foreign key 右表一個欄位(通常是id)#2、再站在右表的角度去找是否右表的多條記錄可以對應左表的一條記錄,如果是,則證明右表的一個欄位foreign key 左表一個欄位(通常是id)#3、總結:#多對一:如果只有步驟1成立,則是左表多對一右表如果只有步驟2成立,則是右表多對一左表#多對多如果步驟1和2同時成立,則證明這兩張表時一個雙向的多對一,即多對多,需要定義一個這兩張表的關係表來專門存放二者的關係#一對一:如果1和2都不成立,而是左表的一條記錄唯一對應右表的一條記錄,反之亦然。這種情況很簡單,就是在左表foreign key右表的基礎上,將左表的外鍵欄位設定成unique即可
三、表的三種關係
(1)書和出版社
一對多(或多對一):一個出版社可以出版多本書。看圖說話。
關聯方式:foreign key
create table press( id int primary key auto_increment, name varchar(20));create table book( id int primary key auto_increment, name varchar(20), press_id int not null, constraint fk_book_press foreign key(press_id) references press(id) on delete cascade on update cascade);# 先往被關聯表中插入記錄insert into press(name) values(‘北京工業地雷出版社‘),(‘人民音樂不好聽出版社‘),(‘智慧財產權沒有用出版社‘);# 再往關聯表中插入記錄insert into book(name,press_id) values(‘九陽神功‘,1),(‘九陰真經‘,2),(‘九陰白骨爪‘,2),(‘獨孤九劍‘,3),(‘降龍十巴掌‘,2),(‘葵花寶典‘,3);查詢結果:mysql> select * from book;+----+-----------------+----------+| id | name | press_id |+----+-----------------+----------+| 1 | 九陽神功 | 1 || 2 | 九陰真經 | 2 || 3 | 九陰白骨爪 | 2 || 4 | 獨孤九劍 | 3 || 5 | 降龍十巴掌 | 2 || 6 | 葵花寶典 | 3 |+----+-----------------+----------+6 rows in set (0.00 sec)mysql> select * from press;+----+--------------------------------+| id | name |+----+--------------------------------+| 1 | 北京工業地雷出版社 || 2 | 人民音樂不好聽出版社 || 3 | 智慧財產權沒有用出版社 |+----+--------------------------------+3 rows in set (0.00 sec)
書和出版社(多對一)
(2)作者和書籍的關係
多對多:一個作者可以寫多本書,一本書也可以有多個作者,雙向的一對多,即多對多。看圖說話。
關聯方式:foreign key+一張新的表
# 建立被關聯表author表,之前的book表在講多對一的關係已建立create table author( id int primary key auto_increment, name varchar(20));#這張表就存放了author表和book表的關係,即查詢二者的關係查這表就可以了create table author2book( id int not null unique auto_increment, author_id int not null, book_id int not null, constraint fk_author foreign key(author_id) references author(id) on delete cascade on update cascade, constraint fk_book foreign key(book_id) references book(id) on delete cascade on update cascade, primary key(author_id,book_id));#插入四個作者,id依次排開insert into author(name) values(‘egon‘),(‘alex‘),(‘wusir‘),(‘yuanhao‘);# 每個作者的代表作egon: 九陽神功、九陰真經、九陰白骨爪、獨孤九劍、降龍十巴掌、葵花寶典alex: 九陽神功、葵花寶典wusir:獨孤九劍、降龍十巴掌、葵花寶典yuanhao:九陽神功# 在author2book表中插入相應的資料insert into author2book(author_id,book_id) values(1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(2,1),(2,6),(3,4),(3,5),(3,6),(4,1);# 現在就可以查author2book對應的作者和書的關係了mysql> select * from author2book;+----+-----------+---------+| id | author_id | book_id |+----+-----------+---------+| 1 | 1 | 1 || 2 | 1 | 2 || 3 | 1 | 3 || 4 | 1 | 4 || 5 | 1 | 5 || 6 | 1 | 6 || 7 | 2 | 1 || 8 | 2 | 6 || 9 | 3 | 4 || 10 | 3 | 5 || 11 | 3 | 6 || 12 | 4 | 1 |+----+-----------+---------+12 rows in set (0.00 sec)
作者與書籍關係(多對多)
(3)使用者和部落格
一對一:一個使用者只能註冊一個部落格,即一對一的關係。看圖說話
關聯方式:foreign key+unique
#例如: 一個使用者只能註冊一個部落格#兩張表: 使用者表 (user)和 部落格表(blog)# 建立使用者表create table user( id int primary key auto_increment, name varchar(20));# 建立部落格表create table blog( id int primary key auto_increment, url varchar(100), user_id int unique, constraint fk_user foreign key(user_id) references user(id) on delete cascade on update cascade);#插入使用者表中的記錄insert into user(name) values(‘alex‘),(‘wusir‘),(‘egon‘),(‘xiaoma‘);# 插入部落格表的記錄insert into blog(url,user_id) values(‘http://www.cnblog/alex‘,1),(‘http://www.cnblog/wusir‘,2),(‘http://www.cnblog/egon‘,3),(‘http://www.cnblog/xiaoma‘,4);# 查詢wusir的部落格地址select url from blog where user_id=2;
使用者和部落格(一對一)
09-Mysql資料庫----外鍵的變種