[英]Derby: Referencing Multiple Foreign Keys From Different Tables
Good day! 美好的一天!
I have been searching the Internet for an answer to my problem but I have not been able to get one. 我一直在网上搜索我的问题的答案,但我没能得到一个。 How do I reference foreign keys from different tables in a Derby database?
如何从Derby数据库中的不同表引用外键?
This is my current SQL code: 这是我目前的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
);
Your help is very much appreciated! 非常感激您的帮忙! Thanks in advance.
提前致谢。
Nevermind, I've figured it out myself. 没关系,我自己弄清楚了。 Had a clue at the solution when I looked at http://db.apache.org/derby/docs/10.2/ref/rrefsqlj13590.html in the examples part.
当我在示例部分中查看http://db.apache.org/derby/docs/10.2/ref/rrefsqlj13590.html时,对解决方案有了一定的了解。 There should be different constraints for each foreign keys from different tables.
对于来自不同表的每个外键,应该有不同的约束。 It should be like this:
它应该是这样的:
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)
);
Thanks for all your help stackoverflow.com community! 感谢您的帮助stackoverflow.com社区! :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.