添加约束以比较不同表的两个属性?

时间:2019-03-05 19:54:22

标签: sql constraints

这是我的桌子。我需要检查在StudentBranch中引用的“程序”属性与表“学生”和表“分支”是否相同。我该怎么办?

 CREATE TABLE Programmes (
    name            VARCHAR(200)   UNIQUE NOT NULL,
    CONSTRAINT pk_Programmes PRIMARY KEY (name)
);

CREATE TABLE Students (
    id              NUMERIC(10,0)  UNIQUE NOT NULL,
    program         VARCHAR(200)   NOT NULL,
    CONSTRAINT pk_Students PRIMARY KEY (idnr),
    FOREIGN KEY (program) REFERENCES Programmes(name)
);

CREATE TABLE Branches (
    name            VARCHAR(200)   UNIQUE NOT NULL,
    program         VARCHAR(200)   NOT NULL,
    CONSTRAINT pk_Branches PRIMARY KEY (name, program),
    FOREIGN KEY (program) REFERENCES Programmes(name)
);

CREATE TABLE StudentsBranch (
    student         NUMERIC(10,0)  NOT NULL,
    program         VARCHAR(200)   NOT NULL,
    branch          VARCHAR(200)   NOT NULL,
    CONSTRAINT pk_StudentsBranch PRIMARY KEY (student),
    /* Below how the foreign keys I think should be  */
    FOREIGN KEY (student, program) REFERENCES Students(idnr, program),
    FOREIGN KEY (branch, program) REFERENCES Branches(name, program)
    /* I need to add a constraint to verify that the 'program' in Students
     * and the 'program' in Branches are equivalent. How?
    */
);

2 个答案:

答案 0 :(得分:0)

拥有一个主键的数字列并为每个表都具有主键总是一个好主意。 一旦拥有每个表的主键,就可以引用特定表的主键,并将其称为默认键。

CREATE TABLE Programmes (
ID          INT,
name        VARCHAR(200)   UNIQUE NOT NULL,
CONSTRAINT pk_Programmes PRIMARY KEY (ID)
);

CREATE TABLE  Students(
id              INT,
ProgrammID  INT NOT NULL,
CONSTRAINT pk_Students PRIMARY KEY (ID),
FOREIGN KEY (ProgrammID) REFERENCES Programmes(ID)
);

CREATE TABLE Branches (
BranchID        INT,
ProgrammID  INT NOT NULL,
name            VARCHAR(200)   UNIQUE NOT NULL,
--program         VARCHAR(200)   NOT NULL,
CONSTRAINT pk_Branches PRIMARY KEY (BranchID, ProgrammID),
FOREIGN KEY (ProgrammID) REFERENCES Programmes(ID)
);

CREATE TABLE StudentsBranch (
StudentsBranchID    INT,
studentID           INT  NOT NULL,
ProgrammID          INT   NOT NULL,
BranchID            INT   NOT NULL,
CONSTRAINT pk_StudentsBranch PRIMARY KEY (StudentsBranchID),
FOREIGN KEY (ProgrammID) REFERENCES Programmes(id),
FOREIGN KEY (studentID) REFERENCES Students(id),
FOREIGN KEY (BranchID, ProgrammID) REFERENCES Branches(BranchID, ProgrammID)

);

答案 1 :(得分:0)

您无法使用现有的现有数据库模型编写该约束。

我认为您可以做到的唯一方法是将Students的主键更改为(idprogram):

CREATE TABLE Students (
    id              NUMERIC(10,0)  UNIQUE NOT NULL,
    program         VARCHAR(200)   NOT NULL,
    CONSTRAINT pk_Students PRIMARY KEY (id, program),
    FOREIGN KEY (program) REFERENCES Programmes(name)
);

然后,表StudentsBranch可以自然而然地使用单个program来强制执行两个FK,如下所示:

CREATE TABLE StudentsBranch (
    student         NUMERIC(10,0)  NOT NULL,
    program         VARCHAR(200)   NOT NULL,
    branch          VARCHAR(200)   NOT NULL,
    CONSTRAINT pk_StudentsBranch PRIMARY KEY (student),
    FOREIGN KEY (student, program) REFERENCES Students (id, program),
    FOREIGN KEY (branch, program) REFERENCES Branches (name, program)
);