Search code examples
sqldb-browser-sqlite

Foreign Key Constraint Failed - Even when I try to manually add data


I am studying SQL in my data science degree program, and the assignment is to perform SQL queries for the first time. In order to do that, I have to paste the SQL starter code in and run it. However, when I do, I hit an error that I can't solve. My class is online and my professor hasn't responded and, since this isn't part of the graded part of the assignment, I figured I'd try here to get help on this error.

Edit: It was pointed out that I didn't include all of the tables. Please see full code for all tables and all data. I was trying to avoid flooding this post with a lot of lines of code, but it seems I left out pertinent information. See below.

The tables were created with these blocks of code:

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 
);

Then, I added data to instructor and section with the follow. Side note, I had to go in and manually set the time_slot_id as a primary key to avoid getting an error.

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');

I didn't have a problem. Finally, I tried to run this:

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);

The error occurred on the last line. I always get:

Result: FOREIGN KEY constraint failed
At line 1: (Note: I tried re-running it by itself afterwards, thus the "Line 1")
INSERT INTO teaches VALUES ('44444', 'MAT-114', '2', 'Fall', 2019);

I checked over each constraint, looked at each column, and couldn't figure it out. I even went in and entered the data manually in the "Browse Data" tab within DB Browser Lite; while I was able to enter the first 4 values, when I try to enter anything for year, I get the same error in a popup box. Even when I click in the box, leave it blank, then try to click out, I get the same error. See image.

Please assist... I'm lost at this point and quite frustrated.

Error Message


Solution

  • There are two things here:

    1. the issue, and
    2. how to work out what the issue is.

    Let's start with 2).

    The error message is:

    Result: FOREIGN KEY constraint failed
    At line 1:
    INSERT INTO teaches VALUES ('44444', 'MAT-114', '2', 'Fall', 2019);

    That tells us to look at the foreign keys defined on the teaches table.

    There are 2:

    • ID references the ID in instructor table (aside: maybe call this instructor_id instead of ID, it'll make more sense in 6 weeks when you come back to look at it).
    • course_id, sec_id, semester, year reference the section table.

    There is, in your data, an instructor with ID '44444', so that's probably not the problem.

    But it doesn't look like there's a row in section with 'MAT-114', '2', 'Fall', 2019 and that is likely your problem.

    Note also that there's not one for 'MTH-114' like in the image, either.