In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the use of MySQL foreign keys, has a certain reference value, friends in need can refer to. I hope you will learn a lot after reading this article. Next, let the editor take you to learn about it.
The method of using MySQL foreign key: 1, two tables must be InnoDB table type; 2, the field used in foreign key relationship must be indexed Index;3, and the field used in foreign key relationship must be similar to data type.
Methods for using MySQL foreign keys:
1. Only tables of type InnoDB can use foreign keys. Mysql defaults to MyISAM. This type does not support foreign key constraints.
2, the benefits of foreign keys: can make two tables associated, ensure data consistency and achieve some cascading operations.
3. The role of foreign keys:
The main purpose of maintaining data consistency and integrity is to control the data stored in the foreign key table. Associate the two tables, and the foreign key can only refer to the values of the columns in the table.
4. The premise of establishing foreign keys:
Both tables must be of the InnoDB table type.
Fields used in foreign key relationships must be indexed (Index).
Fields used in foreign key relationships must be similar to data types.
5. Steps to create
Specify the primary key keyword: foreign key (column name).
Reference foreign key keyword: references (foreign key column name).
6. Event trigger limit: on delete and on update. You can set the parameter cascade (follow the foreign key change).
Restrict (restrict foreign key changes in appearance), set
Null (set null), set Default (set default).
[default] no action
7. Give an example
OutTable table primary key id type int
Create a table that contains foreign keys:
The code is as follows:
Create table temp (id int,namechar (20), foreign key (id) references outTable (id) on delete cascade on updatecascade)
Description: set the id column as the foreign key reference id column of the external outTable as the value of the foreign key delete the corresponding column in this table when the value of the foreign key changes the corresponding column value in this table.
The code is as follows:
Create table temp (id int, name char (20), foreign key (id) references outTable (id) on delete cascade on updatecascade); Thank you for reading this article carefully. I hope it will be helpful for the editor to share the usage of MySQL external keys. At the same time, I also hope that you will support us, pay attention to the industry information channel, and find detailed solutions waiting for you to learn.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.