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

前端 未结 5 654
生来不讨喜
生来不讨喜 2021-01-01 06:40

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
(         


        
相关标签:
5条回答
  • 2021-01-01 07:13

    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) 
    
    0 讨论(0)
  • 2021-01-01 07:22

    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.

    0 讨论(0)
  • 2021-01-01 07:26

    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)

    0 讨论(0)
  • 2021-01-01 07:34

    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

    0 讨论(0)
  • 2021-01-01 07:35

    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.

    0 讨论(0)
提交回复
热议问题