外键引用内连接

时间:2017-03-21 08:51:24

标签: sql postgresql foreign-keys inner-join

我有三张桌子:

CREATE TABLE problems (pid SERIAL PRIMARY KEY);
CREATE TABLE submissions (sid SERIAL PRIMARY KEY, pid INTEGER NOT NULL REFERENCES problems);
CREATE TABLE testcases (tid SERIAL PRIMARY KEY, pid INTEGER NOT NULL REFERENCES problems);

现在我要添加第四个:

CREATE TABLE results (sid INTEGER NOT NULL REFERENCES submissions,
    tid INTEGER NOT NULL REFERENCES testcases, PRIMARY KEY (sid, tid));

我想确保"提交"和" testcase"每个"结果"是#"问题"。不幸的是,人们无法引用视图,所以这不会起作用:

CREATE VIEW allowed_results AS
    SELECT sid, tid FROM submissions INNER JOIN testcases USING (pid);
CREATE TABLE results (sid INTEGER NOT NULL, tid INTEGER NOT NULL,
    PRIMARY KEY (sid, tid), FOREIGN KEY (sid, tid) REFERENCES allowed_results);

我正在使用PostgreSQL 9.6.1。我该怎么解决这个问题?

0 个答案:

没有答案