Database Union foreign Key master key (to resolve many-to-many relationships)

Source: Internet
Author: User

Many-to-many concepts: A student can learn several courses, a course can be several students study, stand in the perspective of a student to correspond to a number of courses, standing in the course there, corresponding to a number of students, so students and courses is a simple many-to-many relationship.

In the database we want to solve this many-to-many relationship application, first create a student table, such as

CREATE TABLE Stu (
stu_id int PRIMARY KEY,

Sty_name varchar (20)

);

And then create a curriculum
CREATE TABLE book (

book_id int PRIMARY KEY,

Book_name varchar (20)

);

Finally, we create an application table that sets the primary key of the student table and the main key of the curriculum to the Federated primary key of the application table.
CREATE TABLE Yingyong (
stu_id int primary KEY (stu_id,book_id),

book_id int

);

When we need to save "Zhang three learn Chinese" this data is just the Zhang San ID and the language of the ID can be saved, take the same time. This solves the many-to-many relational data in the database.
By the way: The table with the foreign key is the child table, and the parent table is its own primary key that is referenced by any child tables.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.