Home Common Problem What is foreign key

What is foreign key

Nov 26, 2020 pm 02:35 PM
foreign key

Foreign keys are keywords. If a common key is the primary key in a relationship, then the common key is called the foreign key of another relationship. Foreign keys represent the relevant link between two relationships. Foreign keys can maintain data consistency and integrity, and their main purpose is to control the data stored in the foreign key table.

What is foreign key

Foreign key introduction:

If the public key is the primary key in a relationship, then the public key is is called a foreign key to another relationship. It can be seen that foreign keys represent the relevant links between two relationships. The table with the foreign key of another relationship as the primary key is called the master table, and the table with the foreign key is called the slave table of the master table. Foreign keys are also called foreign keywords.

(Learning video sharing: mysql video tutorial)

Function:

Maintain data consistency and integrity, the main purpose is to control the storage of foreign keys data in the table. To associate two tables, the foreign key can only refer to the values ​​of columns in the table or use null values.

Prevent execution

  • Insert a new row from the table, and the foreign key value is not the primary key value of the main table to prevent the insertion;

  • Modify the foreign key value from the slave table. If the new value is not the primary key value of the master table, the modification will be blocked;

  • Delete the row from the master table, and its primary key value will exist in the slave table. Prevent deletion (if you want to delete, you must first delete the relevant rows from the slave table);

  • #The master table modifies the primary key value. If the old value exists in the slave table, it will prevent the modification (if you want to modify it) , the relevant rows from the table must first be deleted).

Cascade execution

  • Delete rows from the main table and delete related rows from the table together;

  • Modify the primary key value of the main table, and modify the foreign key value of the related rows of the slave table together. Two methods are provided for users to choose. No matter which method is chosen, there will be no extra rows from the table. From another perspective, the same thing is achieved by rejecting the inconsistency between the flags in the slave table and the master table to achieve consistency with the flags in the master table.

Related recommendations: mysql tutorial

The above is the detailed content of What is foreign key. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to automatically associate MySQL foreign keys and primary keys? How to automatically associate MySQL foreign keys and primary keys? Mar 15, 2024 pm 12:54 PM

How to automatically associate MySQL foreign keys and primary keys? In the MySQL database, foreign keys and primary keys are very important concepts. They can help us establish relationships between different tables and ensure the integrity and consistency of the data. In actual application processes, it is often necessary to automatically associate foreign keys to the corresponding primary keys to avoid data inconsistencies. The following will introduce how to implement this function through specific code examples. First, we need to create two tables, one as the master table and the other as the slave table. Create in main table

How to use foreign keys in MySQL to maintain data integrity? How to use foreign keys in MySQL to maintain data integrity? Aug 02, 2023 am 08:09 AM

How to use foreign keys in MySQL to maintain data integrity? Introduction: In a database, data integrity is very important. By using foreign keys, we can ensure data consistency and integrity between related tables in the database. This article will introduce you to how to use foreign keys to maintain data integrity in MySQL. Create tables and associations: First, we need to create two tables and associate them. Suppose we have two tables, one is the "orders" table and the other is the "customer" table

How to use MySQL's foreign keys and constraints to improve data integrity and consistency? How to use MySQL's foreign keys and constraints to improve data integrity and consistency? Sep 08, 2023 pm 01:21 PM

How to use MySQL's foreign keys and constraints to improve data integrity and consistency? In the MySQL database, foreign keys and constraints are two important concepts that can help improve data integrity and consistency. In this article, we will discuss in detail how to use MySQL's foreign keys and constraints to achieve this goal, and provide some code examples. 1. The concept and function of foreign keys Foreign keys are a mechanism used to establish relationships between tables, which can ensure the consistency of data between related tables. Foreign keys usually consist of a table's primary key (or unique

What does mysql foreign key constraint mean? What does mysql foreign key constraint mean? Jul 10, 2023 am 09:56 AM

MySQL foreign key constraints refer to a constraint that limits the relationship between tables. You can define a column in one table, and this column will reference a column in another table. This association can ensure the integrity of the data. Completeness and consistency.

The importance and practical significance of foreign keys in MySQL database The importance and practical significance of foreign keys in MySQL database Mar 15, 2024 pm 03:06 PM

The importance and practical significance of foreign keys in the MySQL database. In the MySQL database, the foreign key (ForeignKey) is an important constraint used to establish relationships between different tables. Foreign key constraints ensure data consistency and integrity between tables, and can effectively avoid incorrect data insertion, update, or deletion operations. 1. The importance of foreign keys: Data integrity: Foreign key constraints can ensure that data in one table refers to data that exists in another table, avoiding errors caused by foreign keys erroneously referencing or referencing non-existent data.

Does mysql have foreign keys? Does mysql have foreign keys? Jun 13, 2023 pm 01:29 PM

MySQL has foreign keys, which are mainly used to establish the relationship between the master table and the slave table. It can establish a connection for the data in the two tables and constrain the consistency and integrity of the data in the two tables. When a record is deleted from the main table, the corresponding record from the slave table must also be changed accordingly. A table can have one or more foreign keys. The foreign key can be a null value. If it is not a null value, the value of each foreign key must be equal to a certain value of the primary key in the main table, and the number and corresponding data type of the columns in the foreign key must be the same as those in the primary key of the main table. same.

What is mysql foreign key What is mysql foreign key Apr 11, 2023 am 10:45 AM

MySQL foreign key is a very useful data processing function that can quickly update data between tables; simply put, it can establish an association between two tables, so that when operating one table, The data in another table will also change simultaneously.

Database Primary and Foreign Key Design: Best Practices in PHP Programming Database Primary and Foreign Key Design: Best Practices in PHP Programming Jun 22, 2023 am 08:46 AM

In PHP programming, database design is a crucial part. When designing a database, the correct use of primary keys and foreign keys can greatly improve the performance and reliability of the database, thereby promoting the development of the entire website or application. This article will introduce the concepts, functions and best practices of database primary keys and foreign keys, making you more comfortable when writing PHP programs. 1. The role and implementation of the primary key The primary key is a column or a group of columns in a database table, and their values ​​can uniquely identify each row of data in the table. The functions of the primary key mainly include the following aspects: