外键约束错误地形成简单外键(简单)

时间:2017-05-02 08:38:16

标签: sql postgresql

这似乎是一个相当简单的问题,但我还没有弄清楚为什么会这样。

CREATE TABLE Instrument (
    name VARCHAR(50),
    manufacturer VARCHAR(50),
    CONSTRAINT Instrument_PK PRIMARY KEY (name),
    CONSTRAINT Instrument_FK1 FOREIGN KEY (manufacturer) REFERENCES Companies(name)
);

CREATE TABLE Companies (
    name VARCHAR(50),
    CONSTRAINT Companies_PK PRIMARY KEY (name),
);

返回错误:外键约束形成错误

2 个答案:

答案 0 :(得分:0)

首次创建Companies表,然后Instrument。你这里也有错误的逗号:CONSTRAINT Companies_PK PRIMARY KEY (name),

试试这个:

CREATE TABLE Companies (
    name VARCHAR(50),
    CONSTRAINT Companies_PK PRIMARY KEY (name)
);

CREATE TABLE Instrument (
    name VARCHAR(50),
    manufacturer VARCHAR(50),
    CONSTRAINT Instrument_PK PRIMARY KEY (name),
    CONSTRAINT Instrument_FK1 FOREIGN KEY (manufacturer) REFERENCES Companies(name)
);

答案 1 :(得分:0)

我的问题是两个表都引用了彼此,无论哪个顺序,都会引用一个尚不存在的表。