我是结构化查询语言的初学者。我想添加具有不同外键的多列。如示例:
drop schema humman;
create schema humman;
CREATE TABLE humman.father (
id int not null auto_increment,
firstname varchar(200) not null,
primary key(id)
);
create table humman.mather(
id int not null auto_increment,
FirstName varchar(200),
primary key(id)
);
CREATE TABLE humman.child (
id int not null auto_increment,
firstname varchar(200) not null,
primary key(id)
);
use `humman`;
alter table humman.child
ADD `parentId` int ,
ADD `motherId` int,
ADD foreign key (`parentId`) references father(`id`),
ADD foreign key (`motherId`) references mother(`id`);
Error code: 1215 Cannot add foreign key CONSTRAINT
最佳答案
您的代码很好,除了一个拼写错误,您在第二个表定义中将“mother”拼写为“mather”;
create table humman.mather(
id int not null auto_increment,
FirstName varchar(200),
primary key(id)
);
纠正它,它应该可以工作。
关于mysql - 添加多列和外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50788397/