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

How to establish the relationship between table and table in database

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article is about how to establish a table-to-table relationship in a database. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The database uses "foreign keys" to establish the relationship between tables. Foreign keys can be used to establish the relationship between master table and slave table, to establish a connection between the data of the two tables, and to restrict the consistency and integrity of the data in the two tables.

The database uses "foreign keys" to establish the relationship between tables.

The foreign key constraint (FOREIGN KEY) of a database is a special field of a table and is often used with primary key constraints. For two tables with an associated relationship, the table with the primary key in the associated field is the master table (parent table), and the table with the foreign key is the slave table (child table).

Foreign keys are used to establish the relationship between the master table and the slave table, to establish a connection between the data of the two tables, and to restrict the consistency and integrity of the data in the two tables. For example, a fruit stall, only apples, peaches, plums, watermelons and other four kinds of fruit, then you come to the fruit stall to buy fruit, you can only choose apples, peaches, plums and watermelons, other fruits can not be bought.

When a record is deleted by the master table, the corresponding record in the slave table must be changed accordingly. A table can have one or more foreign keys, which can be null. If not, the value of each foreign key must be equal to a value of the primary key in the primary table.

When defining foreign keys, you need to follow the following rules:

The master table must already exist in the database or the table that is currently being created. In the latter case, the master table and the slave table are the same table, and such a table is called a self-reference table, and this structure is called self-referential integrity.

You must define a primary key for the primary table.

The primary key cannot contain null values, but null values are allowed in foreign keys. That is, as long as each non-null value of the foreign key appears in the specified primary key, the content of the foreign key is correct.

Specify a column name or a combination of column names after the table name of the main table. This column or combination of columns must be the primary key or candidate key of the primary table.

The number of columns in the foreign key must be the same as the number of columns in the primary key of the primary table.

The data type of the column in the foreign key must be the same as the corresponding column in the primary key of the primary table.

Thank you for reading! This is the end of the article on "how to establish the relationship between tables in the database". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!

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

Internet Technology

Wechat

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

12
Report