Background:
Yesterday I was creating a small database for practice, and created about 9 tables, and got this error in the create table
statement of only one of them. The question about that is located here. Thankfully somebody told an alternative way by creating the table without the foreign key
and then use the alter table
statement to add the foreign key constraint, and it worked.
Today I am creating another database, and I am getting this error in all the tables which contain foreign keys. I have tried the alternative way of creating the table and then adding foreign keys by alter table
statement. But it doesn't seem to help.
My Research:
I know this question has been been addressed before on this website, but I have tried the solutions on this, this page, except adding indexes because firstly, I don't need them in such a small database (second point in the first answer), and secondly I don't know them and I want to keep it simple. None of those answers helped.
Moreover, as it is mentioned here, " If the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed. "
Please tell me what's wrong with my foreign key constraint.
CREATE TABLE table1 (
table1_id INT(11) AUTO_INCREMENT,
name VARCHAR(5000),
code VARCHAR(5000),
color VARCHAR(5000),
PRIMARY KEY (table1_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE table2( -- error code 1005 can t create table errno 150 mysql
table2_id INT(11) AUTO_INCREMENT,
date DATE,
start_time TIME,
end_time TIME,
table1_id INT(11) COMMENT 'FK FROM table1',
PRIMARY KEY (table2_id),
FOREIGN KEY (table1_id)
REFERENCES table1(table1_id)
ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
So any hints/tips on this?