尝试使用 Oracle 为作业创建表,但我看不到语法的问题。任何帮助都非常感谢



这是代码:

create table instructor (
id varchar(10) primary key,
name varchar(15) not null,
dept_name varchar(20) not null
references department(dept_name) on delete cascade,
salary varchar(20) not null
);
create table teaches (
id varchar(10) primary key
references instructor(id) on delete cascade,
course_id varchar(15) not null check
references section(course_id),
sec_id varchar(5) not null
references section(sec_id),
semester varchar(15)
references section(semester),
year int not null
references section(year)
);
create table student (
id varchar(10) not null,
name varchar(20) not null,
dept_name varchar(15) not null
references department(dept_id) on delete cascade,
tot_cred int not null
);
create table advisor (
s_id  varchar(10) primary key
references student(id) on delete cascade,
i_id varchar(6)
references instructor(id) on delete cascade
);
create table department (
dept_name varchar(15) primary key,
building varchar(10),
budget int
);
create table course (
course_id varchar(10) primary key,
title varchar(10),
dept_name varchar(15)
references department(dept_name) on delete cascade,
credits int
);
create table prereq (
course_id varchar(10) 
references course(course_id) on delete cascade,
prereq_id varchar(10) 
references course(course_id) on delete cascade,
primary key (course_id, prereq_id)
);
create table takes (
id varchar(10) not null
references student(id) on delete cascade,
course_id varchar(10) not null
references section(course_id) on delete cascade,
sec_id varchar(10) not null
references section(sec_id) on delete cascade,
semester varchar(10) not null
references section(semester) on delete cascade,
year int not null
references section(year) on delete cascade,
grade varchar(10) not null
primary key (id, course_id, sec_id, semester, year, grade)
);
create table section (
course_id varchar(10) not null
references course(course_id),
sec_id varchar(10) not null, 
semester varchar(10) not null,
year varchar(10) not null,
building varchar(10) not null
references classroom(building),
room_no varchar(5) not null
references classroom(room_no),
time_slot_id varchar(5) not null
references time_slot(time_slot_id),
primary key (course_id, sec_id, semester, year)
);
create table classroom (
building varchar(10) not null,
room_no varchar(5) not null,
capacity varchar(10) not null,
primary key (building, room_no)
);
create table time_slot (
time_slot_id varchar(5) not null
references section(time_slot_id),
day varchar(10) not null,
start_time varchar(10) not null,
end_time varchar(10) not null,
primary key (time_slot_id, day, start_time)
);

这是输出:

已创建表。

ORA-00906:缺少左括号

ORA-00904:"DEPT_ID":标识符无效

ORA-00942:表或视图不存在

ORA-00955:名称已被现有对象使用

已创建表。

已创建表。

ORA-00907:缺少右括号

ORA-02270:此列列表没有匹配的唯一键或主键

ORA-00955:名称已被现有对象使用

ORA-00942:表或视图不存在

  • student中,外键应该引用department.dept_name而不是department.dept_id
  • student中没有主键,因此引用student的所有其他表都将失败。
  • time_slot中,您有一个引用约束,该约束引用sectiontime_slot又引用回同一列,因此您有循环约束;您可能希望从time_slot中删除foreign key约束。
  • time_slot中,你可能只希望primary key只对time_slot_id,对time_slot_iddaystart_time进行复合unique约束。
  • 切勿将日期和时间存储为字符串;始终使用DATE数据类型(在 Oracle 中,它可以存储日期和时间)。如果在time_slot中存储日期和时间,则只需使用两列DATE列进行start_timeend_time。如果要存储星期几和一天中的时间,则可以使用NUMBER(1,0)表示星期几,INTERVAL DAY(0) TO SECOND(0)表示开始和结束时间(或者不使用日列,而只使用两种INTERVAL DAY(1) TO SECOND (0)数据类型表示开始和结束时间,并从一周开始开始测量)。
  • section中,分别引用classroom.buildingclassroom.room;但是,这是一个复合主键,应该是单个引用约束。
  • teaches中,有一个随机的check关键字应该被删除。
  • teachestakes中,course_idsec_idsemesteryear是一个需要一起引用的组合键。
  • 当列是引用约束的一部分时,数据类型是可选的。如果删除数据类型,则它将使用父表的数据类型,并确保表之间的一致性。
  • 您需要对表重新排序,以便在引用它们的外键之前创建主键。
  • salarycapacityyear应该是数值数据类型。

db<>小提琴在这里

最新更新