简体   繁体   中英

SQL Can't create table (errno: 150)

I have to create this table with all these composite keys.

prospect(custname, carmake, carmodel, caryear, carextcolour, cartrim, optioncode)

All the fields above are underlined to show they are primary. So 7 primary composite keys.

For some reason this does not create the table.

CREATE TABLE prospect
(Custname      VARCHAR(25) NOT NULL,
 Carmake       VARCHAR(25) NOT NULL,
 Carmodel      VARCHAR(20) NOT NULL,
 Caryear       VARCHAR(4) NOT NULL,
 Carextcolour  VARCHAR(10) NOT NULL,
 Cartrim       VARCHAR(10) NOT NULL,
 Optioncode    CHAR(4),
 CONSTRAINT pkprospect PRIMARY KEY (Custname, Carmake, Carmodel, Caryear, Carextcolour,           Cartrim, Optioncode),
 CONSTRAINT fkprospect FOREIGN KEY (Custname) REFERENCES customer(Custname),
 CONSTRAINT fk2prospect FOREIGN KEY (Carmake) REFERENCES car(Carmake),
 CONSTRAINT fk3prospect FOREIGN KEY (Carmodel) REFERENCES car(Carmodel),
 CONSTRAINT fk4prospect FOREIGN KEY (Caryear) REFERENCES car(Caryear),
 CONSTRAINT fk5prospect FOREIGN KEY (Carextcolour) REFERENCES car(Carextcolour),
 CONSTRAINT fk6prospect FOREIGN KEY (Cartrim) REFERENCES car(Cartrim),
 CONSTRAINT fk7prospect FOREIGN KEY (Optioncode) REFERENCES optiontable(Optioncode)
);

the code i'm using to create this table full of composite keys.

只需在查询之前添加此行;)

SET foreign_key_checks = 0;

You should make one foreign key constraint referencing the full primary or unique key of each referenced table. You can't make separate foreign keys to individual columns in the middle of the references table's primary key.

CREATE TABLE prospect
(Custname      VARCHAR(25) NOT NULL,
 Carmake       VARCHAR(25) NOT NULL,
 Carmodel      VARCHAR(20) NOT NULL,
 Caryear       VARCHAR(4) NOT NULL,
 Carextcolour  VARCHAR(10) NOT NULL,
 Cartrim       VARCHAR(10) NOT NULL,
 Optioncode    CHAR(4),
 CONSTRAINT pkprospect PRIMARY KEY (Custname, Carmake, Carmodel, Caryear, Carextcolour,           Cartrim, Optioncode),
 CONSTRAINT fkprospect FOREIGN KEY (Custname) REFERENCES customer(Custname),
 CONSTRAINT fk2prospect FOREIGN KEY (Carmake, Carmodel, Caryear, Carextcolour, Cartrim)
   REFERENCES car(Carmake, Carmodel, Caryear, Carextcolour, Cartrim),
 CONSTRAINT fk7prospect FOREIGN KEY (Optioncode) REFERENCES optiontable(Optioncode)
);

I used the word should above because InnoDB is actually a bit more lenient than the ANSI/ISO SQL standard when it comes to foreign keys. Standard SQL says the columns of the foreign key must be the full list of columns of the referenced primary or unique key.

InnoDB permits you to use a subset of columns, as long as they're a left-prefix of those columns. But you shouldn't do that, because you get really confusing results when a child row may reference multiple rows in its parent table.

Try this: Creating table with primary key for the first step and then use alter table command for foreign keys and add them.

If you have not created the reference tables, you cannot create this table. You can first create all your reference tables and then enter FK submissions with alter table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM