Foreign Key. When the primary key of a two-dimensional table (such as table A) is included in another two-dimensional table (such as table B), table A The primary key in becomes the foreign key of table B.
Foreign keywords represent the connection 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 keywords are also called foreign keys.
Validity of foreign keys
Many times, programmers will find that fields are missing, redundant, or that they cannot add unconstrained fields after creating foreign keys. [This is necessary in special circumstances]. If you don’t want to operate on the table structure at this time, you can use constraint invalidation.
Take Northwind as an example: I want to add a piece of data to the product table [Products table] that is not restricted by the category table [Categories table]. You can invalidate the Categories constraint in the products table.
Writing method:
ALTER TABLE dbo.Products NOCHECK CONSTRAINT FK_Products_Categories
After the addition is completed, make it valid:
ALTER TABLE dbo.Products CHECK
This completes the addition of data that is not constrained by a certain table.
Another benefit is: as in the above example. When modifying the Categories table or adding fields, all foreign keys that reference the Categories table must be invalidated. After adding fields to the Categories table, restore validity to all foreign keys that apply to the Categories table.
For more related knowledge, please visit: PHP Chinese website!
The above is the detailed content of In a database, what maintains the relationship between tables?. For more information, please follow other related articles on the PHP Chinese website!