1. 程式人生 > >六、資料庫之 表與表之間的關係

六、資料庫之 表與表之間的關係

 

表1 foreign key 表2
則表1的多條記錄對應表2的一條記錄,即多對一

利用foreign key的原理我們可以製作兩張表的多對多,一對一關係
多對多:
    表1的多條記錄可以對應表2的一條記錄
    表2的多條記錄也可以對應表1的一條記錄

一對一:
    表1的一條記錄唯一對應表2的一條記錄,反之亦然

分析時,我們先從按照上面的基本原理去套,然後再翻譯成真實的意義,就很好理解了

1、先確立關係

2、找到多的一方,吧關聯欄位寫在多的一方

一、多對一或者一對多(左邊表的多條記錄對應右邊表的唯一一條記錄)

需要注意的:1.先建被關聯的表,保證被關聯表的欄位必須唯一。

      2.在建立關聯表,關聯欄位一定保證是要有重複的。

其實上一篇部落格已經舉了一個多對一關係的小例子了,那我們在用另一個小例子來回顧一下。

這是一個書和出版社的一個例子,書要關聯出版社(多個書可以是一個出版社,一個出版社也可以有好多書)。

誰關聯誰就是誰要按照誰的標準。

書要關聯出版社
被關聯的表
create  table press(
id int primary key auto_increment,
name char(20)
);
關聯的表
create table book(
book_id int primary key auto_increment,
book_name varchar(
20), book_price int, press_id int, constraint Fk_pressid_id foreign key(press_id) references press(id) on delete cascade on update cascade ); 插記錄 insert into press(name) values('新華出版社'), ('海燕出版社'), ('擺渡出版社'), (
'大眾出版社'); insert into book(book_name,book_price,press_id) values('Python爬蟲',100,1), ('Linux',80,1), ('作業系統',70,2), ('數學',50,2), ('英語',103,3), ('網頁設計',22,3);

執行結果截圖:

二、一對一

例子一:使用者和管理員(只有管理員才可以登入,一個管理員對應一個使用者)

管理員關聯使用者

 

===========例子一:使用者表和管理員表=========
先建被關聯的表
create table user(
id int primary key auto_increment, #主鍵自增
name char(10)
);
在建關聯表
create table admin(
id int primary key auto_increment,
user_id int unique,
password varchar(16),
foreign key(user_id) references user(id)
on delete cascade
on update cascade
);
insert into user(name) values('susan1'),
                             ('susan2'),
                             ('susan3'),
                             ('susan4'),
                             ('susan5'),
                             ('susan6');
insert into admin(user_id,password) values(4,'sds156'),
                                          (2,'531561'),
                                          (6,'f3swe');

 

執行結果截圖:

例子二:學生表和客戶表

 

========例子二:學生表和客戶表=========
create table customer(
id int primary key auto_increment,
name varchar(10),
qq int unique,
phone int unique
);
create table student1(
sid int primary key auto_increment,
course char(20),
class_time time,
cid int unique,
foreign key(cid) references customer(id)
on delete cascade
on update cascade
);
insert into customer(name,qq,phone) values('小小',13564521,11111111),
                                          ('嘻哈',14758254,22222222),
                                          ('王維',44545522,33333333),
                                          ('胡軍',545875212,4444444),
                                          ('李希',145578543,5555555),
                                          ('李迪',754254653,8888888),
                                          ('艾哈',74545145,8712547),
                                          ('嘖嘖',11147752,7777777);
insert into student1(course,class_time,cid) values('python','08:30:00',3),
                                                 ('python','08:30:00',4),
                                                 ('linux','08:30:00',1),
                                                 ('linux','08:30:00',7);

 

執行結果截圖:

三、多對多(多條記錄對應多條記錄)

書和作者(我們可以再建立一張表,用來存book和author兩張表的關係)

要把book_id和author_id設定成聯合唯一

聯合唯一:unique(book_id,author_id)

聯合主鍵:alter table t1 add primary  key(id,avg)

多對多:一個作者可以寫多本書,一本書也可以有多個作者,雙向的一對多,即多對多
  
關聯方式:foreign key+一張新的表

========書和作者,另外在建一張表來存書和作者的關係
#被關聯的
create table book1(
id int primary key auto_increment,
name varchar(10),
price float(3,2)
);
#========被關聯的
create table author(
id int primary key auto_increment,
name char(5)
);
#========關聯的
create table author2book(
id int primary key auto_increment,
book_id int not null,
author_id int not null,
unique(book_id,author_id),
foreign key(book_id) references book1(id)
on delete cascade
on update cascade,
foreign key(author_id) references author(id)
on delete cascade
on update cascade
);
#========插入記錄
insert into book1(name,price) values('九陽神功',9.9),
                                    ('葵花寶典',9.5),
                                    ('辟邪劍譜',5),
                                    ('降龍十巴掌',7.3);
insert into author(name) values('egon'),('e1'),('e2'),('e3'),('e4');
insert into author2book(book_id,author_id) values(1,1),
                                                 (1,4),
                                                 (2,1),
                                                 (2,5),
                                                 (3,2),
                                                 (3,3),
                                                 (3,4),
                                                 (4,5);

 多對多關係舉例

使用者表,使用者組,主機表

-- 使用者組
create table user (
id int primary key auto_increment,
username varchar(20) not null,
password varchar(50) not null
);
insert into user(username,password) values('egon','123'),
                                          ('root',147),
                                          ('alex',123),
                                          ('haiyan',123),
                                          ('yan',123);

-- 使用者組表
create table usergroup(
id int primary key auto_increment,
groupname varchar(20)  not null unique
);
insert into usergroup(groupname) values('IT'),
                                        ('Sale'),
                                        ('Finance'),
                                        ('boss');

-- 建立user和usergroup的關係表
 
   
create table user2usergroup(
id int not NULL UNIQUE auto_increment,
user_id int not null,
group_id int not NULL,
PRIMARY KEY(user_id,group_id),
foreign key(user_id) references user(id)
ON DELETE CASCADE
on UPDATE CASCADE ,
foreign key(group_id) references usergroup(id)
ON DELETE CASCADE
on UPDATE CASCADE
);
 
   
insert into user2usergroup(user_id,group_id) values(1,1),
(1,2),
(1,3),
(1,4),
(2,3),
(2,4),
(3,4);
 
 
   
  

 

-- 主機表
CREATE TABLE host(
id int primary key auto_increment,
ip CHAR(15) not NULL UNIQUE DEFAULT '127.0.0.1'
);
insert into host(ip) values('172.16.45.2'),
                             ('172.16.31.10'),
                             ('172.16.45.3'),
                             ('172.16.31.11'),
                             ('172.10.45.3'),
                             ('172.10.45.4'),
                             ('172.10.45.5'),
                             ('192.168.1.20'),
                             ('192.168.1.21'),
                             ('192.168.1.22'),
                             ('192.168.2.23'),
                             ('192.168.2.223'),
                             ('192.168.2.24'),
                             ('192.168.3.22'),
                             ('192.168.3.23'),
                             ('192.168.3.24');

-- 業務線表
create table business(
id int primary key auto_increment,
business varchar(20) not null unique
);
insert into business(business) values
                                        ('輕鬆貸'),
                                        ('隨便花'),
                                        ('大富翁'),
                                        ('窮一生');

-- 建立host和business關係表
CREATE TABLE host2business(
id int not null unique auto_increment,
host_id int not null ,
business_id int not NULL ,
PRIMARY KEY(host_id,business_id),
foreign key(host_id) references host(id),
FOREIGN KEY(business_id) REFERENCES business(id)
);


insert into host2business(host_id,business_id) values
                                                        (1,1),
                                                        (1,2),
                                                        (1,3),
                                                        (2,2),
                                                        (2,3),
                                                        (3,4);

 

-- 建立user和host的關係
create table user2host(
id int not null unique auto_increment,
user_id int not null,
host_id int not null,
primary key(user_id,host_id),
foreign key(user_id) references user(id),
foreign key(host_id) references host(id)
);

insert into user2host(user_id,host_id) values(1,1),
                                                (1,2),
                                                (1,3),
                                                (1,4),
                                                (1,5),
                                                (1,6),
                                                (1,7),
                                                (1,8),
                                                (1,9),
                                                (1,10),
                                                (1,11),
                                                (1,12),
                                                (1,13),
                                                (1,14),
                                                (1,15),
                                                (1,16),
                                                (2,2),
                                                (2,3),
                                                (2,4),
                                                (2,5),
                                                (3,10),
                                                (3,11),
                                                (3,12);

練習

 

     
表1 foreign key 表2
則表1的多條記錄對應表2的一條記錄,即多對一

利用foreign key的原理我們可以製作兩張表的多對多,一對一關係
多對多:
    表1的多條記錄可以對應表2的一條記錄
    表2的多條記錄也可以對應表1的一條記錄

一對一:
    表1的一條記錄唯一對應表2的一條記錄,反之亦然

分析時,我們先從按照上面的基本原理去套,然後再翻譯成真實的意義,就很好理解了

1、先確立關係

2、找到多的一方,吧關聯欄位寫在多的一方

一、多對一或者一對多(左邊表的多條記錄對應右邊表的唯一一條記錄)

需要注意的:1.先建被關聯的表,保證被關聯表的欄位必須唯一。

      2.在建立關聯表,關聯欄位一定保證是要有重複的。

其實上一篇部落格已經舉了一個多對一關係的小例子了,那我們在用另一個小例子來回顧一下。

這是一個書和出版社的一個例子,書要關聯出版社(多個書可以是一個出版社,一個出版社也可以有好多書)。

誰關聯誰就是誰要按照誰的標準。

書要關聯出版社
被關聯的表
create  table press(
id int primary key auto_increment,
name char(20)
);
關聯的表
create table book(
book_id int primary key auto_increment,
book_name varchar(20),
book_price int,
press_id int,
constraint Fk_pressid_id foreign key(press_id) references press(id)
on delete cascade
on update cascade
);

插記錄
insert into press(name) values('新華出版社'),
                              ('海燕出版社'),
                              ('擺渡出版社'),
                              ('大眾出版社');
insert into book(book_name,book_price,press_id) values('Python爬蟲',100,1),
                                                       ('Linux',80,1),
                                                       ('作業系統',70,2),
                                                       ('數學',50,2),
                                                       ('英語',103,3),
                                                       ('網頁設計',22,3);

執行結果截圖:

二、一對一

例子一:使用者和管理員(只有管理員才可以登入,一個管理員對應一個使用者)

管理員關聯使用者

 

===========例子一:使用者表和管理員表=========
先建被關聯的表
create table user(
id int primary key auto_increment, #主鍵自增
name char(10)
);
在建關聯表
create table admin(
id int primary key auto_increment,
user_id int unique,
password varchar(16),
foreign key(user_id) references user(id)
on delete cascade
on update cascade
);
insert into user(name) values('susan1'),
                             ('susan2'),
                             ('susan3'),
                             ('susan4'),
                             ('susan5'),
                             ('susan6');
insert into admin(user_id,password) values(4,'sds156'),
                                          (2,'531561'),
                                          (6,'f3swe');

 

執行結果截圖:

例子二:學生表和客戶表

 

========例子二:學生表和客戶表=========
create table customer(
id int primary key auto_increment,
name varchar(10),
qq int unique,
phone int unique
);
create table student1(
sid int primary key auto_increment,
course char(20),
class_time time,
cid int unique,
foreign key(cid) references customer(id)
on delete cascade
on update cascade
);
insert into customer(name,qq,phone) values('小小',13564521,11111111),
                                          ('嘻哈',14758254,22222222),
                                          ('王維',44545522,33333333),
                                          ('胡軍',545875212,4444444),
                                          ('李希',145578543,5555555),
                                          ('李迪',754254653,8888888),
                                          ('艾哈',74545145,8712547),
                                          ('嘖嘖',11147752,7777777);
insert into student1(course,class_time,cid) values('python','08:30:00',3),
                                                 ('python','08:30:00',4),
                                                 ('linux','08:30:00',1),
                                                 ('linux','08:30:00',7);

 

執行結果截圖:

三、多對多(多條記錄對應多條記錄)

書和作者(我們可以再建立一張表,用來存book和author兩張表的關係)

要把book_id和author_id設定成聯合唯一

聯合唯一:unique(book_id,author_id)

聯合主鍵:alter table t1 add primary  key(id,avg)

多對多:一個作者可以寫多本書,一本書也可以有多個作者,雙向的一對多,即多對多
  
關聯方式:foreign key+一張新的表

========書和作者,另外在建一張表來存書和作者的關係
#被關聯的
create table book1(
id int primary key auto_increment,
name varchar(10),
price float(3,2)
);
#========被關聯的
create table author(
id int primary key auto_increment,
name char(5)
);
#========關聯的
create table author2book(
id int primary key auto_increment,
book_id int not null,
author_id int not null,
unique(book_id,author_id),
foreign key(book_id) references book1(id)
on delete cascade
on update cascade,
foreign key(author_id) references author(id)
on delete cascade
on update cascade
);
#========插入記錄
insert into book1(name,price) values('九陽神功',9.9),
                                    ('葵花寶典',9.5),
                                    ('辟邪劍譜',5),
                                    ('降龍十巴掌',7.3);
insert into author(name) values('egon'),('e1'),('e2'),('e3'),('e4');
insert into author2book(book_id,author_id) values(1,1),
                                                 (1,4),
                                                 (2,1),
                                                 (2,5),
                                                 (3,2),
                                                 (3,3),
                                                 (3,4),
                                                 (4,5);

 多對多關係舉例

使用者表,使用者組,主機表

-- 使用者組
create table user (
id int primary key auto_increment,
username varchar(20) not null,
password varchar(50) not null
);
insert into user(username,password) values('egon','123'),
                                          ('root',147),
                                          ('alex',123),
                                          ('haiyan',123),
                                          ('yan',123);

-- 使用者組表
create table usergroup(
id int primary key auto_increment,
groupname varchar(20)  not null unique
);
insert into usergroup(groupname) values('IT'),
                                        ('Sale'),
                                        ('Finance'),
                                        ('boss');

-- 建立user和usergroup的關係表
 
 
create table user2usergroup(
id int not NULL UNIQUE auto_increment,
user_id int not null,
group_id int not NULL,
PRIMARY KEY(user_id,group_id),
foreign key(user_id) references user(id)
ON DELETE CASCADE
on UPDATE CASCADE ,
foreign key(group_id) references usergroup(id)
ON DELETE CASCADE
on UPDATE CASCADE
);
 
 
insert into user2usergroup(user_id,group_id) values(1,1),
(1,2),
(1,3),
(1,4),
(2,3),
(2,4),
(3,4);
 
 
 

 

-- 主機表
CREATE TABLE host(
id int primary key auto_increment,
ip CHAR(15) not NULL UNIQUE DEFAULT '127.0.0.1'
);
insert into host(ip) values('172.16.45.2'),
                             ('172.16.31.10'),
                             ('172.16.45.3'),
                             ('172.16.31.11'),
                             ('172.10.45.3'),
                             ('172.10.45.4'),
                             ('172.10.45.5'),
                             ('192.168.1.20'),
                             ('192.168.1.21'),
                             ('192.168.1.22'),
                             ('192.168.2.23'),
                             ('192.168.2.223'),
                             ('192.168.2.24'),
                             ('192.168.3.22'),
                             ('192.168.3.23'),
                             ('192.168.3.24');

-- 業務線表
create table business(
id int primary key auto_increment,
business varchar(20) not null unique
);
insert into business(business) values
                                        ('輕鬆貸'),
                                        ('隨便花'),
                                        ('大富翁'),
                                        ('窮一生');

-- 建立host和business關係表
CREATE TABLE host2business(
id int not null unique auto_increment,
host_id int not null ,
business_id int not NULL ,
PRIMARY KEY(host_id,business_id),
foreign key(host_id) references host(id),
FOREIGN KEY(business_id) REFERENCES business(id)
);


insert into host2business(host_id,business_id) values
                                                        (1,1),
                                                        (1,2),
                                                        (1,3),
                                                        (2,2),
                                                        (2,3),
                                                        (3,4);

 

-- 建立user和host的關係
create table user2host(
id int not null unique auto_increment,
user_id int not null,
host_id int not null,
primary key(user_id,host_id),
foreign key(user_id) references user(id),
foreign key(host_id) references host(id)
);

insert into user2host(user_id,host_id) values(1,1),
                                                (1,2),
                                                (1,3),
                                                (1,4),
                                                (1,5),
                                                (1,6),
                                                (1,7),
                                                (1,8),
                                                (1,9),
                                                (1,10),
                                                (1,11),
                                                (1,12),
                                                (1,13),
                                                (1,14),
                                                (1,15),
                                                (1,16),
                                                (2,2),
                                                (2,3),
                                                (2,4),
                                                (2,5),
                                                (3,10),
                                                (3,11),
                                                (3,12);

練習