Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the function of mysql foreign key constraint

Shulou Source: shulou.com Published: 2022-05-31 10:55:01 09月15日 Update

This article focuses on "what is the role of mysql foreign key constraints", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what is the role of mysql foreign key constraints"!

1. The foreign key constraint ensures the referential integrity of one or two tables, and the foreign key is the reference relationship between two fields of a table or two fields of two tables.

2. Ensure the completeness and accuracy of the data between tables through foreign key constraints.

Example

-- Operation of foreign key constraints-- Overview of the keyword foreighn key--: that is, to establish a relationship between two or more tables-- add foreign keys CREATE TABLE tab (id int PRIMARY KEY, name VARCHAR (30), t_id INT) when creating tables -- the primary key data type of the primary table corresponding to the foreign key should be the same CONSTRAINT tab_tab1_id-- foreign key name FOREIGN KEY (t_id)-foreign key column name REFERENCES tab1 (id)-primary table (column name)) -- the second join table CREATE TABLE tab1 (id INT PRIMARY KEY,-- the primary key id is also the foreign key age INT that connects the tab table);-- Delete the foreign key constraint ALTER TABLEtab-- table name DROP FOREIGN KEY tab_tab1_id;-- foreign key name-- add the foreign key constraint ALTER TABLEtab after creating the table-- slave table add CONSTRAINTtab_tab1_id-- foreign key name FOREIGN KEY (t_id)-- foreign key column name REFERENCEStab1 (id) -- main table (column name) at this point, I believe you have a deeper understanding of "what is the role of mysql foreign key constraints". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Two between function content name field integrity data reference learning practical deeper key keyword interest accuracy multiple example practical practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Microsoft macOS Redmi Linux