數據庫表與表之間的一對1、一對多、多對多關係

表1 foreign key 表2
多對一:表 1 的多條記錄對應表 2 的一條記錄sql

利用foreign key的原理咱們能夠製做兩張表的多對多,一對一關係spa


多對多:
    表1的多條記錄能夠對應表2的一條記錄
    表2的多條記錄也能夠對應表1的一條記錄操作系統

一對一:
    表1的一條記錄惟一對應表2的一條記錄設計

 

一、先確立關係code

二、找到多的一方,把關聯字段寫在多的一方blog

 

1、多對一或者一對多rem

                    1.先建被關聯的表,保證被關聯表的字段必須惟一。數學

      2.在建立關聯表,關聯字段必定保證是要有重複的。table

一個書和出版社的一個例子,書要關聯出版社(多個書能夠是一個出版社,一個出版社也能夠有好多書)。class

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

從這裏看出來,書是關聯者,出版社是被關聯者。

書要關聯出版社
被關聯的表
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);

結果:

2、一對一

用戶和管理員(只有管理員才能夠登陸,一個管理員對應一個用戶)

用戶被關聯

先建被關聯的表
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');

結果:

3、多對多

書和做者(咱們能夠再建立一張表,用來存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);

總結:

在一對一中,unique 在關聯表中

相關文章
相關標籤/搜索