只接受基于其他表格的值?(MySQL,PostgreSQL,Django)

wgx48brx  于 2023-01-01  发布在  Mysql
关注(0)|答案(1)|浏览(95)

我正在开发一个学校/大学软件,我陷入了这种情况:

    • 餐桌教授**

| 身份证|姓名|
| - ------| - ------|
| 1个|沃德先生|
| 第二章|史密斯先生|

    • 表格主题**

| 身份证|姓名|
| - ------| - ------|
| 1个|数学|
| 第二章|物理学|
| 三个|英语|

    • 表教授主题**

| 产品标识|s_标识符|
| - ------| - ------|
| 1个|1个|
| 1个|第二章|
| 第二章|三个|

  • (沃德先生,数学)、(沃德先生,物理)和(史密斯先生,英文)*
    • 表学生类:**

| 身份证|姓名|建筑物|
| - ------| - ------| - ------|
| 十个|十年级|A类|
| 十一|十一年级|A类|

    • 表A:**

| 等级_id|受试者ID|教授标识|富|棒|
| - ------| - ------| - ------| - ------| - ------|
| 十个|1个|1个|足1|棒1|
| 十个|第二章|1个|食物2|巴2|
| 十个|三个|零|foo3语言|棒3|
| 十一|1个|1个|foo4|棒4|
| 十一|第二章|1个|foo5语言|棒5|
| 十一|三个|第二章|foo6|棒6|

      • 对(* sclass_id,subj_id )必须唯一。*
        **教授ID * 可能为NULL。
CREATE TABLE Professor (
    id   int PRIMARY KEY AUTO_INCREMENT,
    
    name varchar(100) NOT NULL
);

CREATE TABLE Subject (
    id   int PRIMARY KEY AUTO_INCREMENT,
    
    name varchar(100) NOT NULL
);

CREATE TABLE ProfessorsSubject (
    prof_id int,
    subj_id int,
    
    PRIMARY KEY (prof_id, subj_id),
    
    FOREIGN KEY (prof_id) REFERENCES Professor(id),
    FOREIGN KEY (subj_id) REFERENCES Subject(id)
);

CREATE TABLE StudentClass (
    id       int PRIMARY KEY AUTO_INCREMENT,
    
    name     varchar(100) NOT NULL,
    building varchar(100)
);

CREATE TABLE A (
   sclass_id  int,
   subj_id    int,
   prof_id    int,
   foo        varchar(10),
   bar        varchar(10),
   
   PRIMARY KEY (sclass_id, subj_id),

   FOREIGN KEY (sclass_id) REFERENCES StudentClass (id),
   FOREIGN KEY (subj_id )  REFERENCES ProfessorsSubject (subj_id),
   FOREIGN KEY (prof_id )  REFERENCES ProfessorsSubject (prof_id)
);

/* ==================================================================== */

INSERT INTO Professor(id, name) VALUES (1, "Mr. Ward");
INSERT INTO Professor(id, name) VALUES (2, "Mr. Smith");

INSERT INTO Subject(id, name) VALUES (1, "Math");
INSERT INTO Subject(id, name) VALUES (2, "Physics");
INSERT INTO Subject(id, name) VALUES (3, "English");

INSERT INTO ProfessorsSubject (prof_id, subj_id) VALUES (1, 1);
INSERT INTO ProfessorsSubject (prof_id, subj_id) VALUES (1, 2);
INSERT INTO ProfessorsSubject (prof_id, subj_id) VALUES (2, 3);

INSERT INTO StudentClass(id, name, building) VALUES (10, "Tenth Grade", "A");
INSERT INTO StudentClass(id, name, building) VALUES (11, "Eleventh Grade", "A");

/* ==================================================================== */

INSERT INTO A (sclass_id, subj_id, prof_id, foo, bar) VALUES (10, 1, 1, "foo1", "bar1");
INSERT INTO A (sclass_id, subj_id, prof_id, foo, bar) VALUES (10, 2, 1, "foo2", "bar2");
INSERT INTO A (sclass_id, subj_id, prof_id, foo, bar) VALUES (10, 3, 1, "foo3", "bar3");

/* ==================================================================== */

Ward先生不应该把英语教到十年级,因为表ProfessorsSubject中没有这个组合。
1.为了避免数据库级别的不一致,如何保证Table A中的对***(prof_id,subj_id)insert之前尊重*"SubjectOfProfessor"table中的内容?
1.1)是否需要使用程序?触发器?
1.2)是否可以只使用字段属性和/或约束来实现这一点?
1.如果我想在数据库级别使用Django ORM来做这个呢?
参考:migrations.RunSQL('ALTER TABLE app_event ADD CONSTRAINT chronology CHECK (start_date > end_date);

b1uwtaje

b1uwtaje1#

您可以在表A中使用组合外键,就像在表ProfessorsSubject中使用主键一样。需要被引用表上的相应索引,而该索引实际上已由其主键实现。

FOREIGN KEY (subj_id, prof_id)  REFERENCES ProfessorsSubject (subj_id, prof_id)

相关问题