Foreign key error when creating table (errno 150) - mysql

I'm trying to create a relationship between two tables using the following queries:
create table card
(id int not null auto_increment,
post_id bigint(20) unsigned not null,
primary key(id));
create table user_comment
(id int not null auto_increment,
comment_author longtext,
comment_post_id bigint(20) unsigned not null,
primary key (id),
foreign key (comment_post_id) references card(post_id));
However it gives me the following error message:
ERROR 1005 (HY000) at line 9: Can't create table 'test.user_comment'
(errno: 150)
If I execute the command for showing innodb status:
show engine innodb status;
It shows this message:
LATEST FOREIGN KEY ERROR
Error in foreign key constraint of table test/user_comment: foreign key
(comment_post_id) references card(post_id)): 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.
But I still can't figure out how to fix the problem.
Any ideas?

From the docs:
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later, if you create another index that can be used to enforce the foreign key constraint. index_name, if given, is used as described previously.
Create an index on card (post_id).

Just use this command once before creating the table:
SET FOREIGN_KEY_CHECKS=0;

Related

error 1215 while trying to create foreign key

I'm trying to create roles management system and I get error while trying to create mysql table with foreign keys. I checked w3school documents and everything seems to be same.
My error:
#1215 - Cannot add foreign key constraint
MySQL:
CREATE TABLE user_role (
user_id INTEGER UNSIGNED NOT NULL,
role_id INTEGER UNSIGNED NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (role_id) REFERENCES roles(role_id)
);
I run this command:
SHOW ENGINE INNODB STATUS;
And got this:
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.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.

Unable to add two foreign keys to a table

I have two tables as follow:
1st Table:
CREATE TABLE User (
User_ID VARCHAR(8)NOT NULL PRIMARY KEY,
User_Name VARCHAR (25) NOT NULL,
User_Gender CHAR (1) NOT NULL,
User_Position VARCHAR (10) NOT NULL,
);
2nd table:
CREATE TABLE Training (
Training_Code VARCHAR(8) NOT NULL Primary Key,
Training_Title VARCHAR(30) NOT NULL,
);
I am trying to create a table which has two foreign keys to join both of the previous tables:
CREATE TABLE Request (
User_ID VARCHAR(8) NOT NULL,
Training_Code VARCHAR(8) NOT NULL,
Request_Status INT(1) NOT NULL
);
When I am trying to set the foreign keys in the new table, the User_ID can be done successfully but the Training_Code cannot be set to foreign key due to the error:
ERROR 1215 (HY000): Cannot add foreign key constraint
As I searched for this problem, the reason for it, is that data type is not the same, or name is not the same.. but in my situation both are correct so could you tell me what is wrong here ?
You need an index for this column in table Request too:
Issue first
CREATE INDEX idx_training_code ON Request (Training_Code);
Then you should be successful creating the foreign key constraint with
ALTER TABLE Request
ADD CONSTRAINT FOREIGN KEY idx_training_code (Training_Code)
REFERENCES Training(Training_Code);
It worked for me. But I've got to say that it worked without create index too, as the documentation of Using FOREIGN KEY Constraints states:
MySQL requires indexes on foreign keys and referenced keys so that
foreign key checks can be fast and not require a table scan. In the
referencing table, there must be an index where the foreign key
columns are listed as the first columns in the same order. Such an
index is created on the referencing table automatically if it does not
exist. This index might be silently dropped later, if you create
another index that can be used to enforce the foreign key constraint.
index_name, if given, is used as described previously.
Emphasis by me. I don't know what's the issue in your case.
Demo
Explanation of the issue
The behavior mentioned in the question can be reproduced if the table Training is using the MyISAM storage engine. Then creating a foreign key referencing the table Training will produce the mentioned error.
If there's data in the table, then simple dropping of the table would not be the best solution. You can change the storage engine to InnoDB with
ALTER TABLE Training Engine=InnoDB;
Now you can successfully add the foreign key constraint.

Error Code: 1005 can't create table

please see my create statement:
CREATE TABLE INTERNAL_MEDICINE_DETAIL(DIAGNOSE_ITEM VARCHAR(15) NOT NULL,
EXM_RESULT VARCHAR(50),
IDENTIFY_ID INT AUTO_INCREMENT PRIMARY KEY,
SUMMARY_ID INT NOT NULL,
FOREIGN KEY(SUMMARY_ID) REFERENCES INTERNAL_MEDICINE_SUMMARY(SUMMARY_ID),
FOREIGN KEY(DIAGNOSE_ITEM) REFERENCES INTERNAL_MEDICINE_ITEM_DEF(DIAGNOSE_ITEM))
The referenced two tables are created successfully:
CREATE TABLE INTERNAL_MEDICINE_ITEM_DEF(DIAGNOSE_ITEM VARCHAR(15) NOT NULL,
ITEM_DESCRIPTION VARCHAR(50) NOT NULL,
IDENTIFY_ID INT AUTO_INCREMENT PRIMARY KEY)
CREATE TABLE INTERNAL_MEDICINE_SUMMARY(SUMMARY_ID INT AUTO_INCREMENT PRIMARY KEY,
SUMMARY VARCHAR(1000),
R_IDENTIFYID INT NOT NULL,
FOREIGN KEY(R_IDENTIFYID) REFERENCES BASICINFO(IDENTIFY_ID))
I have seen the mean of error code 1005 on MySQL web site, it says:
Cannot create table. If the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed. If the error message refers to error –1, table creation probably failed because the table includes a column name that matched the name of an internal InnoDB table.
my error is 150 currently, but I cannot find the difference of the two foreign keys defined in INTERNAL_MEDICINE_DETAIL table with the two fields defined in INTERNAL_MEDICINE_SUMMARY AND INTERNAL_MEDICINE_ITEM_DEF respectively.
So, could you please help me and tell me the reason?
From the InnoDB foreign key constraints documentation:
InnoDB permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.
You'll need to create an index on INTERNAL_MEDICINE_ITEM_DEF that has DIAGNOSE_ITEM as its first column. (No need to change anything for INTERNAL_MEDICINE_SUMMARY since the referenced column is that table's primary key.)

Can't add Foreign Key in MySQL with identical columns

I have two tables, each with a provider column:
CREATE TABLE `title` (
`provider` varchar(40) CHARACTER SET latin1 NOT NULL,
CREATE TABLE `notification` (
`provider` varchar(40) CHARACTER SET latin1 NOT NULL,
However, when I try and add a foreign key
ALTER TABLE notification ADD FOREIGN KEY (provider) REFERENCES title (provider)
I get the following obscure error:
Can't create table 'metadata.#sql-c91_345b' (errno: 150)
Both of the tables are empty. Why is this occurring and what do I need to do to fix this?
http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html says:
InnoDB requires indexes on foreign keys and referenced keys so that
foreign key checks can be fast and not require a table scan.
In the referencing table, there must be an index where the foreign key
columns are listed as the first columns in the same order.
Such an index is created on the referencing table automatically if it
does not exist.
(This is in contrast to some older versions, in which indexes had to
be created explicitly or the creation of foreign key constraints would
fail.) index_name, if given, is used as described previously.
I think you should create any (unique, primary, or plain) index for title.provider before creating a foreign key pointing to it.
Run SHOW ENGINE INNODB STATUS\G and look at the "LATEST FOREIGN KEY ERROR" to see more details on the error.

MySQL Error when dropping index (errno 150)

I've got problem with dropping foreign key index, I always get the same error
ALTER TABLE `comments` DROP INDEX `id_user`
which outputs
1025 - Error on rename of './postuj_cz1/#sql-d834_a0c704'
to './postuj_cz1/comments' (errno: 150)
The id_user on the other table is simple primary key index.
I'm using MySQL version 5.0.85
There are other causes too. For example I had a unique index involving two separate foreign key columns. I had to drop the foreign keys in question before I could drop the unique index. (And obviously you can add the foreign keys back afterward.)
INNODB : this could be as simple as removing the Relation before dropping the Index.
According to this link, the error relates to the definition of the primary key field. The error isn't about the foreign key index.
Check the primary key for the COMMENTS table to make sure it does not have the UNSIGNED keyword while the COMMENTS.id_user foreign key had the UNSIGNED keyword. This keyword was causing the problem - inconsistent type of field.
To fix, add the UNSIGNED keyword to the primary key definition for the COMMENTS table. Or remove the UNSIGNED keyword from the foreign key definition...
The index is for an foreign key on 'user' table, so
In first, try this command:
SHOW CREATE TABLE my_table
Find the name of the constraint corresponding to the index on the foreign key,
and after that, try the command:
ALTER TABLE my_table DROP FOREIGN KEY FK_myconstraintcode
WARNING: If you try to drop the foreign key with the foreign key name, you will have an error to!