我可以在SQL中创建同一列的外键和主键吗?

发布于 2025-01-11 21:44:59 字数 884 浏览 2 评论 0原文

我想从(STDNO 和 CORSNO)列为 Enrollment 表创建一个复合主键,它们也是 Student 和 Course 表中的外键,但它一直给我错误。另外,学生表中也发生了同样的事情,我需要让每个学生属于一个部门。

这就是我所做的:

CREATE TABLE Student
(
    STDNO number(8),
    SNAME varchar2(30) NOT NULL,
    DEPTNO varchar(2),

    CONSTRAINT PK_Studnet PRIMARY KEY(STDNO,DEPTNO),
    FOREIGN KEY(DEPTNO) REFERENCES Department(DEPTNO)
);

CREATE TABLE Department
(
    DEPTNO varchar2(2) PRIMARY KEY,
    DNAME varchar2(20)
);

CREATE TABLE Course
(
    CORSNO number(3) PRIMARY KEY,
    CNAME varchar2(30),
    DETNO varchar2(30),
    CMAX number(2)
);

CREATE TABLE Enrolment
(
    STDNO number(8),
    CORSNO number(3),
    GRADE number(2),
    EDATE date date default CURRENT_TIMESTAMP,
    CONSTRAINT PK_Enrolment PRIMARY KEY (STDNO, CORSNO),
    FOREIGN KEY(STDNO) REFERENCES Student(STDNO),
    FOREIGN KEY(CORSNO) REFERENCES Course(CORSNO)
);

I want to create a composite primary key for the Enrolment table from the (STDNO & CORSNO) columns which they are also a foreign key from Student and Course tables but it keeps giving me errors. also, the same thing happened in student table I need to let each student belong to a department.

Here's what I did:

CREATE TABLE Student
(
    STDNO number(8),
    SNAME varchar2(30) NOT NULL,
    DEPTNO varchar(2),

    CONSTRAINT PK_Studnet PRIMARY KEY(STDNO,DEPTNO),
    FOREIGN KEY(DEPTNO) REFERENCES Department(DEPTNO)
);

CREATE TABLE Department
(
    DEPTNO varchar2(2) PRIMARY KEY,
    DNAME varchar2(20)
);

CREATE TABLE Course
(
    CORSNO number(3) PRIMARY KEY,
    CNAME varchar2(30),
    DETNO varchar2(30),
    CMAX number(2)
);

CREATE TABLE Enrolment
(
    STDNO number(8),
    CORSNO number(3),
    GRADE number(2),
    EDATE date date default CURRENT_TIMESTAMP,
    CONSTRAINT PK_Enrolment PRIMARY KEY (STDNO, CORSNO),
    FOREIGN KEY(STDNO) REFERENCES Student(STDNO),
    FOREIGN KEY(CORSNO) REFERENCES Course(CORSNO)
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

吾性傲以野 2025-01-18 21:44:59

PRIMARY KEY 应该唯一地标识它所代表的事物;对于学生,我们可以假设学生编号对于该学生来说应该是唯一的,并且本身应该是主键(因为假设您不会向两个学生颁发相同的学生编号)不同部门)。

如果您更改代码,使 STUDNO 单独成为 PRIMARY KEY 并重新排列表的顺序,以便在引用它们的表之前创建引用的表,然后您会得到:

CREATE TABLE Department
(
    DEPTNO varchar2(2) PRIMARY KEY,
    DNAME varchar2(20)
);

CREATE TABLE Course
(
    CORSNO number(3) PRIMARY KEY,
    CNAME varchar2(30),
    DETNO varchar2(30),
    CMAX number(2)
);

CREATE TABLE Student
(
    STDNO number(8),
    SNAME varchar2(30) NOT NULL,
    DEPTNO varchar(2),

    CONSTRAINT PK_Studnet PRIMARY KEY(STDNO),
    FOREIGN KEY(DEPTNO) REFERENCES Department(DEPTNO)
);

CREATE TABLE Enrolment
(
    STDNO number(8),
    CORSNO number(3),
    GRADE number(2),
    EDATE date default CURRENT_TIMESTAMP,
    CONSTRAINT PK_Enrolment PRIMARY KEY (STDNO, CORSNO),
    FOREIGN KEY(STDNO) REFERENCES Student(STDNO),
    FOREIGN KEY(CORSNO) REFERENCES Course(CORSNO)
);

(并修复 Enrolment 表中 date date 的拼写错误。)

然后是 Enrolment 中的约束代码>表工作,因为每个引用约束引用了一些独特的东西。之前,您曾说过学生编号和部门的组合是唯一的,但我们试图仅引用学生编号,而学生编号本身并不唯一。

db<>fiddle 此处

A PRIMARY KEY should uniquely identify a thing it represents; in the case of a Student, we can assume that the student number should be unique to that student and should be the primary key by itself (as, it is assumed that, you are not going to issue the same student number to two students in different departments).

If you change your code so that STUDNO alone is the PRIMARY KEY and rearrange the orders of the tables so that the referenced tables are created before the tables that references them then you get:

CREATE TABLE Department
(
    DEPTNO varchar2(2) PRIMARY KEY,
    DNAME varchar2(20)
);

CREATE TABLE Course
(
    CORSNO number(3) PRIMARY KEY,
    CNAME varchar2(30),
    DETNO varchar2(30),
    CMAX number(2)
);

CREATE TABLE Student
(
    STDNO number(8),
    SNAME varchar2(30) NOT NULL,
    DEPTNO varchar(2),

    CONSTRAINT PK_Studnet PRIMARY KEY(STDNO),
    FOREIGN KEY(DEPTNO) REFERENCES Department(DEPTNO)
);

CREATE TABLE Enrolment
(
    STDNO number(8),
    CORSNO number(3),
    GRADE number(2),
    EDATE date default CURRENT_TIMESTAMP,
    CONSTRAINT PK_Enrolment PRIMARY KEY (STDNO, CORSNO),
    FOREIGN KEY(STDNO) REFERENCES Student(STDNO),
    FOREIGN KEY(CORSNO) REFERENCES Course(CORSNO)
);

(And also fix the typo where you have date date in the Enrolment table.)

Then the constraints in the Enrolment table work because each of the referential constraints references something unique. Before, you had said that the combination of both student number and department together was unique but were trying to reference just the student number which, by itself, was not unique.

db<>fiddle here

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