复合外键引用2个不同的表

时间:2019-02-02 21:14:29

标签: sql sqlite foreign-keys relational-database

我已经在3个表StudentDepartmentMajor_in

我想在email, Department_ID的(Major_in)上创建一个复合外键,它引用email的主键Student和{{的Department_ID} 1}}。

Department

但是我得到了错误

  

错误:“(”附近:语法错误CREATE TABLE Student ( email VARCHAR(20) PRIMARY KEY ); CREATE TABLE Department ( Department_ID INTEGER PRIMARY KEY ); CREATE TABLE Major_in ( email VARCHAR(20), Department_ID INTEGER, FOREIGN KEY (roll, Department_ID) REFERENCES (Student(email), Department(Department_ID) ); sqlite3`

1 个答案:

答案 0 :(得分:1)

发现这样做是一个基本的语法错误。

CREATE TABLE Major_in
(
    email REFERENCES Student, 
    Department_ID REFERENCES Department, 
    PRIMARY KEY (email, Department_ID)
);