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)
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.
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.
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.
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.
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:
ENGINE=InnoDB;
to your CREATE TABLE
- command.SHOW VARIABLES LIKE 'have_innodb';
- if it returns a YES, then InnoDB is enabled.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With