Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of setting Foreign key in Database sql statement

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the database sql statement to set the foreign key method, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Database sql statement set foreign key method: 1, add foreign key constraint [alter table slave table add foreign key (foreign key field) references primary table (primary key field)]; 2, delete foreign key constraint [alter table table name drop foreig].

The method of setting foreign keys in the database sql statement:

1. Foreign key constraint

Foreign key constraint: when the value of the foreign key field is updated and inserted, it will be verified with the data of the field in the reference table. If the data is illegal, the update and insertion will fail to ensure the validity of the data.

2. Add foreign key constraints to existing fields

-- add a foreign key constraint alter table students add foreign key (cls_id) references classes (id) to the cls_id field; [it will be verified first, and an error will be reported if it does not conform]

3. Set foreign key constraints when creating a datasheet

-- create school table create table school (id int not null primary key auto_increment, name varchar (10));-- create teacher table create table teacher (id int not null primary key auto_increment, name varchar (10), s_id int not null, foreign key (s_id) references school (id))

4. Delete foreign key constraints

-- you need to obtain the foreign key constraint name first, which is automatically generated by the system. You can obtain the name show create table teacher;-- by viewing the table creation statement, and then delete the foreign key constraint alter table teacher drop foreign key foreign key name according to the name. Thank you for reading this article carefully. I hope the article "the method of setting foreign keys in database sql statements" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is 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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report