IX. Database (multi-table queries foreign keys variants of three relationships)

An outer key (multi-table relation)

1. foreign key (foreign key)

Because there are foreign key constraints, making the two tables to form a three relationships: 

many-to- 
many-to 
-one

2, focusing on understanding the relationship between the two tables to find out if

Step Analysis:
 # 1, left to stand table to find the angle 
whether the left table may correspond to a plurality of records in the right table record, if it does, then a foreign key field of the table on the left and right table field (usually id ) 

# 2, and then the right angle of the table stand look 
whether the right table may correspond to a plurality of recording record left table, if it does, then a foreign key field in the right table a left table field (usually ID) 

# 3. summary: 
# Many-: 
If only step 1 is satisfied, the left table is many-to-right table 
if only step 2 is satisfied, the right table is many-to-left table 

# -many 
if steps 1 and 2 at the same time set up when, then prove that the two tables of a bi-directional many-that-many, need to define a relationship between two tables table to store the relationship between the two special 

# -one: 
if 1 and 2 are not established, and It is a left table corresponds to a unique recording right table, and vice versa. This situation is simple, that is, the left table based on the right table foreign key, foreign key field to the left table uniq

3. The three relationships table

(1 ) publishers of books and 

  many (or many): a Press can publish books. Figure words. 

  Associatively: foreign key

                                                                                 

Create Table Press ( 
    ID int Primary Key AUTO_INCREMENT, 
    name VARCHAR ( 20 is ) 
); 

Create Table Book ( 
    ID int Primary Key AUTO_INCREMENT, 
    name VARCHAR ( 20 is ), 
    press_id int Not null, 
         constraint fk_book_press Foreign Key (press_id) References Press (ID) 
    the Delete Cascade ON 
    ON Update Cascade 
); 

# the first to be inserted into the associated table records 
iNSERT iNTO Press (name) values 
( ' Beijing Publishing industry mines ' ), 
( ' people's music did not sound Press ' ), 
( ' IPR no by Press ') 
; 

# Then further inserted into the recording association table 
INSERT INTO Book (name, press_id) values 
( ' nine positive magic ' , 1 ), 
( ' human resource configurations ' , 2 ), 
( ' 九阴白骨爪' , 2 ) , 
( ' Magic ' , 3 ), 
( ' Dragon ten slap in the face ' , 2 ), 
( ' holy canon ' , 3 ) 
; 

query results: 
MySQL > the SELECT * from Book;
 + ---- + --- + ---------- + --------------
| The above mentioned id | name | press_id | 
+ ---- + ----------------- + ---------- + 
| 1 | Nine Yang magic | 1 | 
| 2 | human resource configurations | 2 | 
|. 3 |九阴白骨爪| 2 | 
|. 4 | Magic |. 3 | 
|. 5 | Dragon ten slap | 2 | 
|. 6 | sunflower Collection |. 3 | 
+ - + ----------------- + ---------- + --- 
rows in the SET (0.00 sec) 

MySQL > the SELECT * from Press;
 + - - + -------------------------------- + 
| the above mentioned id | name | 
+ ---- + --- + ----------------------------- 
| 1 | Beijing industrial mines Press | 
| 2 | people's music did not sound Press | 
| 3 | intellectual property useless Press | 
+ ---- + - + ------------------------------ 
rows in the SET (0.00 sec) 

books and publishing ( Many-to)

 

 

 

(2 relationship) and author of books 

  -many: one author can write more than one book, a book can have multiple authors, two-way-to-many, many-that is. Figure words. 

  Associatively: Foreign Key + a new table

                                                                   

# Create a table author is associated table, book table in the relationship before talking about many-to-have created 
the Create the Table author ( 
    the above mentioned id int Primary Key AUTO_INCREMENT, 
    name VARCHAR ( 20 ) 
); 
# this table to store the table and book table author the relationship, that relationship between the two search queries this table on it 
the Create the table author2book ( 
    the above mentioned id int not null UNIQUE AUTO_INCREMENT, 
    author_id int not null, 
    book_id int not null, 
    constraint a fk_author Foreign Key (author_id) the References author (the above mentioned id) 
    oN the Delete Cascade 
    ON Update Cascade, 
    constraint A fk_book Foreign Key (book_id) the References Book (the above mentioned id) 
    ON the Delete Cascade 
    ON Update Cascade,
    Key Primary (the author_id, book_id) 
); 
# insertion of four, id turn arranged 
INSERT INTO author (name) values ( ' Egon ' ), ( ' Alex ' ), ( ' wusir ' ), ( ' yuanhao ' ); 

# each author's masterpiece 
egon: nine Yang magic, human resource configurations,九阴白骨爪, magic, Dragon ten slap holy canon 
alex: nine Yang magic, holy canon 
wusir: magic, Dragon ten slap, sunflower canon 
yuanhao: nine yangshengong 

# inserted in the corresponding data table author2book 

iNSERT INTO author2book (the author_id, book_id) values 
( 1,1 ), 
( 1,2 ), 
( l, 3 ), 
( l, 4), 
( l, 5 
| 5 | 1 | 5 |), 
( 1,6 ), 
( 2,1 ), 
( 2,6 ), 
( 3,4 ), 
( 3,5 ), 
( 3,6 ), 
( 4,1 ) 
; 
# now can check author2book the corresponding relationship between the author and the book 
MySQL> the SELECT * from author2book;
 + ---- + ----------- + --------- + 
| the above mentioned id | author_id | book_id | 
---- + ----------- + --------- + + 
| 1 | 1 | 1 | 
| 2 | 1 | 2 | 
| 3 | 1 | 3 | 
| . 4 |. 1 |. 4 | 
|. 7 | 2 |. 1 |
|. 6 |. 1 |. 6 | 
|. 8 | 2 |. 6 | 
|. 9 |. 3 |. 4 | 
| 10 |. 3 |. 5 | 
|. 11 |. 3 |. 6 | 
| 12 is |. 4 |. 1 | 
+ ---- + - + --------- + --------- 
rows in the SET (0.00 sec) 

authors and books relationship (many to many)

 

 

 

(3 ) users and blog 

  -one: a user can only sign up for a blog that one to one relationship. Figure words 

  related way: 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;

用户和博客(一对一)

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/Sup-to/p/11243363.html