sql外键错误

发布于 2024-12-10 05:40:59 字数 564 浏览 0 评论 0原文

我想创建这两个表,但出现错误,指出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

预谋 2024-12-17 05:40:59

ID、CID、SECT 不是主键,它们一起是一个主键(多个属性)。您必须像这样引用它们:

CONSTRAINT "HOLD_FK01" FOREIGN KEY ("ID", "CID", "SECT")
      REFERENCES "SECTION" ("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:

CONSTRAINT "HOLD_FK01" FOREIGN KEY ("ID", "CID", "SECT")
      REFERENCES "SECTION" ("ID", "CID", "SECT")
世界如花海般美丽 2024-12-17 05:40:59

通过使用这个语句
约束 PK_SECTION 主键(ID、CID、SECT)
您仅在主键上进行操作并通过以下语句
ID 整数外键引用 TERM(ID),
CID 整数外键引用 COURSE(CID)
您正在尝试创建两个外键,但问题是 ID 和 CID 不是主键,组合是主键(ID、CID、SECT)

要纠正此问题,您需要创建单独的主键 ID 和 CID

create table SECTION( 
ID integer foreign key references TERM(ID),
CID integer foreign key references COURSE(CID),
SECT integer,
constraint PK_SECTION_ID primary key (ID),
constraint PK_SECTION_SECT primary key (SECT),
constraint PK_SECTION_CID primary key (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

create table SECTION( 
ID integer foreign key references TERM(ID),
CID integer foreign key references COURSE(CID),
SECT integer,
constraint PK_SECTION_ID primary key (ID),
constraint PK_SECTION_SECT primary key (SECT),
constraint PK_SECTION_CID primary key (CID)
);

Or

CONSTRAINT "FK" FOREIGN KEY ("ID", "CID")
REFERENCES "SECTION" ("ID", "CID")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文