我正在创建一个数据库表。具体来说,这段代码由于某种原因不会添加两个外键,它说不能添加外键约束,我将向您展示我所指的表。
mysql> ALTER TABLE PROGRAM
-> ADD SeriesName varchar(100) NOT NULL, ADD StartYear char(4) NOT NULL,
-> ADD CONSTRAINT program_seriesname_fk
-> FOREIGN KEY(SeriesName) REFERENCES SERIES(SeriesName),
-> ADD CONSTRAINT program_startyear_fk
-> FOREIGN KEY(StartYear) REFERENCES SERIES(StartYear);
ERROR 1215 (HY000): Cannot add foreign key constraint
SERIES 表有 PROGRAM 表,这意味着它是 1:N(一对多关系)。因此,SERIES 中的两个主键成为 PROGRAM 中的两个外键。
mysql> describe series;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| SeriesName | varchar(100) | NO | PRI | | |
| StartYear | char(4) | NO | PRI | | |
| EndYear | char(4) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
3 rows in set (0.07 sec)
mysql> describe program;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| ProgramName | varchar(50) | NO | PRI | | |
| Description | varchar(255) | YES | | NULL | |
| Recorded | date | NO | | NULL | |
+-------------+--------------+------+-----+---------+-------+
3 rows in set (0.01 sec)