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

How to use Foreign key constraints in MySQL

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

Share

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

How to use foreign key constraints in MySQL? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. View the sql statements created by the database table

Show create table vip

two。 View the constraint name of the foreign key

CREATE TABLE `vip` (`id` int (11) NOT NULL AUTO_INCREMENT, `address` varchar (255) DEFAULT NULL, `code` varchar (255) DEFAULT NULL, `mobile` varchar (255) DEFAULT NULL, `name` varchar (255) DEFAULT NULL, `score` int (11) DEFAULT NULL, `id_ code`varchar (255) DEFAULT NULL, `user_ id` int (11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `FK1C81D1738DA76` (`user_ id`), CONSTRAINT `FK1C81D1738DA76` FOREIGN KEY (`user_ id`) `user` (`id`) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8

3. Release foreign key constraint

Alter table vip drop foreign key FK1C81D1738DA76

4. Delete foreign key

Alter table vip drop user_id

5. Increase foreign key constraint

ALTER TABLE `tfeedbackmessage`ADD CONSTRAINT `FK_ i1q2cf5pxfr8r69cfci3yyari` FOREIGN KEY (`HANDLERID`) REFERENCES `toperationuser` (`FID`) ON DELETE CASCADE ON UPDATE RESTRICT

Summary of common operations:

View the field information of the table: desc table name

View all information about the table: show create table table name

Add primary key constraint: alter table table name add constraint primary key (such as: PK_ table name) primary key table name (primary key field)

Add foreign key constraint: alter table slave table add constraint foreign key (such as: FK_ slave table _ master table) foreign key slave table (foreign key field) references master table (primary key field)

Delete primary key constraint: alter table table name drop primary key

Delete foreign key constraint: alter table table name drop foreign key foreign key (case sensitive)

After reading the above, have you mastered how to use foreign key constraints in MySQL? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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