error while creating both of these tables or when foreign key contraints are added - sql

Whenever I try to create these two tables in oracle live SQL it gives right parenthesis error.
CREATE TABLE Section_Table (
Status_ID INT Primary Key,
Names varchar(20),
course_id int FOREIGN KEY references Course_Table(course_id),
Schedule_id int FOREIGN KEY references Schedule_Table(Schedule_id),
Instruction_id int
);
CREATE TABLE Enrollment_Table (
ENROLLMENT_ID INT PRIMARY KEY,
STUDENT varchar(20),
course_id INT FOREIGN KEY REFERENCES Course_Table(course_id),
LEVEL_ID INT FOREIGN KEY REFERENCES Level_Table(LEVEL_ID),
Status_id INT FOREIGN KEY REFERENCES Status_Table(Status_ID),
DATE_ENROLLED DATE
);

Related

Problems when creating a table referencing other with FK

I'm trying to create a database for a project that works like a school with some teachers,courses,classes etc.
I need to create a new Table involving 2 tables however I'm getting an error when creating the table Classes_Teachers.
There are no primary or candidate keys in the referenced table 'Classes' that match the referencing column list in the foreign key 'FK__Classes_T__Class__11007AA7'.
Here is my SQL Code:
CREATE TABLE Teachers(
id INT,
name varchar(40),
email varchar(30) FOREIGN KEY REFERENCES Users(email),
PRIMARY KEY(id)
);
CREATE TABLE Courses(
name varchar(20),
acr varchar(4),
teacher int FOREIGN KEY REFERENCES Teachers(id),
PRIMARY KEY(acr)
);
CREATE TABLE Classes(
id varchar(2),
courses_acronym varchar(4) FOREIGN KEY REFERENCES Courses(acr),
year_Semesters varchar(5),
PRIMARY KEY(id,courses_acronym),
);
CREATE TABLE Classes_Teacher(
Classes_id varchar(2) FOREIGN KEY REFERENCES Classes(id),
Teachers_id INT FOREIGN KEY REFERENCES Teachers(id),
courses_acronym varchar(4) FOREIGN KEY REFERENCES Classes(courses_acronym),
PRIMARY KEY(Classes_id,courses_acronym)
);
The error comes from the foreign keys declared in table Classes_Teacher. To relate to the Classes table, you want to use both primary columns of the referred tables, instead of twn separated foreign keys.
Consider:
CREATE TABLE Classes_Teacher(
Classes_id varchar(2),
Teachers_id INT FOREIGN KEY REFERENCES Teachers(id),
courses_acronym varchar(4),
FOREIGN KEY (Classes_id, courses_acronym) REFERENCES Classes(id, courses_acronym),
PRIMARY KEY(Classes_id,courses_acronym)
);
Demo on DB Fiddle.
In table Classes you define a Primary Key on two columns. This is probably not needed if courses_acronym depends on ID.
CREATE TABLE Classes(
id varchar(2),
courses_acronym varchar(4) FOREIGN KEY REFERENCES Courses(acr),
year_Semesters varchar(5),
PRIMARY KEY(id),
);
In table Classes_Teacher you refer Classes twice. Shouldn't the second Foreign Key refer Courses?
CREATE TABLE Classes_Teacher(
Classes_id varchar(2) FOREIGN KEY REFERENCES Classes(id),
Teachers_id INT FOREIGN KEY REFERENCES Teachers(id),
courses_acronym varchar(4) FOREIGN KEY REFERENCES Courses(acr),
PRIMARY KEY(Classes_id,courses_acronym)
);

column "parent_id" referenced in foreign key constraint does not exist when creating SQL table

I am new in SQL and trying to understand Foreign key syntax. I know this was asked in multiple questions but each question I found did not seem to teach me what I am doing wrong here.
This is my SQL code:
CREATE TABLE Customer
(
id int primary key,
name varchar(30),
age int,
gender bool
);
CREATE TABLE Minor
(
FOREIGN KEY (parent_id) REFERENCES Customer(id)
);
CREATE TABLE Adult
(
FOREIGN KEY (parent_id) REFERENCES Customer(id)
);
CREATE TABLE Shop
(
id int primary key
);
CREATE TABLE Drink
(
name varchar(30) primary key
);
CREATE TABLE AlcoholicDrink
(
FOREIGN KEY (name) REFERENCES Drink(name)
);
CREATE TABLE NonAlcoholicDrink
(
FOREIGN KEY (name) REFERENCES Drink(name)
);
And this is the error I am getting:
ERROR: column "parent_id" referenced in foreign key constraint does not exist
SQL state: 42703
You need to add fields in your tables to make the reference. Something like this :
CREATE TABLE Customer
(
id int primary key,
name varchar(30),
age int,
gender bool
);
CREATE TABLE Minor
(
minor_id serial primary key,
parent_id int,
other_fields text etc.
FOREIGN KEY (parent_id) REFERENCES Customer(id)
);
This is simply the reason why it's not working.
Like this
CREATE TABLE Customer
(
id int primary key,
name varchar(30),
age int,
gender bool
);
CREATE TABLE Minor
(
parent_id int ,
FOREIGN KEY (parent_id) REFERENCES Customer(id)
);
To add an answer without just a code snippet:
You've got the right idea with FOREIGN KEY (parent_id) REFERENCES Customer(id). This bit adds a constraint or a "rule" to your table. This constraint ensures that parent_id holds a real id in your Customer table.
The error message told us that column "parent_id" referenced in foreign key constraint does not exist. The confusion comes, understandably, from mistaking the constraint declaration for a column declaration.
As others have pointed out, we need to both 10 declare the foreign key constraint and 2) declare the column.
CREATE TABLE Customer
(
id int primary key,
name varchar(30),
age int,
gender bool
);
CREATE TABLE Minor
(
parent_id int, -- Declare the column
FOREIGN KEY (parent_id) REFERENCES Customer(id) -- Declare the constraint
);

Error when setting foreign key in SQL Server

I have the following queries that I run to create tables in MS SQL Server:
CREATE TABLE menus
(
menu_id int NOT NULL PRIMARY KEY,
menu_name char,
other_details char
)
CREATE TABLE bookings
(
booking_Id int NOT NULL PRIMARY KEY,
date_booked DATE,
date_of_booking DATE,
other_details char,
staff_id int FOREIGN KEY REFERENCES staff(staff_id),
customer_id int FOREIGN KEY REFERENCES customers(customer_id)
)
CREATE TABLE menus_booked
(
menu_id INT NOT NULL,
booking_id INT NOT NULL,
CONSTRAINT PK_menus_booked PRIMARY KEY(menu_id,booking_id),
FOREIGN KEY (menu_id) REFERENCES menus(menu_id),
FOREIGN KEY (booking_id) REFERENCES bookings(booking_id)
)
CREATE TABLE menu_changes
(
change_id int NOT NULL PRIMARY KEY,
menu_id int NOT NULL,
booking_id int NOT NULL,
change_details char,
FOREIGN KEY (menu_id) REFERENCES menus_booked(menu_id),
FOREIGN KEY (booking_id) REFERENCES menus_booked(booking_id)
)
On running the last query I get the error:
There are no primary or candidate keys in the referenced table 'menus_booked' that match the referencing column list in the foreign key 'FK_menu_chan_menu'
I am unsure if my queries are correct and can't resolve this error.
The primary key of menus_booked is a unique combination of menu_id and booking_id. A foreign must point to that combination, not just one of its fields, which is not necessarily unique. Your query currently tries to define two foreign keys, one on each column, instead of one foreign key on the combination of the columns:
CREATE TABLE menu_changes
(
change_id int NOT NULL PRIMARY KEY,
menu_id int NOT NULL,
booking_id int NOT NULL,
change_details char,
FOREIGN KEY (menu_id, booking_id)
REFERENCES menus_booked(menu_id, booking_id) -- Here!
)
A foreign key has to reference a primary key (or unique key but here the PK is the problem), and it has to reference it in it's entirety.
FOREIGN KEY (menu_id) REFERENCES menus_booked(menu_id),
FOREIGN KEY (booking_id) REFERENCES menus_booked(booking_id)
You have two foreign key's referencing part of the primary key of menus_booked. You'll have to alter it to:
FOREIGN KEY (menu_id, booking_id) REFERENCES menus_booked(menu_id, booking_id)

foreign key to a table having more than one primary key

so this is my double primary key table
create table order_mattress
(
order_number int,
mattress_id int ,
primary key (order_number,mattress_id)
);
this is my second table
create table mattress
(
mattress_id int IDENTITY(1,1) PRIMARY KEY,
mattress_name varchar(25)
);
i want to mattress_id in the table mattress to be a foreign key to mattress_id in the table order_mattress how is that possible, without having any problem because of the double primary key in the firsttable
I'd change it so that you have 3 tables as opposed to 2:
Simple order table to hold unique order number:
create table orders
(
order_number int IDENTITY(1,1) PRIMARY KEY,
);
Mattress table that hold all unique mattresses:
create table mattress
(
mattress_id int IDENTITY(1,1) PRIMARY KEY,
mattress_name varchar(25)
);
The order mattress table, then links mattresses to orders with foreign keys.
create table order_mattress
(
order_number int FOREIGN KEY REFERENCES orders(order_number),
mattress_id int FOREIGN KEY REFERENCES mattress(mattress_id)
);
You have the direction wrong - if the mattress table describes mattresses, the mattress_order table should have a foreign key to it:
ALTER TABLE order_mattress
ADD FOREIGN KEY (mattress_id)
REFERENCES mattress(mattress_id)
The other direction is indeed impossible as mattress_id is not unique in order_mattress.

Foreign key on a Foreign key - SQL Server

I'm creating a database for a project in which I need to declare a foreign key on another foreign key for the sake of a checking constraint.
I have a Person table and a Groups table, both of these contain a DepartmentID. Whenever I insert a new task in the Task table, I want to check that the Groups.DepartmentID matches the Person.DepartmentID.
The idea is that a task is linked to a person and has 2 types, a grouptype which defines if its database work, financial work etc and a tasktype which defines if its maintenance, training etc. When a person tries to add a task with a groupType that is not for his/her department it should fail.
I tried adding these attributes to the Task table as a foreign key, however declaring a foreign key on a non-unique or non-primary key isn't accepted in Microsoft SQL Server (the DepartmentID in the Person and Group tables cannot be unique!).
Anyone knows how to fix this?
CREATE TABLE Department
(
ID int PRIMARY KEY IDENTITY,
Name varchar(50),
UNIQUE ("Name")
)
CREATE TABLE Groups
(
ID int IDENTITY,
GroupType varchar(50) PRIMARY KEY,
Description varchar(255) DEFAULT ('-'),
DepartmentID int
FOREIGN KEY (DepartmentID) REFERENCES Department(ID),
)
CREATE TABLE Person
(
ID int PRIMARY KEY IDENTITY,
Name varchar(50),
DepartmentID int
FOREIGN KEY (DepartmentID) REFERENCES Department(ID)
)
CREATE TABLE TaskType
(
ID int IDENTITY,
TaskType varchar(50) PRIMARY KEY,
Description varchar(255) DEFAULT ('-'),
)
CREATE TABLE Task
(
ID int IDENTITY,
TimeFrame decimal(4,2),
Yearcount int,
GroupType varchar(50),
TaskType varchar(50),
WeekNr int,
ExceptionDetail varchar(255) DEFAULT ('-'),
PersonID int
)
These are the FK attributes in the task table that are not accepted:
GDID int FOREIGN KEY REFERENCES Groups(DepartmentID),
PDID int FOREIGN KEY REFERENCES Person(DepartmentID),
CHECK (GDID = PDID),
UNIQUE ("TaskType", "GroupType", "WeekNr", "Yearcount"),
FOREIGN KEY (TaskType) REFERENCES TaskType(TaskType),
FOREIGN KEY (PersonID) REFERENCES Person(ID),
FOREIGN KEY (GroupType) REFERENCES Groups(GroupType)
Add wider "super keys" to these tables that include the primary key and additional columns1, then declare the foreign keys using them. Whether you also remove the superfluous smaller foreign keys is a matter of taste:
CREATE TABLE Groups(
ID int IDENTITY,
GroupType varchar(50) PRIMARY KEY,
Description varchar(255) DEFAULT ('-'),
DepartmentID int FOREIGN KEY (DepartmentID) REFERENCES Department(ID),
constraint Group_Dep_XRef UNIQUE (GroupType,DepartmentID)
)
CREATE TABLE Person(
ID int PRIMARY KEY IDENTITY,
Name varchar(50),
DepartmentID int FOREIGN KEY (DepartmentID) REFERENCES Department(ID),
constraint Person_Dept_XRef UNIQUE (ID,DepartmentID)
)
CREATE TABLE Task(
ID int IDENTITY,
TimeFrame decimal(4,2),
Yearcount int,
GroupType varchar(50),
TaskType varchar(50),
WeekNr int,
ExceptionDetail varchar(255) DEFAULT ('-'),
PersonID int,
DepartmentID int,
constraint FK_Group_Dept_XRef FOREIGN KEY (GroupType,DepartmentID)
references Group (GroupType,DepartmentID),
constraint FK_Person_Dept_XRef FOREIGN KEY (PersonID,DepartmentID)
references Person (ID,DepartmentID),
UNIQUE ("TaskType", "GroupType", "WeekNr", "Yearcount"),
FOREIGN KEY (TaskType) REFERENCES TaskType(TaskType),
FOREIGN KEY (PersonID) REFERENCES Person(ID), --Redundant now
FOREIGN KEY (GroupType) REFERENCES Groups(GroupType) --Also redundant
)
(I also consolidated GDID and PDID into DepartmentID - if they're always meant to be equal, why store that twice and then have to have another constraint to assert their equality?)
1If a primary key (or unique key) is sufficient to uniquely identify each row then any wider key which includes the key columns and additional columns must also be sufficient to uniquely identify each row.