[英]Assigning primary and foreign key in multiple tables oracle
I just started a class in Oracle SQL and we were given an assignment to create 3 entity and 2 relation tables. 我刚刚开始使用Oracle SQL进行授课,并获得了创建3个实体表和2个关系表的任务。 He gave us these fields and said add as we feel necessary:
他给了我们这些字段,并在我们认为必要时说:
Course: department, semester, maximumEnrolled
Instructor: firstName, lastName, ssn
Room: roomNumber, building
Teaches: instructor, course
Uses: course, room
When I created the tables, only the first 4 worked but I am not able to create the uses table. 创建表时,只有前4个起作用,但无法创建uses表。 The error is:
错误是:
ORA-02270: no matching unique or primary key for this column-list.
ORA-02270:此列列表没有匹配的唯一键或主键。
This is what I did: 这是我所做的:
CREATE TABLE course
(course_id NUMBER(4) PRIMARY KEY,
courseName VARCHAR2(30) NOT NULL,
department VARCHAR2(50),
semester VARCHAR2(10),
maximumEnrolled NUMBER(36) NOT NULL);
CREATE TABLE room
(roomNo NUMBER(5) PRIMARY KEY,
building VARCHAR2(10));
CREATE TABLE instructor
(instructor_id NUMBER(6) PRIMARY KEY,
firstName VARCHAR2(20),
lastName VARCHAR2(30) NOT NULL
ssn NUMBER(9) NOT NULL);
CREATE TABLE teaches
(instructor_id NUMBER(5) NOT NULL,
course_id NUMBER(4) NOT NULL,
teachingCourse VARCHAR2(4) PRIMARY KEY,
CONSTRAINT instructor_fk FOREIGN KEY (instructor_id) REFERENCES instructor (instructor_id)
CONSTRAINT course_fk FOREIGN KEY (course_id) REFERENCES course (course_id));
CREATE TABLE uses
(course_id NUMBER(4) NOT NULL,
roomNo NUMBER(5) NOT NULL,
roomUse VARCHAR2(4) PRIMARY KEY,
CONSTRAINT course_fk FOREIGN KEY (course_id) REFERENCES course (course_id)
CONSTRAINT room_fk FOREIGN KEY (roomNo) REFERENCES room (roomNo));
I thought maybe the problem is that I used course_id as a FK in the teaches table so I tried: 我以为可能是我在示教表中将course_id用作FK,所以尝试了:
CREATE TABLE uses
(roomUsage VARCHAR2(30) PRIMARY KEY,
roomNo NUMBER(5) NOT NULL,
courseName VARCHAR2(30) NOT NULL,
CONSTRAINT room_fk FOREIGN KEY (roomNo) REFERENCES room (roomNo),
CONSTRAINT course_fk FOREIGN KEY (courseName) REFERENCES course (courseName));
But it didn't work either and gave the same error. 但是它也不起作用,并给出了相同的错误。 What am I doing wrong?
我究竟做错了什么?
There are some commas missing in some of your table definitions (after lastname
in instructor
, after the first fk in the two last tables) and also a data type mismatch (in size) for the instructor_id
in teaches
. 有一些逗号你的一些表定义的缺失(后
lastname
的instructor
,最后两个表中的第一FK后),也是一种数据类型不匹配(大小)的instructor_id
的teaches
。 Also, foreign key constraint names must be unique - you are reusing course_fk
in the uses
table - rename it. 另外,外键约束名称必须唯一-您正在
uses
表中重用course_fk
将其重命名。
The corrected code would be this: 正确的代码是这样的:
CREATE TABLE course
(course_id NUMBER(4) PRIMARY KEY,
courseName VARCHAR2(30) NOT NULL,
department VARCHAR2(50),
semester VARCHAR2(10),
maximumEnrolled NUMBER(36) NOT NULL
);
CREATE TABLE room
(roomNo NUMBER(5) PRIMARY KEY,
building VARCHAR2(10));
CREATE TABLE instructor
(instructor_id NUMBER(6) PRIMARY KEY,
firstName VARCHAR2(20),
lastName VARCHAR2(30) NOT NULL,
ssn NUMBER(9) NOT NULL
);
CREATE TABLE teaches
(instructor_id NUMBER(6) NOT NULL,
course_id NUMBER(4) NOT NULL,
teachingCourse VARCHAR2(4) PRIMARY KEY,
CONSTRAINT instructor_fk FOREIGN KEY (instructor_id) REFERENCES instructor (instructor_id),
CONSTRAINT course_fk FOREIGN KEY (course_id) REFERENCES course (course_id)
);
CREATE TABLE uses
(course_id NUMBER(4) NOT NULL,
roomNo NUMBER(5) NOT NULL,
roomUse VARCHAR2(4) PRIMARY KEY,
CONSTRAINT course_fk2 FOREIGN KEY (course_id) REFERENCES course (course_id),
CONSTRAINT room_fk FOREIGN KEY (roomNo) REFERENCES room (roomNo)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.