In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces to you what methods can add mysql foreign keys, the content of the article is carefully selected and edited by the author, with a certain pertinence, for everyone's reference significance is still relatively great, the following with the author to understand what methods can add mysql foreign keys bar.
Mysql can add foreign keys directly after attribute values, at the end of SQL statements, using fk and adding foreign keys after table creation.
Four ways to add Foreign Keys to mysql
Create master table: class
CREATE TABLE class (cid INT PRIMARY KEY AUTO_INCREMENT,sname VARCHAR (15) NOT NULL) INSERT INTO class VALUES (Class 1, NULL,' Grade 6) INSERT INTO class VALUES (Class 2, Grade 6, NULL,')
First: add directly after the attribute value
CREATE TABLE student (sid INT AUTO_INCREMENT,sname VARCHAR (10) NOT NULL,s_cid INT REFERENCES class (cid), PRIMARY KEY (sid))
/ / add data for testing
INSERT INTO student VALUES (NULL,' King sledgehammer', 2) INSERT INTO student VALUES (NULL,' Hu Hansan', 3) / / the constraint does not take effect. The reason to be checked is INSERT INTO student VALUES (NULL,' shit', 4) / / unbelievable, once again tested.
This way, the test does not know why, actually added WTF? Baidu failed on the Internet, and the self-study group had no reply and put it on hold here for the time being.
The second kind: 2. Add at the end of the sql statement
CREATE TABLE student (sid INT AUTO_INCREMENT,sname VARCHAR (10), s_cid INT,PRIMARY KEY (sid), FOREIGN KEY (s_cid) REFERENCES class (cid))
/ / insert data to test
INSERT INTO student VALUES (NULL,' King sledgehammer', 2) INSERT INTO student VALUES (NULL,' Hu Hansan', 3) / constraint effective insert failed
Foreign key constraint takes effect
The third kind: the use of fk
CREATE TABLE student (sid INT AUTO_INCREMENT,sname VARCHAR (10) NOT NULL,s_cid INT, PRIMARY KEY (sid), CONSTRAINT fk_student_class FOREIGN KEY (s_cid) REFERENCES class (cid)); INSERT INTO student VALUES (NULL,' King sledgehammer', 2) INSERT INTO student VALUES (NULL,' Hu Hansan', 3) / constraint entry failure
Fourth: add foreign keys after creating the table
CREATE TABLE student (sid INT AUTO_INCREMENT,sname VARCHAR (10) NOT NULL,s_cid INT, PRIMARY KEY (sid))
/ / add a foreign key constraint:
ALTER TABLE student ADD FOREIGN KEY (s_cid) REFERENCES class (cid)
/ / Test
INSERT INTO student VALUES (NULL,' King sledgehammer', 2) INSERT INTO student VALUES (NULL,' Hu Hansan', 3) / constraint effective insert failed
After reading the above about what ways to add mysql foreign keys, many readers must have some understanding, if you need to get more industry knowledge and information, you can continue to pay attention to our industry information column.
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.