sql外键错误
我想创建这两个表,但出现错误,指出 HOLD 外键引用的 SECTION 键不是主键。正如在查询中看到的,它们是主键。我该如何解决这个问题?
create table SECTION(
ID integer foreign key references TERM(ID),
CID integer foreign key references COURSE(CID),
SECT integer,
constraint PK_SECTION primary key (ID,CID,SECT),
);
create table HOLD(
NAME varchar(30) foreign key references INSTRUCTOR(NAME),
ID integer foreign key references SECTION(ID),
CID integer foreign key references SECTION(CID),
SECT integer foreign key references SECTION(SECT),
constraint PK_HOLD primary key (NAME,ID,CID,SECT),
);
i want to create these two tables but i'm getting an error which says SECTION keys that being referenced by foreign keys of HOLD are not primary key. as it's seen in the query they are primary keys. how can i solve this problem?
create table SECTION(
ID integer foreign key references TERM(ID),
CID integer foreign key references COURSE(CID),
SECT integer,
constraint PK_SECTION primary key (ID,CID,SECT),
);
create table HOLD(
NAME varchar(30) foreign key references INSTRUCTOR(NAME),
ID integer foreign key references SECTION(ID),
CID integer foreign key references SECTION(CID),
SECT integer foreign key references SECTION(SECT),
constraint PK_HOLD primary key (NAME,ID,CID,SECT),
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ID、CID、SECT 不是主键,它们一起是一个主键(多个属性)。您必须像这样引用它们:
ID,CID,SECT are not primary keys, they all together are one primary key (of multiple attributes). You have to reference them as one like this:
通过使用这个语句
约束 PK_SECTION 主键(ID、CID、SECT)
您仅在主键上进行操作并通过以下语句
ID 整数外键引用 TERM(ID),
CID 整数外键引用 COURSE(CID)
您正在尝试创建两个外键,但问题是 ID 和 CID 不是主键,组合是主键(ID、CID、SECT)
要纠正此问题,您需要创建单独的主键 ID 和 CID
或
约束“FK”外键(“ID”、“CID”)
参考文献“部分”(“ID”、“CID”)
By using this statement
constraint PK_SECTION primary key (ID,CID,SECT)
you are making only on primary key and by the following statement
ID integer foreign key references TERM(ID),
CID integer foreign key references COURSE(CID)
you are trying to make two foreign keys but the thing is ID and CID are not primary key there combination is a primary key(ID,CID,SECT)
To rectify this you need to create Individual primary key ID and CID
Or
CONSTRAINT "FK" FOREIGN KEY ("ID", "CID")
REFERENCES "SECTION" ("ID", "CID")