Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Multi-meter Design of MySQL

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

I. Foreign key constraint

Ensure the integrity of the data.

Define foreign key constraints:

You can define the foreign key foreign key current table name (field name) references target table name (primary key of the target table) directly in the create statement, after you have created the statement, you can directly use the modification statement to define the alter table table name add foreign key current table name (field name) references target table name (primary key of the target table)

Second, three kinds of entity relations of multi-table design

Many to many, one to many and one to one

Third, the design of multi-watch-one to many

A class can have more than one student, but a student can only belong to one class. Or a department has multiple employees, and an employee belongs to multiple departments. These are all one-to-many relationships, so how is the design of one-to-many database implemented.

Department table

Create table dept (deptId int primary key auto_increment, dname varchar (32) not null)

Employee table

Create table emp (empId int primary key auto_increment, ename varchar (32) not null, age int, dno int, constraint fk_dno foreign key emp (dno) references dept (deptId))

IV. Design of multiple tables-many-to-many

A student can choose multiple courses, and each course can also be selected by multiple students. This is a typical many-to-many relationship.

Many-to-many table-building principles:

You need to create a third-party table that has at least two fields that point to the primary keys of many-to-many parties as foreign keys.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report