mysql - 添加多列和外键

标签 mysql sql foreign-keys

我是结构化查询语言的初学者。我想添加具有不同外键的多列。如示例:

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/

相关文章:

sql - 来自 PostgreSQL jsonb 数组的值查询

mysql - 如何界定外键的可能值?

MySQL 外键导致表删除

mysql - TiDB CREATE FUNCTION 返回错误

mysql - SQL 查询获取表中每个 fileid 条目的计数

php - 如何结合搜索框和下拉菜单来输出结果(php、mysql)

sql - 这种形式的数据库叫什么?

Mysql从2个表运行总计

只有一个 varchar 作为外键的 MySQL 表

mysql 查询中的总和值