[英]ERROR: there is no unique constraint matching given keys for referenced table
I am getting an error when i create my tabels. 创建表格时出现错误。 The problem is that AssCode is not unique, so i can set it to unique, the combination of courseCode and AssCode is unique, thats why they are set as primary keys. 问题在于,AssCode不是唯一的,因此我可以将其设置为唯一,courseCode和AssCode的组合是唯一的,这就是为什么将它们设置为主键的原因。 I am using postgressql 我正在使用postgressql
here is the error: 这是错误:
ERROR: there is no unique constraint matching given keys for referenced table "assignments" SQL state: 42830 错误:没有唯一约束匹配给定键的引用表“ assignments” SQL状态:42830
here is my code: 这是我的代码:
CREATE TABLE Teachers (
BSN int primary key,
Surname varchar(40) NOT NULL,
Name varchar(40) NOT NULL
);
CREATE TABLE Courses (
CourseCode varchar(10) primary key,
Name varchar(20) NOT NULL
);
CREATE TABLE Assignments (
CourseCode varchar(10) REFERENCES Courses ON DELETE CASCADE,
AssCode varchar(10),
primary key(CourseCode,AssCode),
DependOn varchar(10),
Year date,
week int
);
CREATE TABLE WorkOn (
BSN int REFERENCES Teachers(BSN),
CourseCode varchar(10) REFERENCES Assignments(CourseCode),
AssCode varchar(10) REFERENCES Assignments(AssCode),
primary key (CourseCode,BSN,AssCode)
);
I found the answer: 我找到了答案:
CREATE TABLE WorkOn (
BSN int primary key REFERENCES Teachers(BSN),
CourseCode varchar(10),
AssCode varchar(10),
foreign key (CourseCode, AssCode) references Assignments (CourseCode, AssCode)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.