I've created 'Staff' table. In that table it has a self referencing key - sql

create table STAFF
(StaffID TINYINT IDENTITY NOT NULL,
StaffName varchar(20) NOT NULL,
Phone varchar(10) NOT NULL,
Gender char(01),
DoB date NOT NULL,
Mentor TINYINT,
Payment_ID TINYINT NOT NULL,
constraint staff_pk primary key (StaffID),
constraint staff_fk foreign key (Payment_ID) references PAYMENT(Payment_ID),
constraint mentor_fk foreign key (Mentor) references staff(StaffID)
);
when it is executed it gives the following error.
Msg 547, Level 16, State 0, Line 1 The INSERT statement conflicted
with the FOREIGN KEY SAME TABLE constraint "mentor_fk". The conflict
occurred in database "F_T", table "dbo.STAFF", column 'StaffID'. The
statement has been terminated.

Create the staff table first. Then add the foreign key:
create table STAFF (
StaffID TINYINT IDENTITY NOT NULL,
StaffName varchar(20) NOT NULL,
Phone varchar(10) NOT NULL,
Gender char(01),
DoB date NOT NULL,
Mentor TINYINT,
Payment_ID TINYINT NOT NULL,
constraint staff_pk primary key (StaffID),
constraint staff_fk foreign key (Payment_ID) references PAYMENT(Payment_ID)
);
alter table staff
add constraint mentor_fk foreign key (Mentor) references staff(StaffID);

Related

Dbeaver: Why does code not work correctly?

It doesn't create all my tables and also says I have syntax errors. Can anyone help??
CREATE table project (
project_id int not null,
budget_id int not null,
division_id int not null,
project_name varchar(40),
project_begin_date date,
project_end_date date
);
ALTER table project
add constraint pk_project_project_id
primary key(project_id)
;
ALTER table project
add constraint fk_project_budget_id
foreign key(budget_id) references budget(budget_id)
;
ALTER table project
add constraint fk_project_division_id
foreign key (division_id) references division(division_id)
;
CREATE table division(
division_id int not null,
division_name varchar(40),
constraint pk_division_division_id primary key(division_id)
);
CREATE table committee(
committee id int not null,
committee_name varchar(40),
constraint pk_committee_committee_id primary key(committe_id)
);
create table employee(
employee_id int not null,
given_name varchar(40),
middle_name varchar(40),
family_name varchar(40),
form_of_address varchar(10),
name_suffix varchar(25),
work_phone_number varchar(20),
hourly_budget_rate numeric(8,2),
constraint pk_employee_employee_id primary key(employee_id)
);
CREATE table budget(
budget_id int not null,
budget_description varchar(100),
constraint pk_budget_budget_id primary key(budget_id)
);
CREATE table section(
section_id int not null,
division_id int,
budget_id int,
section_name varchar(40),
constraint pk_section_section_id primary key(section_id),
constraint pk_section_buget_id foreign key(budget_id) references budget(budget_id),
constraint pk_section_division_id foreign key(division_id) references division(division_id)
);
CREATE table employee_job_assignment(
job_assignment_id int not null,
employee_id int not null,
section_id int not null,
begin_datetime date,
end_datetime date,
job_title varchar(100),
constraint pk_employee_job_assignment primary key(job_assignment_id),
constraint pk_employee_job_assignment foreign key(employee_id) references employee(employee_id),
constraint pk_employee_job_assignment foreign key(section_id) references section(section_id)
);
CREATE table budget_category(
budget_category_code char(5),
budget_category_description char(100),
constraint pk_budget_category primary key(budget_category_code)
);
CREATE table budget_charge(
transaction_id int not null,
budget_category_code char(5),
budget_id int not null,
transaction_date date,
transaction_description varchar(100),
transaction_type char(5),
constraint pk_budget_charge_transaction_id primary key(transaction_id),
constraint pk_budget_charge_budget_category_code foreign key(budget_category_code) references budget_category(budget_category_code),
constraint pk_budget_charge_budget_id foreign key(budget_id) references budget(budget)
);
CREATE office(
office_id int not null,
office_type char(1),
office_building char(5),
office_location varchar(20),
constraint pk_office primary key(office_id)
);
CREATE table employee_office(
office id int not null,
employee_id int not null,
constraint pk_employee_office_office_id foreign key(office_id) references office(office_id),
constraint pk_employee_office_employee_id foreign key(employee id) references employee(employee_id)
);
CREATE table project_employee(
employee_id int not null,
project_id int not null,
project_role varchar(25),
constraint pk_project_employee_employee_id foreign key(employee_id) references employee(employee_id),
constraint pk_project_employee_project_id foreign key(project_id) references project(project_id)
);
CREATE table purchase_expense_charge(
transaction_id int not null,
purchase_expense_dollars numeric(8,2),
constraint pk_purchase_expense_charge_transaction_id primary key(transaction_id),
constraint pk_purchase_expense_charge_transaction_id foreign key(transaction_id) references budget_charge(transaction_id)
);
CREATE table hours_charge(
transaction_id int not null,
employee_id int not null,
hours_charged numeric(7,2),
time_period_begin_date date,
time_period_end_date date,
constraint pk_hours_charge_transaction_id primary key(transaction_id),
constraint pk_hours_charge_transaction_id foreign key(transaction_id) references budget_charge(transaction_id),
constraint pk_hours_charge_employee_id foreign key(employee_id) references employee(employee_id)
);
CREATE table budget_line_item(
budget_category_code char(5),
budget_id int not null,
line_item_budget_dollars numeric(8,2),
constraint pk_budget_line_item_budget_category_code foreign key(budget_category_code) references budget_category(budget_category_code),
constraint pk_budget_line_item_budget_category_code foreign key(budget_id) references budget(budget_id)
);
CREATE table travel_expense_charge(
transaction_id int not null,
employee_id int not null,
trip_expense_dollars NUMERIC(8,2),
trip_destination VARCHAR(100),
trip_begin_date date,
trip_end_date date,
constraint pk_travel_expense_charge_transaction_id primary key (transaction_id),
constraint pk_travel_expense_charge_transaction_id foreign key (transaction_id) references budget_charge(transaction_id),
constraint pk_travel_expense_charge_employee_id foreign key (employee_id) references employee(employee_id)
);
CREATE table committee_member(
committee_id int not null,
employee_id int not null,
committee_role varchar(25),
constraint pk_committee_member_committee_id foreign key(committee_id) references committee(committee_id)
constraint pk_comittee_member_employee_id foreign key(employee id) references employee(employee_id)
);

SQL Missing left/right parenthesis

I keep getting a missing right or left parenthesis error when I try to create my tables with a foreign key, but when I remove the foreign key my code has no problem creating the tables. I need the code to run with the foreign keys.
--deletes respective tables previously created
drop table DEPARTMENT;
drop table POSITION;
drop table EMPLOYEE;
drop table COMPANY;
drop table DEGREE;
drop table GRAD_INFO;
drop table STUDENT;
CREATE TABLE Department
(
Dept_Name varchar(255) PRIMARY KEY, --primary key
Dept_Phone_Num numeric(35) not null
);
CREATE TABLE Position
(
Position_Name varchar(255) NOT NULL PRIMARY KEY, --primary key
Position_Salary decimal(10, 2) not null,
Employee_num numeric(35) not null,
CONSTRAINT fk_Dept_Name
FOREIGN KEY Dept_Name REFERENCES Department (Dept_Name) --foreign key
);
CREATE TABLE Employee
(
EmployeeID INT NOT NULL PRIMARY KEY, --primary key
Employee_Salary DECIMAL(10,2) DEFAULT NULL,
Employee_FName VARCHAR(30) DEFAULT NULL,
Employee_LName VARCHAR(30) NOT NULL,
Employee_Address VARCHAR(30) DEFAULT NULL,
Employee_Phone_Num INT(10) DEFAULT NULL,
Employee_Email VARCHAR(30) NOT NULL,
Employee_Status VARCHAR(10) NOT NULL,
CONSTRAINT Position_Name
FOREIGN KEY Position_Name REFERENCES Position(Position_Name), --foreign key
CONSTRAINT fk_Dept_Name
FOREIGN KEY Dept_Name REFERENCES Department(Dept_Name) --foreign key
);
CREATE TABLE Company
(
Comp_Name VARCHAR(20) NOT NULL PRIMARY KEY, --primary key
Position_Offer VARCHAR(30) NOT NULL,
Salary_Offer DECIMAL(10,2) NOT NULL,
Signing_Bonus DECIMAL(10,2) DEFAULT NULL,
Comp_Phone_Num INT(10) NOT NULL,
Comp_Address VARCHAR(30) NOT NULL,
CONSTRAINT fk_Employee_ID
FOREIGN KEY Employee_ID REFERENCES Employee (Employee_ID), --foreign key
CONSTRAINT fk_Dept_Name
FOREIGN KEY Dept_Name REFERENCES Department (Dept_Name) --foreign key
);
CREATE TABLE Degree
(
Degree_Type varchar(255) PRIMARY KEY, --primary key
Academic_Status varchar(255) not null,
Academic_Level varchar(255) not null,
Major varchar(255) not null,
Minor varchar(255) default null
);
CREATE TABLE Gead_Info
(
Grad_Status VARCHAR(255) NOT NULL PRIMARY KEY, --primary key
College_Name VARCHAR(255) NOT NULL,
Num_Of_Degrees NUMERIC(10) NOT NULL,
Grad_Date NUMERIC(6) NOT NULL,
CONSTRAINT fk_Degree_Type
FOREIGN KEY Degree_Type REFERENCES Degree(Degree_Type) --foreign key
);
CREATE TABLE Student
(
Student_ID NUMERIC(10) NOT NULL PRIMARY KEY, --primary key
Student_FName VARCHAR(20) NOT NULL,
Student_LName VARCHAR(20) NOT NULL,
Student_Address VARCHAR(20) NOT NULL,
Student_Email VARCHAR(20) NOT NULL,
Student_Phone_Num VARCHAR(10) NOT NULL,
CONSTRAINT fk_Comp_Name
FOREIGN KEY Comp_Name REFERENCES Company(Comp_Name), --foreign key
CONSTRAINT fk_Grad_Status
FOREIGN KEY Grad_Status REFERENCES Gead_Info(Grad_Status) --foreign key
);
The degree table is the only table where I don't get an error.
Should I use an alter table instead after I create all the tables or is there another way to solve my problem?
edit: updated code
I think your most immediate issue is with the Department table not having a comma between the two CONSTRAINT clauses. You might also run into issues with your Position table's CONSTRAINT clause referencing the Position table instead of the Employee table, although honestly I don't think you need that relationship defined at all since the Employee table already has a foreign key constraint with Position. And like what was stated earlier, your table definitions aren't in the right order so you have some tables referencing other tables before those others are created.
Give this a shot to see if the syntax works. I've added the column definition for the foreign key in Position, put parentheses around the foreign key column name in the CONSTRAINT clause, and removed the space between Department and the left paren in the CONSTRAINT clause. If this script works, make the same changes to your other table defs.
CREATE TABLE Department(
Dept_Name varchar(255) Primary Key, --primary key
Dept_Phone_Num numeric(35) not null
);
CREATE TABLE Position(
Position_Name varchar(255) not null Primary Key, --primary key
Position_Salary decimal(10, 2) not null,
Employee_num numeric(35) not null,
Dept_Name varchar(255) not null,
CONSTRAINT fk_Dept_Name FOREIGN KEY (Dept_Name) REFERENCES Department(Dept_Name) --foreign key
);

Multiple foreign keys oracle

In this database I have created 7 tables but in my last 2 tables, the billing and the product_billing table, I need to add foreign keys, I have used the method below but getting two different errors on each table when executing.
This is my code:
CREATE TABLE CUSTOMER(
CustomerID INT NOT NULL,
FirstName VARCHAR(50) NOT NULL,
Surname VARCHAR(50) NOT NULL,
Address VARCHAR(50)NOT NULL,
ContactNumber NUMBER NOT NULL,
Email VARCHAR(50) NOT NULL,
PRIMARY KEY(CustomerID));
CREATE TABLE EMPLOYEE(
EmployeeID INT NOT NULL,
FirstName VARCHAR(50) NOT NULL,
Surname VARCHAR(50) NOT NULL,
ContactNumber NUMBER NOT NULL,
Position VARCHAR(50)NOT NULL,
Address VARCHAR(50)NOT NULL,
Email VARCHAR(50) NOT NULL,
PRIMARY KEY(EmployeeID));
CREATE TABLE DELIVERY(
DeliveryID INT NOT NULL,
Description VARCHAR(50) NOT NULL,
DespatchDate DATE,
DeliveryDate DATE,
PRIMARY KEY(DeliveryID));
CREATE TABLE RETURNS(
ReturnID INT NOT NULL,
ReturnDate DATE NOT NULL,
Reason VARCHAR(50) NOT NULL,
PRIMARY KEY(ReturnID));
CREATE TABLE PRODUCT(
ProductID INT NOT NULL,
Product VARCHAR(50) NOT NULL,
Price VARCHAR(50) NOT NULL,
QTY INT NOT NULL,
PRIMARY KEY(ProductID));
CREATE TABLE BILLING(
BillID INT NOT NULL,
CONSTRAINT CustomerID FOREIGN KEY (CustomerID) REFERENCES CUSTOMER(CustomerID),
BillDate DATE,
CONSTRAINT EmployeeID FOREIGN KEY (EmployeeID) REFERENCES EMPLOYEE(EmployeeID),
PRIMARY KEY(BillID));
CREATE TABLE PRODUCT_BILLING(
CONSTRAINT DeliveryID FOREIGN KEY (DeliveryID) REFERENCES DELIVERY(DeliveryID),
CONSTRAINT ReturnID FOREIGN KEY (ReturnID) REFERENCES RETURNS(ReturnID),
CONSTRAINT ProductID FOREIGN KEY (ProductID) REFERENCES PRODUCT(ProductID),
CONSTRAINT BillID FOREIGN KEY (BillID) REFERENCES BILLING(BillID));
Those are the errors:
Error starting at line : 40 in command -
CREATE TABLE BILLING(
BillID INT NOT NULL,
CONSTRAINT CustomerID FOREIGN KEY (CustomerID) REFERENCES CUSTOMER(CustomerID),
BillDate DATE,
CONSTRAINT EmployeeID FOREIGN KEY (EmployeeID) REFERENCES EMPLOYEE(EmployeeID),
PRIMARY KEY(BillID))
Error report -
ORA-00904: "CUSTOMERID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error starting at line : 47 in command -
CREATE TABLE PRODUCT_BILLING(
CONSTRAINT DeliveryID FOREIGN KEY (DeliveryID) REFERENCES DELIVERY(DeliveryID),
CONSTRAINT ReturnID FOREIGN KEY (ReturnID) REFERENCES RETURNS(ReturnID),
CONSTRAINT ProductID FOREIGN KEY (ProductID) REFERENCES PRODUCT(ProductID),
CONSTRAINT BillID FOREIGN KEY (BillID) REFERENCES BILLING(BillID))
Error report -
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Please use below syntax to create foreign key references,
CREATE TABLE BILLING(
BillID INT NOT NULL,
CustomerID INT REFERENCES CUSTOMER(CustomerID),
BillDate DATE,
EmployeeID INT REFERENCES EMPLOYEE(EmployeeID),
PRIMARY KEY(BillID));
CREATE TABLE PRODUCT_BILLING(
DeliveryID INT REFERENCES DELIVERY(DeliveryID),
ReturnID INT REFERENCES RETURNS(ReturnID),
ProductID INT REFERENCES PRODUCT(ProductID),
BillID INT REFERENCES BILLING(BillID));

SQL Server : code created and not sure why it is not processing - foreign key problem?

CREATE TABLE THREE_GIRLS_COFFEE_HUT
(
ShopName Char NOT NULL,
PhoneNumber Char(12) NOT NULL,
Address Char(20) NOT NULL,
City Char(20) NOT NULL,
State Char(2) NOT NULL,
ZipCode Char(5) NOT NULL,
CONSTRAINT ShopPK PRIMARY KEY (ShopName)
);
CREATE TABLE EMPLOYEE
(
EmployeeID Int NOT NULL IDENTITY(1,1),
EmployeeName Char(30) NOT NULL,
PhoneNumber Char(10) NOT NULL,
Address Char(20) NOT NULL,
City Char(20) NOT NULL,
State Char(2) NOT NULL,
ZipCode Char(5) NOT NULL,
EmployeeType Char(10) NOT NULL,
ShopName Char (25) FOREIGN KEY REFERENCES THREE_GIRLS_COFFEE_HUT (ShopName)
CONSTRAINT EmployeePK PRIMARY KEY(EmployeeID),
CONSTRAINT EmployeeAK1 UNIQUE(EmployeeName)
);
CREATE TABLE CUSTOMER
(
CustomerID Int NOT NULL IDENTITY(1000,1),
CustomerName Char(30) NULL,
PhoneNumber Char(10) NULL,
EmailAddress Char(30) NOT NULL,
CONSTRAINT CustomerPK PRIMARY KEY(CustomerID),
CONSTRAINT CustomerAK1 UNIQUE(EmailAddress)
);
CREATE TABLE [ORDER]
(
SalesOrderNumber INT NOT NULL IDENTITY (1500,1),
Date Numeric(6) NOT NULL,
Subtotal Numeric(6, 2) NOT NULL,
Tax Numeric(6, 2) NOT NULL,
Total Numeric(6, 2) NOT NULL,
CONSTRAINT OrderPK PRIMARY KEY (SalesOrderNumber),
CONSTRAINT EmpOrdFK FOREIGN KEY(EmployeeID)
REFERENCES EMPLOYEE(EmployeeID)
ON UPDATE CASCADE
ON DELETE NO ACTION,
CONSTRAINT CustOrdFK FOREIGN KEY (CustomerID)
REFERENCES CUSTOMER(CustomerID)
ON UPDATE NO ACTION
ON DELETE NO ACTION
);
CREATE TABLE PRODUCT
(
ProductNumber Int NOT NULL IDENTITY(2000,1),
ProductDescription Char(20) NOT NULL,
QuantityOnOrder Numeric(4) NOT NULL,
QuantityOnHand Numeric(4) NOT NULL,
OrderDate Date NOT NULL,
ExpirationDate Date NOT NULL,
CONSTRAINT ProductPK PRIMARY KEY(ProductNumber),
CONSTRAINT ValidExpDate CHECK (ExpirationDate > OrderDate)
);
CREATE TABLE MENU_ITEM (
ItemNumber Int NOT NULL IDENTITY(3000,1),
ItemDescription Char(30) NOT NULL,
ItemCost Numeric(6,2) NOT NULL,
ProductNumber Int FOREIGN KEY REFERENCES PRODUCT(ProductNumber)
CONSTRAINT MenuPK PRIMARY KEY(ItemNumber),
CONSTRAINT MenuAK1 UNIQUE(ItemDescription),
);
CREATE TABLE ORDER_LINE_ITEM (
SalesOrderNumber INT FOREIGN KEY REFERENCES [ORDER](SalesOrderNumber),
ItemNumber INT FOREIGN KEY REFERENCES MENU_ITEM(ItemNumber),
Quantity Numeric NOT NULL,
UnitPrice Numeric(6,2) NOT NULL,
ExtendedPrice Numeric (6,2) NOT NULL,
);
I got these errors and it will not process- I think I have problem with my foreign key but I am not sure.
Msg 1769, Level 16, State 1, Line 40
Foreign key 'EmpOrdFK' references invalid column 'EmployeeID' in referencing table 'ORDER'.
Msg 1750, Level 16, State 0, Line 40
Could not create constraint or index. See previous errors.
There's no column EmployeeID in the ORDER table, so:
EmpOrdFK FOREIGN KEY(EmployeeID) REFERENCES EMPLOYEE(EmployeeID)
which says, create a foreign key in which Order.EmployeeId references Employee.EmployeeId, can't work.
Did you want to add an EmployeeId column to Order?
(There's also no CustomerId in Order, so the next line
CONSTRAINT CustOrdFK FOREIGN KEY (CustomerID)
would have failed too, except it never got to run because of the first error.)
You probably want to add EmployeeId and CustomerId to the Order table.
When you are mapping foreign key from Order table to Employee table, you have to map on common column in both tables. If you look at order table, which is missing employeeId in
that table.
And also don't call it as ORDER table. ORDER is reserved keyword in SQL server.
References: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/reserved-keywords-transact-sql?view=sql-server-ver15

How do I code these relationships in sql without getting am error

Attached is my attempt at creating a database for the following entity relationship diagram. But I keep getting the following error:
"SQL0538N FOREIGN KEY "ADVISOR_STUDENT" does not conform to the
description of the parent key of table or nickname "KISHANPA.STUDENT".
SQLSTATE=42830"
for these 4 tables: advisor, prereq, teaches, takes. The rest of the tables seem to work fine. It would be greatly appreciated if someone can guide me int eh right direction. Thanks
ER Diagram
Schema Diagram
create table department (
dept_name varchar(30) not null,
building varchar(30),
budget numeric(7,2),
constraint department_key primary key (dept_name)
);
create table instructor (
iid char(9) not null,
name varchar(30) not null,
dept_name varchar(30) not null,
salary numeric(6,2),
constraint instructor_key primary key (iid, dept_name),
constraint instructor_dept foreign key(dept_name)
references department on delete no action
);
create table student (
sid char(9) not null,
name varchar(30) not null,
tot_cred smallint,
dept_name varchar(30) not null,
constraint student_key primary key (sid, dept_name),
constraint student_dept foreign key(dept_name)
references department on delete no action
);
create table course (
course_id char(8) not null,
title varchar(30) not null,
dept_name varchar(30) not null,
credits int not null,
constraint course_key primary key (course_id, dept_name),
constraint course_dept foreign key(dept_name)
references department on delete no action
);
create table advisor (
sid char(9) not null,
iid char(9) not null,
constraint advisor_key primary key (sid, iid),
constraint advisor_student foreign key(sid)
references student on delete no action,
constraint advisor_instructor foreign key (iid)
references instructor on delete no action
);
create table prereq (
course_id char(8) not null,
prereq_id char(8),
constraint prereq_key primary key (course_id),
constraint prereq_course foreign key(course_id)
references course on delete no action,
constraint prereq_precourse foreign key(prereq_id)
references course on delete no action
);
create table classroom (
building varchar(30) not null,
room_number varchar(10) not null,
capicity integer,
constraint classroom_key primary key (building, room_number)
);
create table time_slot (
time_slot_id varchar(10) not null,
day varchar(10) not null,
start_time time not null,
end_time time,
constraint time_slot_key primary key (time_slot_id, day, start_time)
);
create table section (
course_id char(8) not null,
sec_id varchar(10) not null,
semester char(1) not null,
year numeric (4,0) not null,
building varchar(30) not null,
room_number varchar(10) not null,
time_slot_id varchar(10) not null,
constraint section_key primary key(course_id, sec_id, year,
building, room_number, time_slot_id),
constraint section_classroom foreign key(building, room_number)
references classroom on delete no action
);
create table teaches (
iid char(9) not null,
course_id char(8) not null,
sec_id varchar(10) not null,
semester char(1) not null,
year numeric(4,0) not null,
constraint teaches_key primary key (iid, course_id, sec_id,
semester, year),
constraint section_instrictor foreign key(iid)
references instructor on delete no action,
constraint teaches_section foreign key(course_id, sec_id, semester, year)
references section on delete no action
);
create table takes (
sid char(9) not null,
course_id char(8) not null,
sec_id varchar(10) not null,
semester char(1) not null,
year numeric(4,0) not null,
grade real,
constraint takes_key primary key (sid, course_id, sec_id,
semester, year),
constraint student_takes foreign key(sid)
references student on delete cascade,
constraint takes_section foreign key(course_id, sec_id,
semester, year) references section on delete cascade
);
Your problem is on your key for student. You must have the same column to join tabes whith FK. Modify you table student like this :
create table student (sid char(9) not null, name varchar(30) not null, tot_cred smallint, dept_name varchar(30) not null, constraint student_key primary key (sid), constraint student_dept foreign key(dept_name) references department on delete no action);
The docs say this:
A foreign key references a primary key or a unique key in the same or
another table. A foreign key assignment indicates that referential
integrity is to be maintained according to the specified referential
constraints.
Your "advisor_student" references "sid", but that is not the primary key. You would need to include the department or change the design.
Your primary keys don't match your schema diagram for several tables.
Instructor, student, and course all have dept_name in the primary key in your DDL, but according to the Schema diagram, and logically as well, this field should not be a part of the primary key for any of those tables. In addition, the primary key of section does not match the Schema diagram. It should be (course_id, sec_id, semester, year) You have a lot of extra fields in there. That will cause problems with the foreign key constraints on teaches and takes.
Finally, the time_slot file both in the schema, and in the DDL have what I would call a questionable, given no other input, primary key. Should just be time_slot_id to my mind, then you can build a foreign key from section to time_slot as well.