SQL*加上 ORA-00904 标识符无效 & ORA-00905 缺少关键字(外键)

SQL*Plus ORA-00904 invalid identifier & ORA-00905 missing keyword (foreign key)

我已尝试尽我最大的能力调试此代码,以消除可能导致这些错误发生的小错误,但我在三个 create table 语句中不断收到两个不同的错误。

CREATE TABLE SECTION 语句在指向 course# 的第 7 行给出了一个无效的标识符错误,这是我的代码:

CREATE TABLE SECTION
(SECTION#   VARCHAR2(8) constraint pk_section# primary key,
TIME   CHAR(5),
MAXST   NUMBER(2),
ROOM   VARCHAR2(14),
constraint chk_maxst check(maxst<=35),
constraint fk_crs foreign key(course#)
REFERENCES course(course#),
constraint fk_pro foreign key(empid)
REFERENCES professor(empid));

CREATE TABLE TAKES 语句在指向 section# 的第 4 行给出了一个无效的标识符错误,这是我的代码:

CREATE TABLE TAKES
(GRADE   CHAR(5) constraint nn_grade not null,
constraint chk_grade check(grade IN ('A','B','C')),
constraint fk_sec foreign key(section#)
REFERENCES section (section#),
constraint fk_stu foreign key(sid)
REFERENCES student(sid));

完整上下文:

drop table professor cascade constraints;
drop table course cascade constraints;
drop table student cascade constraints;
drop table section cascade constraints;
drop table takes cascade constraints;

CREATE TABLE PROFESSOR
(NAME  CHAR(15) constraint nn_name not null,
EMPID VARCHAR2(8) constraint pk_empid primary key,
PHONE NUMBER(10),
DATEHIRED DATE,
SALARY NUMBER);

CREATE TABLE COURSE
(NAME   CHAR(24) constraint nn_names not null,
COURSE# CHAR(10) constraint pk_course# primary key,
CREDIT  CHAR(6) constraint nn_credit not null,
COLLEGE CHAR(20),
HRS   NUMBER(1),
constraint chk_credit check(credit IN('U','G')),
constraint chk_college check(college IN ('Arts and Sciences','Education','Engineering','Business')),
constraint chk_course check((credit='U' AND hrs<=4) OR (credit = 'G' AND hrs=3)),
constraint unq_course unique(name, college));

CREATE TABLE STUDENT
(SID   VARCHAR2(7) constraint pk_sid primary key,
NAME    CHAR(14),
ADDRESS  CHAR(22),
BIRTHDATE DATE,
GRADELEVEL CHAR(2) constraint nn_glvl not null);

CREATE TABLE SECTION
(SECTION#   VARCHAR2(8) constraint pk_section# primary key,
TIME   CHAR(5),
MAXST   NUMBER(2),
ROOM   VARCHAR2(14),
constraint chk_maxst check(maxst<=35),
constraint fk_crs foreign key(course#)
REFERENCES course(course#),
constraint fk_pro foreign key(empid)
REFERENCES professor(empid));

CREATE TABLE TAKES
(GRADE   CHAR(5) constraint nn_grade not null,
constraint chk_grade check(grade IN ('A','B','C')),
constraint fk_sec foreign key(section#)
REFERENCES section (section#),
constraint fk_stu foreign key(sid)
REFERENCES student(sid));

Textbook references:
[https://drive.google.com/open?id=1eDdBShzgnSjISqxByJ7FKgbkLCEwXzpd][1]
[https://drive.google.com/open?id=1WhDsgQy2xSwjxVMqDzaGOcBh7zSokneT][2]
[https://drive.google.com/open?id=12N51OCEucRn_unagqHYsqufEGK3tKJH_][3]

您是否遵循描述您应该如何做您正在做的事情的文档?因为,看起来你在 类 时并没有太注意,也没有阅读文档并且倾向于 编造 事情,做一些错误或从未做过的事情存在。

考虑删除所有乱七八糟的东西并重新开始。

这里有一些指南;尝试修复这些错误,如果仍然无效,请回来。

课程中 table:

  • 不要使用 CHAR 而是 VARCHAR2 数据类型
  • 您不能在不存在的列上创建约束(例如,在 CRS_CREDIT 列上创建检查约束,而您将列命名为 CREDIT)

部分 table:

  • 不要使用 CHAR 数据类型
  • 您不能使用 SECTION table(例如 SEC_CRS_COURSE#)中不存在的列(在 FOREIGN KEY 约束中),也不在 table 您参考(例如课程中的CRS_COURSE#table)
  • Oracle 中没有 ON UPDATE CASCADE 和 ON DELETE RESTRICT

TAKES table.

也是如此

[编辑,在你几乎让它工作之后]

恭喜!你现在已经很接近了! SECTION 和 TAKES table 需要一些调整(缺少列 - 看一下,我用注释标记了它们)然后成功创建了 tables。

再一次(因为你不会听):摆脱 CHAR 数据类型列 - 使用 VARCHAR2 代替。

SQL> CREATE TABLE PROFESSOR
  2  (
  3     NAME        CHAR (15) CONSTRAINT nn_name NOT NULL,
  4     EMPID       VARCHAR2 (8) CONSTRAINT pk_empid PRIMARY KEY,
  5     PHONE       NUMBER (10),
  6     DATEHIRED   DATE,
  7     SALARY      NUMBER
  8  );

Table created.

SQL>
SQL> CREATE TABLE COURSE
  2  (
  3     NAME      CHAR (24) CONSTRAINT nn_names NOT NULL,
  4     COURSE#   CHAR (10) CONSTRAINT pk_course# PRIMARY KEY,
  5     CREDIT    CHAR (6) CONSTRAINT nn_credit NOT NULL,
  6     COLLEGE   CHAR (20),
  7     HRS       NUMBER (1),
  8     CONSTRAINT chk_credit CHECK (credit IN ('U', 'G')),
  9     CONSTRAINT chk_college CHECK
 10        (college IN ('Arts and Sciences',
 11                     'Education',
 12                     'Engineering',
 13                     'Business')),
 14     CONSTRAINT chk_course CHECK
 15        ( (credit = 'U' AND hrs <= 4) OR (credit = 'G' AND hrs = 3)),
 16     CONSTRAINT unq_course UNIQUE (name, college)
 17  );

Table created.

SQL>
SQL> CREATE TABLE STUDENT
  2  (
  3     SID          VARCHAR2 (7) CONSTRAINT pk_sid PRIMARY KEY,
  4     NAME         CHAR (14),
  5     ADDRESS      CHAR (22),
  6     BIRTHDATE    DATE,
  7     GRADELEVEL   CHAR (2) CONSTRAINT nn_glvl NOT NULL
  8  );

Table created.

SQL>
SQL> CREATE TABLE SECTION
  2  (
  3     SECTION#   VARCHAR2 (8) CONSTRAINT pk_section# PRIMARY KEY,
  4     TIME       CHAR (5),
  5     MAXST      NUMBER (2),
  6     ROOM       VARCHAR2 (14),
  7     course#    CHAR (10),                                        -- added by LF
  8     empid      VARCHAR2 (8),                                     -- added by LF
  9     CONSTRAINT chk_maxst CHECK (maxst <= 35),
 10     CONSTRAINT fk_crs FOREIGN KEY (course#) REFERENCES course (course#),
 11     CONSTRAINT fk_pro FOREIGN KEY (empid) REFERENCES professor (empid)
 12  );

Table created.

SQL>
SQL> CREATE TABLE TAKES
  2  (
  3     GRADE      CHAR (5) CONSTRAINT nn_grade NOT NULL,
  4     section#   VARCHAR2 (8),                                     -- added by LF
  5     sid        VARCHAR2 (7),                                     -- added by LF
  6     CONSTRAINT chk_grade CHECK (grade IN ('A', 'B', 'C')),
  7     CONSTRAINT fk_sec FOREIGN KEY (section#) REFERENCES section (section#),
  8     CONSTRAINT fk_stu FOREIGN KEY (sid) REFERENCES student (sid)
  9  );

Table created.

SQL>