Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

I get an error when I try to create a table in mysql.

Any tips on resolving it?

create table stock_in(
    ind int not null auto_increment,
    itemcode varchar(10) not null,
    quantity int not null,
    description text not null,
    sales_ref int not null default -1,
    return_outwards_ref int not null default -1,
    stock_in_receipt_ref int not null default -1,
    date text not null,
    time text not null,
    username text not null,
    foreign key (sales_ref) references sales (receiptno),
    foreign key (return_outwards_ref) references returnoutwards(ind),
    primary key (ind)
);

The Error:

ERROR 1005 (HY000): Can't create table 'posinventory.stock_in' (errno: 150)
like image 658
Boon Avatar asked Jun 15 '12 05:06

Boon


People also ask

Can t create table errno 150?

Altering a table returns an error (errno: 150) if a foreign key definition is incorrectly formed for the altered table. Dropping an index required by a foreign key constraint. The foreign key constraint must be removed before dropping the index.

Can t create table ERROR 1005 MySQL?

The table must have the correct column names and types. It must also have indexes on the referenced keys. If these requirements are not satisfied, MySQL returns Error 1005 that refers to errno: 150 in the error message, which means that a foreign key constraint was not correctly formed.

Can t create table ERROR in SQL?

usually, the mismatch between foreign key & primary key causes the error:150. The foreign key must have the same datatype as the primary key. Also, if the primary key is unsigned then the foreign key must also be unsigned.

What is foreign key w3schools?

A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table. The table with the foreign key is called the child table, and the table with the primary key is called the referenced or parent table.


1 Answers

Check out the MySQL manual about foreign key constrains:

If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.

A few ideas:

  • Better drop the tables and create it new with a well formed syntax.
  • Make sure to add ENGINE=InnoDB; to your CREATE TABLE - command.
  • Make sure InnoDB is enabled on your MySQL server. To verify this, try this command: SHOW VARIABLES LIKE 'have_innodb'; - if it returns a YES, then InnoDB is enabled.
  • Check your command for upper- and lowercases in table- and fieldnames.
  • Check this not only one the table you want to create, but also on the tables the foreign keys are referring to.
  • Make sure your referred tables are properly indexed.
like image 129
Bjoern Avatar answered Sep 21 '22 19:09

Bjoern