简体   繁体   English

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

[英]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.

相关问题 无论如何,是否要使用来自2个不同表的2个不同外键将数据插入表中? - Is there anyway to insert data to table with 2 different foreign keys from 2 different tables? 在JPA 2.0中从没有外键的多个表中获取数据 - Getting data from multiple tables without foreign keys in JPA 2.0 将一个表中的多个外键引用到另一个表中的相同值并读取该值 - Referencing multiple foreign keys from one table to the same value in another table and reading the value 如何在 Apache Derby 中声明外键? - How to declare foreign keys in Apache Derby? Hibernate在数据库表中创建多个外键以进行双向关联 - Hibernate creates multiple foreign keys in database tables for bidirectional associations 使用JDBC批量插入具有外键的多个表 - Batch insert to multiple tables with foreign keys using JDBC 使用Criteria和Metamodel API在多个外键上联接两个表 - Join two tables on multiple foreign keys using Criteria and Metamodel APIs 如何获取derby数据库中的外键列表 - How do I get a list of the foreign keys in a derby database apache derby 中的外键会自动填充列吗? - Do foreign keys in apache derby automatically populate a column? 查询两个表中的外键 - Query the foreign keys in the two tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM