1

我正在我的数据科学学位课程中学习 SQL,任务是第一次执行 SQL 查询。为此,我必须将 SQL 起始代码粘贴进去并运行它。但是,当我这样做时,我遇到了一个我无法解决的错误。我的课程在线,我的教授没有回复,由于这不是作业评分部分的一部分,我想我会尝试在这里寻求有关此错误的帮助。

编辑:有人指出我没有包括所有表格。请查看所有表格和所有数据的完整代码。我试图避免用大量代码行淹没这篇文章,但似乎我遗漏了相关信息。见下文。

这些表是使用以下代码块创建的:

create table section (
    course_id   varchar(8),
    sec_id          varchar(8),
    semester    varchar(6) check (semester in ('Fall', 'Winter', 'Spring', 'Summer')),
    year    numeric(4,0) check (year >1701 and year < 2100),
    building     varchar(15),
    room_number     varchar(7),
    time_slot_id    varchar(4),
    primary key (course_id, sec_id, semester, year),
    foreign key (course_id) references course on delete cascade,
    foreign key (time_slot_id) references timeslot on delete no action,
    foreign key (building, room_number) references classroom on delete set null
);

create table teaches (
    ID      varchar(5),
    course_id   varchar(8),
    sec_id      varchar(8),
    semester    varchar(6),
    year    numeric(4,0),
    primary key (ID, course_id, sec_id, semester, year),
    foreign key (course_id, sec_id, semester, year) references section on delete cascade,
    foreign key (ID) references instructor on delete set null
);

create table instructor (
    ID varchar(5),
    name varchar(20) not null,
    dept_name varchar(20),
    salary  numeric(8,2) check (salary >10000),
    primary key (ID),
    foreign key (dept_name) references department on delete set null 
);

然后,我将数据添加到讲师和部分,如下所示。旁注,我必须进入并手动将 time_slot_id 设置为主键以避免出错。

INSERT INTO classroom VALUES ('Innovation Hall', '101', 10);
INSERT INTO classroom VALUES ('Innovation Hall', '327', 30);
INSERT INTO classroom VALUES ('Exploratory Hall', '202', 20);
INSERT INTO classroom VALUES ('Research Hall', '302', 20);
INSERT INTO classroom VALUES ('Arts Center', '212B', 100);
INSERT INTO classroom VALUES ('Research Hall', '310', 30);
INSERT INTO classroom VALUES ('Innovation Hall', '121', 10);
INSERT INTO classroom VALUES ('Innovation Hall', '321', 20);
INSERT INTO classroom VALUES ('Arts Center', '120A', 100);
INSERT INTO classroom VALUES ('Exploratory Hall', '231', 20);
INSERT INTO classroom VALUES ('Innovation Hall', '220', 30);

INSERT INTO department VALUES ('CDS', 'Research Hall', 100000);
INSERT INTO department VALUES ('Music', 'Arts Center', 70000);
INSERT INTO department VALUES ('History', 'Arts Center', 50000);
INSERT INTO department VALUES ('Geography', 'Exploratory Hall', 60000);
INSERT INTO department VALUES ('Biology', 'Exploratory Hall', 85000);
INSERT INTO department VALUES ('Physics', 'Innovation Hall', 90000);
INSERT INTO department VALUES ('Math', 'Exploratory Hall', 55000);

INSERT INTO course VALUES ('CDS-101', 'Intro to CDS', 'CDS', 3);
INSERT INTO course VALUES ('CDS-130', 'Computing for Scientists', 'CDS', 3);
INSERT INTO course VALUES ('CDS-302', 'Databases', 'CDS', 3);
INSERT INTO course VALUES ('CDS-303', 'Data Mining', 'CDS', 3);
INSERT INTO course VALUES ('MUS-100', 'Fundamentals of Music', 'Music', 3);
INSERT INTO course VALUES ('HIS-101', 'History of Western Civilization', 'History', 3);
INSERT INTO course VALUES ('GGS-101', 'Major World Regions', 'Geography', 4);
INSERT INTO course VALUES ('BIO-101', 'Intro to Biology', 'Biology', 3);
INSERT INTO course VALUES ('BIO-301', 'Genetics', 'Biology', 4);
INSERT INTO course VALUES ('PHY-101', 'Intro to Physics', 'Physics', 3);
INSERT INTO course VALUES ('PHY-403', 'Quantum Mechanics', 'Physics', 4);
INSERT INTO course VALUES ('MAT-110', 'Probabilities', 'Math', 3);
INSERT INTO course VALUES ('MAT-114', 'Calculus', 'Math', 4);

INSERT INTO prereq VALUES ('CDS-130', 'CDS-101');
INSERT INTO prereq VALUES ('MAT-114', 'MAT-110');
INSERT INTO prereq VALUES ('PHY-403', 'PHY-101');
INSERT INTO prereq VALUES ('CDS-302', 'CDS-130');
INSERT INTO prereq VALUES ('CDS-302', 'PHY-403');
INSERT INTO prereq VALUES ('CDS-303', 'CDS-302');
INSERT INTO prereq VALUES ('CDS-303', 'MAT-114');

INSERT INTO instructor VALUES ('11111', 'Turing', 'CDS', 950000);
INSERT INTO instructor VALUES ('11112', 'Widom', 'CDS', 100000);
INSERT INTO instructor VALUES ('12121', 'Mozart', 'Music', 75000);
INSERT INTO instructor VALUES ('22222', 'Heraclitus', 'History', 65000);
INSERT INTO instructor VALUES ('12345', 'Eratosthenes', 'Geography', 55000);
INSERT INTO instructor VALUES ('54321', 'Crick', 'Biology', 88000);
INSERT INTO instructor VALUES ('33333', 'Newton', 'Physics', 80000);
INSERT INTO instructor VALUES ('44444', 'Euler', 'Math', 77000);
INSERT INTO instructor VALUES ('55555', 'Euclid', 'Math', 77000);

INSERT INTO timeslot VALUES ('A', 'M', 8, 0 , 9, 15);
INSERT INTO timeslot VALUES ('B', 'M', 10, 0 , 11, 15);
INSERT INTO timeslot VALUES ('C', 'T', 8, 0 , 9, 15);
INSERT INTO timeslot VALUES ('D', 'T', 10, 0 , 11, 15);
INSERT INTO timeslot VALUES ('E', 'T', 11, 15 , 12, 30);
INSERT INTO timeslot VALUES ('F', 'W', 9, 0 , 10, 15);
INSERT INTO timeslot VALUES ('G', 'R', 8, 0 , 10, 15);
INSERT INTO timeslot VALUES ('H', 'R', 15, 0 , 16, 15);
INSERT INTO timeslot VALUES ('I', 'F', 9, 0 , 10, 15);

INSERT INTO section VALUES ('CDS-101', '1', 'Fall', 2019, 'Research Hall', '310', 'A');
INSERT INTO section VALUES ('CDS-130', '1', 'Fall', 2019, 'Innovation Hall', '121', 'B');
INSERT INTO section VALUES ('CDS-302', '1', 'Fall', 2019, 'Innovation Hall', '327', 'C');
INSERT INTO section VALUES ('CDS-302', '2', 'Spring', 2019, 'Innovation Hall', '321', 'C');
INSERT INTO section VALUES ('MUS-100', '1', 'Fall', 2019, 'Arts Center', '120A', 'A');
INSERT INTO section VALUES ('HIS-101', '1', 'Fall', 2019, 'Arts Center', '212B', 'B');
INSERT INTO section VALUES ('GGS-101', '1', 'Fall', 2019, 'Exploratory Hall', '202', 'D');
INSERT INTO section VALUES ('BIO-101', '2', 'Fall', 2019, 'Exploratory Hall', '231', 'E');
INSERT INTO section VALUES ('PHY-403', '1', 'Spring', 2019, 'Innovation Hall', '220', 'F');
INSERT INTO section VALUES ('BIO-101', '2', 'Fall', 2020, 'Exploratory Hall', '231', 'C');
INSERT INTO section VALUES ('PHY-403', '1', 'Spring', 2020, 'Innovation Hall', '220', 'D');

我没有问题。最后,我尝试运行它:

INSERT INTO teaches VALUES ('11111', 'CDS-101', '1', 'Fall', 2019);
INSERT INTO teaches VALUES ('11112', 'CDS-130', '1', 'Fall', 2019);
INSERT INTO teaches VALUES ('12121', 'MUS-100', '1', 'Fall', 2019);
INSERT INTO teaches VALUES ('54321', 'BIO-101', '2', 'Fall', 2019);
INSERT INTO teaches VALUES ('33333', 'PHY-403', '1', 'Spring', 2019);
INSERT INTO teaches VALUES ('44444', 'MAT-114', '2', 'Fall', 2019);

错误发生在最后一行。我总是得到:

结果:FOREIGN KEY 约束失败
在第 1 行:(注意:我之后尝试自行重新运行它,因此“第 1 行”)
INSERT INTO 教导 VALUES ('44444', 'MAT-114', '2', '秋季”,2019 年);

我检查了每个约束,查看了每一列,但无法弄清楚。我什至在 DB Browser Lite 的“浏览数据”选项卡中手动输入数据;虽然我能够输入前 4 个值,但当我尝试输入年份的任何内容时,我在弹出框中得到了同样的错误。即使我单击该框,将其留空,然后尝试单击退出,我也会收到相同的错误。见图片。

请协助...我在这一点上迷路并且非常沮丧。

错误信息

4

1 回答 1

2

这里有两件事:

  1. 问题,以及
  2. 如何解决问题所在。

让我们从 2) 开始。

错误信息是:

结果:FOREIGN KEY 约束失败
在第 1 行:
INSERT INTO 教导 VALUES ('44444', 'MAT-114', '2', 'Fall', 2019);

这告诉我们查看teaches表上定义的外键。

有2个:

  • ID 引用了讲师表中的 ID(除此之外:可能调用此讲师 ID 而不是 ID,当您在 6 周内回来查看它时会更有意义)。
  • course_id、sec_id、学期、年参考节表。

在您的数据中,有一位 ID 为“44444”的讲师,所以这可能不是问题。

但它看起来不像有一行,section'MAT-114', '2', 'Fall', 2019可能是你的问题。

另请注意,图像中也没有“MTH-114”。

于 2020-02-14T04:05:49.207 回答