There is no unique constraint matching given keys for referenced table " exam_subjects" - sql

CREATE TABLE student
(
student_id INT PRIMARY KEY,
first_name VARCHAR(40) NOT NULL,
last_name VARCHAR(40) NOT NULL,
birth_day DATE NOT NULL,
sex VARCHAR(1) NOT NULL,
student_email_address VARCHAR(40) NOT NULL UNIQUE,
student_password VARCHAR(10) NOT NULL UNIQUE,
student_nick_name VARCHAR(10) NOT NULL,
student_qualification VARCHAR(10) NOT NULL,
student_documents VARCHAR(255) NOT NULL,
student_image VARCHAR(100) NOT NULL
);
CREATE TABLE student_feedback
(
sr_no BIGSERIAL PRIMARY KEY,
student_id INT NOT NULL,
feedback_type VARCHAR(10) NOT NULL,
feedback_text VARCHAR(200) NOT NULL,
FOREIGN KEY (student_id) REFERENCES student(student_id)
);
CREATE TABLE online_exam
(
exam_id INT PRIMARY KEY,
exam_title VARCHAR(20) UNIQUE NOT NULL,
exam_duration_min INT NOT NULL,
total_questions INT NOT NULL,
marks_per_right_answer INT NOT NULL,
marks_per_wrong_answer INT NOT NULL,
passing_marks INT NOT NULL,
exam_status VARCHAR(2)
);
CREATE TABLE exam_subjects
(
sub_id INT,
exam_id INT,
sub_name VARCHAR(20) NOT NULL,
sub_desc VARCHAR(20) NOT NULL,
UNIQUE(sub_id,exam_id),
PRIMARY KEY(sub_id,exam_id),
FOREIGN KEY (exam_id) REFERENCES online_exam(exam_id) ON DELETE CASCADE
);
CREATE TABLE sub_questions
(
sub_id1 INT,
ques_id INT,
ques_text VARCHAR(150) NOT NULL,
ques_attachments VARCHAR(255),
option_1 VARCHAR(20) NOT NULL,
option_2 VARCHAR(20) NOT NULL,
option_3 VARCHAR(20) NOT NULL,
option_4 VARCHAR(20) NOT NULL,
UNIQUE(sub_id1,ques_id),
PRIMARY KEY (sub_id1,ques_id),
FOREIGN KEY (sub_id1) REFERENCES exam_subjects(sub_id) ON DELETE CASCADE
);
CREATE TABLE ques_answers
(
ans_id INT,
ques_id INT,
ans VARCHAR(20) NOT NULL,
PRIMARY KEY (ans_id,ques_id),
FOREIGN KEY (ques_id) REFERENCES sub_questions(ques_id) ON DELETE CASCADE
);
CREATE TABLE exam_registration
(
student_id INT,
exam_id INT,
exam_date_time TIMESTAMP NOT NULL,
PRIMARY KEY (student_id,exam_id),
FOREIGN KEY (student_id) REFERENCES student(student_id) ON DELETE CASCADE,
FOREIGN KEY (exam_id) REFERENCES online_exam(exam_id) ON DELETE CASCADE
);
CREATE TABLE exam_result
(
student_id INT,
exam_id INT,
sub_id INT,
marks_scored INT NOT NULL,
correct_ques INT NOT NULL,
wrong_ques INT NOT NULL,
grade VARCHAR(10) NOT NULL,
PRIMARY KEY(student_id,exam_id,sub_id),
FOREIGN KEY (student_id) REFERENCES student(student_id) ON DELETE CASCADE,
FOREIGN KEY (exam_id) REFERENCES online_exam(exam_id) ON DELETE CASCADE,
FOREIGN KEY (sub_id) REFERENCES exam_subjects(sub_id) ON DELETE CASCADE
);
Output :
CREATE TABLE,
CREATE TABLE,
CREATE TABLE,
CREATE TABLE,
There is no unique constraint matching given keys for referenced table "exam_subjects", relation "sub_questions" do not exist,
CREATE TABLE,
There is no unique constraint matching given keys for referenced table "exam_subjects".
Why am I getting "there is no unique..." error in postgresql? How to solve it? Please help.

In your table exam_subjects the primary key is a compossed key (PRIMARY KEY(sub_id, exam_id))
Therefore, your table sub_questions must have both columns to create the constraint, otherwise you get the error about unique constraints (or you can create a unique field acting as primary key, and create a unique index on both columns sub_id and exam_id)

Related

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

Problems referencing primary key with foreign key

This is code to create the beginnings of a book library database with Microsoft SQL Server Management Studio.
CREATE DATABASE BOOK_LIBRARY
CREATE TABLE LIBRARY_USER
(
usr_id int not null primary key,
f_name varchar(30) not null,
m_init char(1),
l_name varchar(30) not null,
balance decimal(6,2),
join_date date,
addrss_1 varchar(30) not null,
addrss_2 varchar(30),
city varchar(30) not null,
addrss_state char(2) not null,
zip_code varchar(10) not null,
email varchar(30)
);
CREATE TABLE LIBRARY_TRANSACTIONS
(
transaction_id int not null primary key,
maximum_borrow_duration int not null,
strt_date date not null,
actual_return_date date not null,
borrow_usr_id int not null,
foreign key (borrow_usr_id) references LIBRARY_USER(usr_id)
);
CREATE TABLE BOOKS
(
isbn varchar(17) not null primary key,
title varchar(30) not null,
number_of_copies int not null,
Author varchar(30) not null,
Number_of_pages int not null,
publish_year int not null,
book_type varchar(20)
);
CREATE TABLE DIGITAL_BOOKS
(
digital_id int not null primary key,
format varchar(30) not null,
size_mb int not null,
digital_isbn varchar(17) not null,
foreign key(digital_isbn) references BOOKS(isbn)
);
CREATE TABLE PHYSICAL_BOOKS
(
physical_id int not null primary key,
condition varchar(20) not null,
physical_isbn varchar(17) not null,
foreign key(physical_isbn) references BOOKS(isbn)
);
CREATE TABLE BOOK_COPY
(
digi_id int not null,
phys_id int not null,
primary key(digi_id, phys_id),
foreign key(digi_id) references DIGITAL_BOOKS(digital_id),
foreign key(phys_id) references PHYSICAL_BOOKS(physical_id)
);
CREATE TABLE CONTNS
(
trans_id int not null primary key,
digi_id int not null,
phys_id int not null,
foreign key(digi_id) references BOOK_COPY(digi_id),
foreign key(phys_id) references BOOK_COPY(phys_id)
);
Despite me being able to look and see that digi_id and phys_id are in fact primary keys of the book_copy table, I keep getting this error:
Msg 1776, Level 16, State 0, Line 66
There are no primary or candidate keys in the referenced table 'BOOK_COPY' that match the referencing column list in the foreign key 'FK__CONTNS__digi_id__37A5467C'.
Msg 1750, Level 16, State 1, Line 66
Could not create constraint or index. See previous errors.
Am I missing something obvious? I'm just starting out with using this program, so any help is greatly appreciated.
Here:
CREATE TABLE CONTNS
(
trans_id int not null primary key,
digi_id int not null,
phys_id int not null,
foreign key(digi_id) references BOOK_COPY(digi_id),
foreign key(phys_id) references BOOK_COPY(phys_id)
);
You are creating two foreign keys to parent table BOOK_COPY. But that table has a compound primary key (ie a multi-column primary key)
CREATE TABLE BOOK_COPY
(
digi_id int not null,
phys_id int not null,
primary key(digi_id, phys_id), --> here
...
)
As a consequence, you need a compound foreign key:
CREATE TABLE CONTNS
(
trans_id int not null primary key,
digi_id int not null,
phys_id int not null,
foreign key(digi_id, phys_id) references BOOK_COPY(digi_id, phys_id)
);

i getting this error: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "foreign_key1"

CREATE TABLE singer(
name varchar(50) primary key,
email varchar(50) not null,
account_number varchar(50) not null,
balance int not null
);
create table song(
id int primary key,
name varchar(50) not null,
singer varchar(50) foreign key references singer,
producer varchar(50) not null,
album varchar(50) not null,
total_sell int not null
);
CREATE TABLE producer(name VARCHAR(20) PRIMARY KEY);
ALTER TABLE song ALTER COLUMN producer VARCHAR(20);
ALTER TABLE song ADD CONSTRAINT foreign_key1 FOREIGN KEY(producer) REFERENCES producer;
In MySQL you can use this:
CREATE TABLE producer(
name varchar(20),
PRIMARY KEY (name)
);
CREATE TABLE song(
id int,
name varchar(50) not null,
singer varchar(50) not null,
producer varchar(50) not null,
album varchar(50) not null,
total_sell int not null,
PRIMARY KEY (`id`),
CONSTRAINT `foreign_key1` FOREIGN KEY (`producer`) REFERENCES `producer` (`name`)
);
OR:
ALTER TABLE `song`
ADD CONSTRAINT `foreign_key1`
FOREIGN KEY (`producer`)
REFERENCES `producer` (`name`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
you can see Here.
I can advise you to declare the same length as the name column. es. varchar(50)

SQL error 150 on table creation

I know the error 150 is related to foreign keys, but i can't figure out what wrong here.
CREATE TABLE IF NOT EXISTS User(
id_user INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
nom VARCHAR(255) NOT NULL,
prenom VARCHAR(255) NOT NULL,
naissance DATE NOT NULL,
email VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS Livre(
id_livre INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
titre VARCHAR(255) NOT NULL,
parution DATE NOT NULL,
id_edit INT NOT NULL,
id_user INT NOT NULL,
FOREIGN KEY ( id_edit)
REFERENCES Editeur(id_edit)
ON DELETE CASCADE,
FOREIGN KEY ( id_user)
REFERENCES User(id_user)
ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS Publication(
id_pub INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
texte TEXT NOT NULL,
date_pub DATE NOT NULL,
titre_pub VARCHAR(255) NOT NULL,
id_livre INT NOT NULL,
id_user INT NOT NULL,
FOREIGN KEY ( id_user)
REFERENCES User(id_user)
ON DELETE CASCADE,
FOREIGN KEY (id_livre)
REFERENCES Livre(id_livre)
ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS Editeur(
id_edit INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
nom VARCHAR(255) NOT NULL,
pays VARCHAR(255) NOT NULL,
adresse VARCHAR(255) NOT NULL,
tel VARCHAR(255) NOT NULL
);
The error occurs when i try to create tables Livre and Publication.
Types are the same so it doesn't seem to be the problem.
By mindful of the order in which you create the tables.
For instance, your table creation statement for Livre defines a foreign key on Editeur. But the table Editeur has not been created yet.
Adjust the create table statement ordering as required.

PostgreSQL constraint problems

I am having these two errors :
ERROR: there is no unique constraint matching given keys for referenced table "flight"
*** Error ***
ERROR: there is no unique constraint matching given keys for referenced table "flight"
This is my code :
CREATE TABLE Staff (
EmployeeNumber int NOT NULL,
FirstName char(15) NOT NULL,
LastName char(15) NOT NULL,
SocialSecurity int NOT NULL,
Sex char(1) NOT NULL,
Address char(20) NOT NULL,
City char(20) NOT NULL,
Province char(15) NOT NULL,
Country char(20) NOT NULL,
Primary Key (EmployeeNumber)
);
CREATE TABLE FlightAttendent (
FALN int,
StaffRole char (20) NOT NULL,
EmployeeNumber int NOT NULL,
Foreign Key (EmployeeNumber) References Staff(EmployeeNumber),
Primary Key (FALN)
);
Create TABLE AircraftType (
ACType char (10),
Instrument char(1) NOT NULL,
Engines int NOT NULL,
CrewCount int NOT NULL,
PassengerCount int NOT NULL,
Primary Key (ACType)
);
CREATE TABLE Pilot (
PILN int,
MedicalValid date NOT NULL,
StaffRole char (20) NOT NULL,
EmployeeNumber int NOT NULL,
AircraftType char (10) NOT NULL,
Foreign Key (EmployeeNumber) references Staff(EmployeeNumber),
Foreign Key (AircraftType) References AircraftType(ACType),
Primary Key (PILN)
);
Create TABLE Aircraft (
AircraftID char(6) NOT NULL,
AircraftManufacturer char(10) NOT NULL,
AircraftType char(10) NOT NULL,
Foreign Key (AircraftType) References AircraftType(ACType),
Primary Key (AircraftID)
);
CREATE Table Airport (
AirportCode char(4) NOT NULL,
AirportName char(40) NOT NULL,
City char(20) NOT NULL,
Country char(20) NOT NULL,
Continent char(20) NOT NULL,
Primary Key (AirportCode)
);
Create TABLE Flight (
FlightID char (20),
FlightDate date,
AircraftID char(6) NOT NULL,
ArrivalAirport char(4) NOT NULL,
DepartureAirport char(4) NOT NULL,
PRIMARY KEY (FlightID, FlightDate),
FOREIGN Key (ArrivalAirport) references Airport(AirportCode),
FOREIGN Key (DepartureAirport) references Airport(AirportCode),
FOREIGN KEY (AircraftID) references Aircraft(AircraftID)
);
Create TABLE FlightCrew (
FlightID char (20) REFERENCES Flight(FlightID) ON DELETE CASCADE,
FlightDate date REFERENCES Flight(FlightDate) ON DELETE CASCADE,
EmployeeNumber int NOT NULL,
StaffRole char(20) NOT NULL,
PRIMARY KEY(FlightID, FlightDate),
Foreign Key (EmployeeNumber) references Staff(EmployeeNumber)
);
CREATE Table Passenger (
PassengerNumber int,
PassportNumber int NOT NULL,
Citizenship char (20) NOT NULL,
FirstName char (20) NOT NULL,
LastName char (20) NOT NULL,
Primary Key (PassengerNumber)
);
CREATE Table PassengerManifest (
FlightID char(20),
FlightDate date,
PassengerNumber int NOT NULL,
Foreign Key (FlightDate) References Flight(FlightDate),
Foreign Key (PassengerNumber) References Passenger(PassengerNumber),
Primary Key (FlightID, FlightDate)
);
What did I do wrong? Thanks!
When you have multiple values in a primary key, you need to reference it differently as a foreign key.
Basically, when you say
FlightID char (20) REFERENCES Flight(FlightID) ON DELETE CASCADE,
PostgreSQL checks for that primary key, which doesn't exist (since the primary key on that table is (flightid, flightdate)).
So drop the REFERENCES clauses when referencing the flight table, and add
FOREIGN KEY (FlightID, FlightDate) REFERENCES Flight (FlightID, FlightDate)
In the manner you have in some of the other table definitions.