小编R. *_*rdi的帖子

MYSQL - 同一个表上的外键问题

在 Mysql 5.5 中,我有一个表,其中存储了一些有关“事件”的信息。为了按时间顺序排列,我创建了两列,分别引用前一个事件和下一个事件。所以我做了两个外键,引用同一个表的主键。如果一行是按时间顺序排列的第一行,则前一个元素的值将为空。

所以,我的表是:

calendars
--------
calendar_id (PRIMARY KEY)
calendar_prev
calendar_next
other columns...
Run Code Online (Sandbox Code Playgroud)

我有这个数据(作为例子)

+--------------+---------------+---------------+
|  calendar_id | calendar_prev | calendar_next |
+--------------+---------------+---------------+
|       1      |    NULL       |       2       |
|       2      |      1        |       3       |
|       3      |      2        |       4       |
|       4      |      3        |       5       |
|       5      |      4        |     NULL      |
+--------------+---------------+---------------+
Run Code Online (Sandbox Code Playgroud)

和这些外键。

ALTER TABLE `calendars`
ADD CONSTRAINT `calendars_ibfk_3` FOREIGN KEY (`calendar_next`) REFERENCES `calendars` (`calendar_id`) ON DELETE SET NULL ON UPDATE CASCADE, …
Run Code Online (Sandbox Code Playgroud)

foreign-key constraint mysql-5.5

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

constraint ×1

foreign-key ×1

mysql-5.5 ×1