Bobcares

MySQL error 1005 (hy000): Causes and solution.

by | Jun 4, 2023

Let us take a closer look at MySQL error 1005 (hy000) and how to remove it with the support of our MySQL support team at Bobcares.

 

 

MySQL ERROR 1005: Can’t create table (errno: 150) (Foreign Key)

MySQL error 1005 (hy000)

The example given below will demonstrate a case in which this error could occur.

CREATE TABLE main(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id)
);
CREATE TABLE other(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
main_id INT NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY(main_id) REFERENCES main(id)
);

When attempting to make the table “other” reference the table “main” via the foreign key “main id,” an Error 150 will opn up.

Solution for MySQL error 1005 (hy000) :

The foreign key “main id” must be of the same type as the primary key it refers. In the example, “main id” in the table “other” has the type INT NOT NULL, whereas “id” in the table “main” has the type “INT UNSIGNED NOT NULL” as well as AUTO INCREMENT, but this isn’t a problem. Here’s a working example to make everything crystal apparent.

CREATE TABLE main(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id)
);
CREATE TABLE other(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
main_id INT UNSIGNED NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY(main_id) REFERENCES main(id)
);

To fix ‘MySQL ERROR 1005: Can’t create table (errno: 150),’ we only need to make sure that the foreign key is of the same type as the primary key.

Other methods

  • Make sure to include ENGINE=InnoDB; in your CREATE TABLE – statement.
  • Ensure that the InnoDB isin the activation stage on the MySQL server. To test this, use the following command: SHOW VARIABLES LIKE ‘have innodb’; if YES, then InnoDB is enabled.
  • Examine the command for upper- and lowercase letters in table and field names.
  • Check this not just on the table we wish to create, but also on the tables to which the foreign keys relate.
  • Make sure that the linked tables are appropriately indexed.

Troubleshooting:

We can attempt SHOW WARNINGS, but we’ll learn more about the error by executing the following command as root:

mysql> SHOW engine innodb STATUS;

It returns a dump of the InnoDB’s activity. A user can gain access to the complete details by unfolding the complete log.

------------------------
LATEST FOREIGN KEY ERROR
------------------------
1001111 13:11:00 Error IN FOREIGN KEY CONSTRAINT OF TABLE my_data :
FOREIGN KEY(id2)
REFERENCES pk(test1):
Cannot find an INDEX IN the referenced TABLE WHERE the
referenced COLUMNS appear AS the FIRST COLUMNS, OR COLUMN types
IN the TABLE AND the referenced TABLE do NOT MATCH FOR CONSTRAINT.

While there is additional information, it is not always sufficient to remedy the problem, especially if the user is new to MySQL. The first thing to check is if the data types in the foreign key and main key columns match.

The most popular variant is to use an int unsigned data type for the main key column and an int data type for the foreign key column. The InnonDB Engine terminates this process. Naturally, anybody can repair it by modifying the data type of the foreign key to match the int unsigned data type.

 

[Need assistance with similar queries? We are here to help]

 

Conclusion

To sum up we have now seen how to remove the To conclude the MySQL error 1005 (hy000) with the support of our tech support team.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF