Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...