我一直在尝试为我的旁边项目构建数据库,但我似乎无法理解外键创建的概念。这是我的代码(如果你能看一下,我将非常感激):
CREATE TABLE IF NOT EXISTS meeting (
meeting_id SERIAL PRIMARY KEY,
period_no varchar(10) NOT NULL,
session_no varchar(10) NOT NULL,
meeting_no varchar(10) NOT NULL,
date DATE NOT NULL,
chair_id BIGINT UNSIGNED,
agenda_id BIGINT UNSIGNED,
CONSTRAINT FOREIGN KEY (chair_id) REFERENCES mp (mp_id),
CONSTRAINT FOREIGN KEY (agenda_id) REFERENCES agenda (agenda_id)
)engine=innodb;
CREATE TABLE IF NOT EXISTS agenda (
agenda_id SERIAL PRIMARY KEY,
name varchar(100) NOT NULL,
special boolean NOT NULL DEFAULT 0,
meeting_id BIGINT UNSIGNED,
speech_id BIGINT UNSIGNED,
FOREIGN KEY (meeting_id) REFERENCES meeting(meeting_id),
FOREIGN KEY (speech_id) REFERENCES agenda(speech)
)engine=innodb;
CREATE TABLE IF NOT EXISTS speech (
speech_id SERIAL PRIMARY KEY,
body text,
mp_id BIGINT UNSIGNED,
FOREIGN KEY (chair_id) REFERENCES mp(mp_id)
)engine=innodb;
CREATE TABLE IF NOT EXISTS mp (
mp_id SERIAL PRIMARY KEY,
name varchar(100) NOT NULL,
chair_in_meeting_id BIGINT UNSIGNED NULL,
speech_id BIGINT UNSIGNED,
FOREIGN KEY (chair_in_meeting_id) REFERENCES meeting(meeting_id),
FOREIGN KEY (speech_id) REFERENCES agenda(speech)
)engine=innodb;
该过程在第一个表停止。我认为应该有一个特定的顺序,用他们的主键创建表,然后通过添加外键的引用来改变它们,但这也不起作用。有什么想法吗?
MariaDB版本是:10.0.29-MariaDB SLE 12 SP1软件包
编辑:
最新的外键错误
2017-03-10 14:55:03 7f3180ae5700表
politikoslogos
的外键约束出错。IF
: 使用外键约束创建表politikoslogos
。IF
失败。在'FOREIGN KEY(chair_id)REFERENCES mp(mp_id)附近的数据字典中找不到引用表politikoslogos
。mp
, 约束外键(agenda_id)参考议程(agenda_id) )发动机= innodb的”。