Error code 1005, SQL state HY000: Can't create table errno: 150

旧街凉风 提交于 2019-12-18 13:38:07

问题


I'm trying to create a table but the script fails as soon as my netbeans errors the first table of DB.

How can this be solved?

CREATE TABLE filmy
(
    Film_Id int NOT NULL,
    Nazwa varchar(250),
    Adres varchar(250),
    Data_Utworzenia date,
    Komentarz varchar(250),
    Gat_Id int,
    Sub_Id int,
    Aut_Id int,
    User_Id int,

    Primary Key (Film_Id),
    CONSTRAINT fk_GatFilmy FOREIGN KEY (Gat_Id) REFERENCES gatunek(Gat_Id),
    CONSTRAINT fk_SubFilmy FOREIGN KEY (Sub_Id) REFERENCES subgatunek(Sub_Id),
    CONSTRAINT fk_AutFilmy FOREIGN KEY (Aut_Id) REFERENCES autor(Aut_Id),
    CONSTRAINT fk_UserFilmy FOREIGN KEY (User_Id) REFERENCES users(User_Id)
)

回答1:


Use show innodb status - buried in the output (around the middle) is a "last foreign key error" section. It'll explain exactly why the table creation failed.

usually it's due to a reference FK field not existing (typo, wrong table), or there's a field-type mismatch. FK-linked fields must match definitions exactly. A char(1) field can't be FK'd to a char(5) field, etc...

Note: In MySQL 5.5, the command for this is show engine innodb status (thanks kewpiedoll99)




回答2:


Here is my solution:

CREATE TABLE filmy 
( 
    Film_Id           int           NOT NULL, 
    Nazwa             varchar(250)      NULL, 
    Adres             varchar(250)      NULL, 
    Data_Utworzenia   date              DEFAULT '0000-00-00', 
    Komentarz         varchar(250)      NULL, 
    Gat_Id            int               NULL, 
    Sub_Id            int               NULL, 
    Aut_Id            int               NULL, 
    User_Id           int               NULL, 
 Primary Key (Film_Id, Gat_Id, Sub_Id, Aut_Id, User_Id )
) ENGINE=INNODB;

Foreign key constraints are done after creating the gat, sub, aut & user or else the ide does not know have the two tables existing to make the table constraint a reality! Try: alter table filmy add constraint gatfilmy foreign key (gat_id) references gat(gat_id) on update restrict on delete restrict you have to keep consistent; either name the table gat or gatunek, it cannot be both. How will the cpu know which is gat or gatunek if you do not define them both? Now try with the rest constraints and remember you ahve to create all the tables before you can alter them!

CONSTRAINT fk_GatFilmy FOREIGN KEY (Gat_Id) REFERENCES gatunek(Gat_Id), 
CONSTRAINT fk_SubFilmy FOREIGN KEY (Sub_Id) REFERENCES subgatunek(Sub_Id), 
CONSTRAINT fk_AutFilmy FOREIGN KEY (Aut_Id) REFERENCES autor(Aut_Id), 
CONSTRAINT fk_UserFilmy FOREIGN KEY (User_Id) REFERENCES users(User_Id) 



回答3:


The code 150 is a foreign key error.

One of the referenced tables or columns does not exist (yet, maybe later in your script) or doesn't match type/length/collation/charset. Comment them out in turn to see which one.

Or run separate ALTER TABLE commands after all your CREATEs have run




回答4:


I usually get that error when I try to add a foreign key for a column that doesn't have an index on it; I notice that none of your associated columns do in the SQL shown.




回答5:


May you used with this table name(filmy) in a relation to other table and then you dropped it. check any relation and remove every where you use with this table name or change your table name for example use "filmy1" I changed my table name then it worked.

I hope this work.



来源:https://stackoverflow.com/questions/8070336/error-code-1005-sql-state-hy000-cant-create-table-errno-150

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!