09-Mysql database — variants of foreign keys

This section focuses on:

  • How to find out the relationship between two tables
  • Three relations of tables

 

1. Introduction

Because of the constraint of foreign key, the two tables form three relationships:

  • Multi to one
  • Multiple to many
  • One-on-one

Two, focus on understanding if you find out the relationship between the two tables.

Copy code ></span></div>
<pre>Analysis steps:#1, look at the left table first.Whether multiple records in the left table correspond to a record in the right table, and if so, prove that a field in the left table, foreign key, and a field (usually id) in the right table#2, look at the right table again.Whether or not the right tableMultiple records can correspond to a record in the left table, and if so, a field in the right table, foreign key, left table, field (usually id)#3, summary:Many to one:If only step 1 is established, then the left table is multiple to one right.If only step 2 is establishedThe right table is many to one left.Many to manyIf steps 1 and 2 hold together, it is proved that a two-way many-to-one, or many-to-many, relationship table between the two tables needs to be defined to store the relationship between them.One to one:If 1 and 2 are not valid, they are just one of the left ones.Record only one record corresponding to the right table, and vice versa. This is very simple, that is, on the basis of the left table foreign key right table, set the foreign key field of the left table to unique</pre>
<div class=Copy code ></span></div>
</div>
<p> </p>
<h2>Three, three relationships of tables</h2>
<p>(1)Books and publishers</p>
<p>  One to many (or many to one): a publishing house can publish many books. Look at the picture and talk.</p>
<p>  <code>Association mode: foreign key</code></p>
<p> <img src=

 

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
);

# First, insert the record into the associated table.Insert into press (name) values("Beijing industrial landmine press")(people's music is not good at publishing house).(intellectual property rights no use Publishing House);Go to the association table again.insert recordInsert into book (name, press_id) values(Joyoung magic, 1)("Jiu Yin Zhen Jing", 2)('nine Yin white bone claw ', 2).("Gu Gu Jiu Jian", 3)("dragon ten slap")', 2),("sunflower treasure Dian", 3);Query results:Mysql> select * from book;+ + + + - -- + - + +ID nAme press_id+ + + + - -- + - + +1, Joyoung, 1.2The 2 channels of the nine Yin classics3, 2 Yin and white bone claw.4, only 3.5Ten palm, 2.6, sunflower treasure Dian 3+ + + + - -- + - + +6 rows inSet (0 sec)Mysql> select * from press;+ + - -- + - -- +ID nameIt+ + - -- + - -- +(1) Beijing industrial landmine press.It2. People's music is not good.3. Intellectual property rights are useless.+ + - -- + - -- +ThreeRows in set (0 sec)

Books and publishers (many to one)

 

(2)The relationship between authors and books

  Many-to-many: A writer can write more than one book, a book can have more than one author, two-way one-to-many, that is, many-to-many.Look at the picture and talk.

  Association mode: foreign key+, a new table.

 

 

# The author table of the associated table is created, and the previous book table has been created in the relationship of many to one.Create table author (Id int primary key auto_increment,NaMe varchar (20));This table keeps the relationship between the author table and the book table, that is to say, check the relationship between the two party to check the table.Create table author2book (Id int notNull 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 cascadeOn update cascade,Constraint fk_book foreign key (book_id) references book (ID)On delete cascadeOn update CASCade,Primary key (author_id, book_id));Insert four authors, ID in turn.Insert into author (name) values ('egon'),'alex'), ('wusir'), ('yuanhao');The representative works of each writer.Egon: Joyoung Shen Gong, nine Yin Jing Jing, nine Yin bone claw, Gu Gu Jiu Jian, ten dragon palm, sunflower treasure.Alex: Joyoung magic, sunflower collectionWusIr: Gu Gu Jiu Jian, Jiang long ten palm, sunflower treasureYuanhao: Joyoung magicInsert the corresponding data in the author2book table.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);You can do it now.Check author2book's correspondence with author and book.Mysql> select * from author2book;+ + - -- + - -- + - -- +ID autHor_id book_id+ + - -- + - -- + - -- +1, 1, 1,2, 1,23, 1, 3,4, 1, 4,5, 1, 5,61, 6,7, 2, 1,8, 2, 6,9, 3,410, 3, 5,11, 3, 6,12, 4, 1,+ - --+ + - -- + - - +12 rows in set (0 sec)

The relationship between authors and books (many to many)

 

 

(3)Users and blogs

  One to one: a user can only register one blog, that is, one to one relationship. look and say

  Association mode: foreign key+unique

 

 

 

#For example, a user can only register a blog.Two tables: user tables (user) and blog tables (blog).Create user tablesCreate table user (Id int primary key auto_increment,Name varchar (20));Create a blog tableCreate 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 cascadeOn update cascade);Insert records in user tablesInsert into user (name) values('alex'),('wusir'),('egon'),('xiaoma');Insert a blog entry record.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);Search for wusir's blog addressSelect URL from blog where user_id=2;

Users and bloggers (one to one)

 

Leave a Reply

Your email address will not be published. Required fields are marked *