我可以在SQL中创建同一列的外键和主键吗?
我想从(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PRIMARY KEY
应该唯一地标识它所代表的事物;对于学生,我们可以假设学生编号对于该学生来说应该是唯一的,并且本身应该是主键(因为假设您不会向两个学生颁发相同的学生编号)不同部门)。如果您更改代码,使
STUDNO
单独成为PRIMARY KEY
并重新排列表的顺序,以便在引用它们的表之前创建引用的表,然后您会得到:(并修复
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 thePRIMARY KEY
and rearrange the orders of the tables so that the referenced tables are created before the tables that references them then you get:(And also fix the typo where you have
date date
in theEnrolment
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