我有这个简单的代码
create table transport (
CODE varchar (10),
TDATE date,
ID integer,
PNAME varchar (10),
primary key (CODE,TDATE,ID,PNAME) );
create table planes (
NAME varchar (10),
NSEATS integer,
FSEATS integer,
ECSEATS integer,
primary key (NAME),
CONSTRAINT foreign key(NAME) references transport(PNAME));
但它不允许我做外键事情,因为它说errno:150“外键约束不正确
任何提示赞赏 感谢
答案 0 :(得分:0)
在这种情况下使用复合主键是不合理的。如果需要外键引用,则需要引用主键中的所有列。所以,这个版本更简单:
create table transport (
transportId int not null auto_increment primary key,
CODE varchar (10),
TDATE date,
ID integer,
PNAME varchar (10),
unique (CODE, TDATE, ID, PNAME)
);
create table planes (
planeId int not null auto_increment primary key,
NAME varchar (10),
NSEATS integer,
FSEATS integer,
ECSEATS integer,
primary key (NAME),
transportId int,
CONSTRAINT foreign key(transportId) references transport(transportId)
);
答案 1 :(得分:0)
规则是:一个表中的外键指向另一个表中的主键。