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 solve the problem of failed creation of mysql foreign keys

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article mainly explains "how to solve the problem of mysql foreign key creation failure", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to solve the problem of mysql foreign key creation failure" bar!

Create a persons:

CREATE TABLE `persons` (

`id_ p` int (11) NOT NULL AUTO_INCREMENT

`lastname` varchar (10) DEFAULT NULL

`firstname` varchar (10) DEFAULT NULL

`address` varchar (50) DEFAULT NULL

`city` varchar (10) DEFAULT NULL

PRIMARY KEY (`id_ p`)

); www.2cto.com

Re-create table orders:

CREATE TABLE `orders` (

`id_ o` int (11) NOT NULL AUTO_INCREMENT

`orderno` int (11) NOT NULL

`id_ p` int (11) NOT NULL

PRIMARY KEY (`id_ o`)

FOREIGN KEY (`id_ p`) REFERENCES `persons` (`id_ p`)

);

Using the show create table orders statement to query the information of orders, it is found that the foreign key was not created successfully. After checking some information, we finally found out the reason. The table created by mysql by default is of type MyISAM, and the type of the table needs to be specified as innodb:

Www.2cto.com

CREATE TABLE `orders` (

`id_ o` int (11) NOT NULL AUTO_INCREMENT

`orderno` int (11) NOT NULL

`id_ p` int (11) NOT NULL

PRIMARY KEY (`id_ o`)

FOREIGN KEY (`id_ p`) REFERENCES `persons` (`id_ p`)

) ENGINE=InnoDB

However, the following error is prompted at this time:

ERROR 1005 (HY000): Can't create table 'test.orders' (errno: 150)

It is OK to specify the type of persons table as innodb.

Summarize several situations in which foreign key creation failed:

1. Foreign keys are different from referenced foreign keys, such as integer and double

two。 Could not find the column to be referenced

3. The character encoding of the table is different.

4. Data engine types are inconsistent, such as innodb and myisam

Thank you for your reading, the above is the content of "how to solve the problem of failed creation of mysql foreign keys". After the study of this article, I believe you have a deeper understanding of how to solve the problem of failed creation of mysql foreign keys. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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