gpt4 book ai didi

java - Derby:引用来自不同表的多个外键

转载 作者:搜寻专家 更新时间:2023-11-01 02:51:40 24 4
gpt4 key购买 nike

美好的一天!

我一直在 Internet 上搜索我的问题的答案,但一直找不到。如何引用 Derby 数据库中不同表的外键?

这是我当前的 SQL 代码:

CREATE TABLE class_t
(course_id VARCHAR(6) NOT NULL,
semester VARCHAR(6) NOT NULL CONSTRAINT sem_constraint CHECK (semester IN ('1st','2nd','module')),
school_year DATE NOT NULL,
course_name VARCHAR(70) NOT NULL,
CONSTRAINT class_pk PRIMARY KEY (course_id, semester, school_year)
);

CREATE TABLE student_t
(id_number INT NOT NULL,
fullname VARCHAR(35) NOT NULL,
contact_num VARCHAR(35),
email VARCHAR(25),
CONSTRAINT student_pk PRIMARY KEY (id_number)
);

CREATE TABLE student_list
(course_id VARCHAR(6) NOT NULL,
semester VARCHAR(6) NOT NULL CONSTRAINT sem_constraint2 CHECK (semester IN ('1st','2nd','module')),
school_year DATE NOT NULL,
id_number INT NOT NULL,
CONSTRAINT student_list_pk PRIMARY KEY (course_id, semester, school_year, id_number),
CONSTRAINT student_list_FK FOREIGN KEY (course_id, semester, school_year, id_number)
REFERENCES class_t (course_id, semester, school_year), student_t (id_number) #this is my problem
);

非常感谢您的帮助!提前致谢。

最佳答案

没关系,我自己想通了。当我查看 http://db.apache.org/derby/docs/10.2/ref/rrefsqlj13590.html 时找到了解决方案的线索在示例部分。来自不同表的每个外键应该有不同的约束。应该是这样的:

CREATE TABLE student_list
(course_id VARCHAR(6) NOT NULL,
semester VARCHAR(6) NOT NULL CONSTRAINT sem_constraint2 CHECK (semester IN ('1st','2nd','module')),
school_year DATE NOT NULL,
id_number INT NOT NULL,
CONSTRAINT student_list_pk PRIMARY KEY (course_id, semester, school_year, id_number),
CONSTRAINT student_list_fk1 FOREIGN KEY (course_id, semester, school_year)
REFERENCES class_t (course_id, semester, school_year),
CONSTRAINT student_list_fk2 FOREIGN KEY (id_number)
REFERENCES student_t (id_number)
);

感谢您对 stackoverflow.com 社区的所有帮助! :)

关于java - Derby:引用来自不同表的多个外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10116303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com