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 write the foreign key of mysql

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to write the foreign keys of mysql, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

How to write the foreign key of mysql?

Mysql add Foreign key:

Add a foreign key to the data table that has been added:

Syntax:

Alter table table name add constraint FK_ID foreign key (your foreign key field name) REFERENCES table name (primary key field name of the corresponding table)

Example:

Alter table tb_active add constraint FK_ID foreign key (user_id) REFERENCES tb_user (id) / / FK_ID is the name of the foreign key / * CREATE TABLE `tb_ active` (`id`int (11) NOT NULL AUTO_INCREMENT, `title`varchar (100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, `content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, `user_ id` int (11) NOT NULL,PRIMARY KEY (`id`), KEY `user_ id` (`user_ id`), KEY `user_id_ 2` (`user_ id`) CONSTRAINT `user_ ID` FOREIGN KEY (`tb_ id`) REFERENCES `tb_ user` (`id`) ENGINE=InnoDB DEFAULT CHARSET=latin1*/ is all the content of how to write the foreign key of mysql Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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