MySQL 错误 1215 - 外键(日期类型)到主键(日期类型)

标签 mysql foreign-keys constraints

我正在尝试将 PostDate 从 Speech 引用到从 P_ASSIGNMENT 的 PostDate,但它给我 “错误 1215:无法添加外键约束”。 我目前正在 MySQL Workbench 上开发 InnoDB。

这是我的 SQL 代码:

CREATE TABLE P_ASSIGNMENT
(StaffID int not null,
 JobID int not null,
 PostDate date not null,
 EndDate date,
 CONSTRAINT P_Assignment_pk PRIMARY KEY (StaffID, JobID, PostDate),
);

CREATE TABLE SPEECH
 (EventName varchar(100) not null,
  EventDate date not null,
  OrderNum int not null,
  ContentAbst varchar(250),
  Contact int not null,
  Presenter int not null,
  JobID int not null,
  PostDate date not null,
 CONSTRAINT Speech_pk PRIMARY KEY (EventName, EventDate, OrderNum),
 CONSTRAINT Speech_fk3 FOREIGN KEY (Presenter) REFERENCES P_ASSIGNMENT (StaffID),
 CONSTRAINT Speech_fk4 FOREIGN KEY (JobID) REFERENCES P_ASSIGNMENT (JobID),
 CONSTRAINT Speech_fk5 FOREIGN KEY (PostDate) REFERENCES P_ASSIGNMENT(PostDate)
);

最佳答案

这是一个单一复合外键。语法是:

CREATE TABLE SPEECH
 (EventName varchar(100) not null,
  EventDate date not null,
  OrderNum int not null,
  ContentAbst varchar(250),
  Contact int not null,
  Presenter int not null,
  JobID int not null,
  PostDate date not null,
 CONSTRAINT Speech_pk PRIMARY KEY (EventName, EventDate, OrderNum),
 CONSTRAINT Speech_fk3 FOREIGN KEY (Presenter, JobID, PostDate) -- fix here
   REFERENCES P_ASSIGNMENT (StaffID, JobID, PostDate)
);

为了说明这一点,您没有三个外键。你有一个外键。该外键是复合键,因为它包含三列。

关于MySQL 错误 1215 - 外键(日期类型)到主键(日期类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55104458/

相关文章:

mysql - MySQL Workbench 中的外键

artificial-intelligence - 约束满足问题

mysql - php, mysql with utf8 数据的奇怪排序问题

python - 过滤 python mysql 结果

mysql - ON DELETE CASCADE 的反向

Mysql Prepare 语句多次插入

python - 如何更改 Django 中外键的 db_column 名称?

sql - 是否可以改变现有 mysql 外键的级联行为?

tsql - 创建表时声明默认约束

mysql - 如何重命名外键约束| MySQL 5.6、InnoDB