Oracle SQL constraint, name entered must be a member - sql

I have created a tennis club database. This database contains the following tables:
The player table
CREATE TABLE player
(
member_id INT PRIMARY KEY NOT NULL,
member_name VARCHAR2(70) NOT NULL,
date_of_birth DATE,
member_address VARCHAR2(300),
contact_number INT NOT NULL,
gender VARCHAR2(1) NOT NULL CHECK(gender IN('f','m')),
club_seeding INT NOT NULL,
county_seeding INT NOT NULL,
renewal_date DATE,
m_type VARCHAR(9) NOT NULL CHECK(m_type IN('junior','student', 'senior', 'family', 'associate'))
);
This consists of members of the tennis club.
And then there is the team table
CREATE TABLE team
(
team_id INT PRIMARY KEY NOT NULL,
club_seeding INT,
county_seeding INT,
player1 VARCHAR2(70) NOT NULL,
player2 VARCHAR2(70) NOT NULL,
team_name VARCHAR2(145) NOT NULL
);
This is used to enter 2 players to form a doubles team.
My question is how could I create a constraint that only allows players(member_name) from the player table to be entered into the team table?

you need create foreign key constraints
see docs here
but in your sample you need change type of player1 and player2 columns from varchar to int:
sample of alter table statements
-- Create foreign key constraints
alter table TEAM
add constraint fk_team_player_1 foreign key (PLAYER1)
references player (MEMBER_ID);
alter table TEAM
add constraint fk_team_player_2 foreign key (PLAYER2)
references player (MEMBER_ID);
sample of createing FK contstraints in table
CREATE TABLE team
(
team_id INT PRIMARY KEY NOT NULL,
club_seeding INT,
county_seeding INT,
player1 INT NOT NULL,
player2 INT NOT NULL,
team_name VARCHAR2(145) NOT NULL
,constraint fk_team_player_1 foreign key (PLAYER1)
references player (MEMBER_ID)
,constraint fk_team_player_2 foreign key (PLAYER2)
references player (MEMBER_ID)
);
if you want to use member names as PK
CREATE TABLE player
(
member_name VARCHAR2(70) PRIMARY KEY NOT NULL,
date_of_birth DATE,
member_address VARCHAR2(300),
contact_number INT NOT NULL,
gender VARCHAR2(1) NOT NULL CHECK(gender IN('f','m')),
club_seeding INT NOT NULL,
county_seeding INT NOT NULL,
renewal_date DATE,
m_type VARCHAR(9) NOT NULL CHECK(m_type IN('junior','student', 'senior', 'family', 'associate'))
);
CREATE TABLE team
(
team_id INT PRIMARY KEY NOT NULL,
club_seeding INT,
county_seeding INT,
player1 VARCHAR2(70) NOT NULL,
player2 VARCHAR2(70) NOT NULL,
team_name VARCHAR2(145) NOT NULL
,constraint fk_team_player_1 foreign key (PLAYER1)
references player (member_name)
,constraint fk_team_player_2 foreign key (PLAYER2)
references player (member_name)
);

Related

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

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)

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

Key column does not exist in table. SQL

So I tried looking up other questions and tried them, but I was not able to figure out why my SQL was not working. It keeps saying that DepartmentID table does not exist and I have it declared as my primary key under Department.
CREATE TABLE EMPLOYEE(
SSN INT NOT NULL,
WorkID INT NOT NULL,
BirthDate DATE NOT NULL,
Name VARCHAR(20) NOT NULL,
UNIQUE(WorkID),
PRIMARY KEY(SSN),
FOREIGN KEY(DepartmentID) REFERENCES DEPARTMENT(DepartmentID),
FOREIGN KEY(DeviceID) REFERENCES DEVICE(DeviceID)
);
CREATE TABLE DEPARTMENT(
DepartmentID INT NOT NULL,
DepartmentName VARCHAR (20) NOT NULL,
PRIMARY KEY(DepartmentID),
UNIQUE(DepartmentName)
);
CREATE TABLE PRODUCT(
ProductID INT NOT NULL,
Name VARCHAR(20) NOT NULL,
Isle VARCHAR(5) NOT NULL,
Company VARCHAR(20) NOT NULL,
PRIMARY KEY(ProductID),
FOREIGN KEY(DepartmentID) REFERENCES DEPARTMENT(DepartmentID)
);
CREATE TABLE DEVICE(
DeviceID INT NOT NULL,
DateReceived DATE NOT NULL,
PRIMARY KEY(DeviceID)
);
CREATE TABLE SALES(
SalesID INT NOT NULL,
Profit INT NOT NULL,
Revenue INT NOT NULL,
PRIMARY KEY(SalesID)
);
CREATE TABLE AmountSold(
FOREIGN KEY (SalesID) REFERENCES SALES(SalesID),
FOREIGN KEY (ProductID) REFERENCES PRODUCT(ProductID)
);
The problem was that you were making foreign key but the column DepartmentID
doesn't exist on that table in which you were referencing and the second was
that your order wasn't correct.
CREATE TABLE DEPARTMENT(
DepartmentID INT NOT NULL,
DepartmentName VARCHAR (20) NOT NULL,
PRIMARY KEY(DepartmentID),
UNIQUE(DepartmentName)
);
CREATE TABLE DEVICE(
DeviceID INT NOT NULL,
DateReceived DATE NOT NULL,
PRIMARY KEY(DeviceID)
);
CREATE TABLE EMPLOYEE(
SSN INT NOT NULL,
WorkID INT NOT NULL,
BirthDate DATE NOT NULL,
DepartmentID INT NOT NULL,
DeviceID INT NOT NULL,
Name VARCHAR(20) NOT NULL,
UNIQUE(WorkID),
PRIMARY KEY(SSN),
FOREIGN KEY(DepartmentID) REFERENCES DEPARTMENT(DepartmentID),
FOREIGN KEY(DeviceID) REFERENCES DEVICE(DeviceID)
);
CREATE TABLE PRODUCT(
ProductID INT NOT NULL,
Name VARCHAR(20) NOT NULL,
DepartmentID INT NOT NULL,
Isle VARCHAR(5) NOT NULL,
Company VARCHAR(20) NOT NULL,
PRIMARY KEY(ProductID),
FOREIGN KEY(DepartmentID) REFERENCES DEPARTMENT(DepartmentID)
);
CREATE TABLE SALES(
SalesID INT NOT NULL,
Profit INT NOT NULL,
Revenue INT NOT NULL,
PRIMARY KEY(SalesID)
);
CREATE TABLE AmountSold(
FOREIGN KEY (SalesID) REFERENCES SALES(SalesID),
FOREIGN KEY (ProductID) REFERENCES PRODUCT(ProductID)
);
The problem here you didn't declared the DepartmentId column in Employee table. Also execute the parent table first followed by the ones referring
CREATE TABLE DEPARTMENT(
DepartmentID INT NOT NULL,
DepartmentName VARCHAR (20) NOT NULL,
PRIMARY KEY(DepartmentID)
);
CREATE TABLE DEVICE(
DeviceID INT NOT NULL,
DateReceived DATE NOT NULL,
PRIMARY KEY(DeviceID)
);
CREATE TABLE EMPLOYEE(
SSN INT NOT NULL,
WorkID INT NOT NULL,
BirthDate DATE NOT NULL,
Name VARCHAR(20) NOT NULL,
DepartmentID INT,
DeviceID INT ,
UNIQUE(WorkID),
PRIMARY KEY(SSN),
FOREIGN KEY(DepartmentID) REFERENCES DEPARTMENT(DepartmentID),
FOREIGN KEY(DeviceID) REFERENCES DEVICE(DeviceID)
);
CREATE TABLE PRODUCT(
ProductID INT NOT NULL,
Name VARCHAR(20) NOT NULL,
Isle VARCHAR(5) NOT NULL,
Company VARCHAR(20) NOT NULL,
DepartmentID INT,
PRIMARY KEY(ProductID),
FOREIGN KEY(DepartmentID) REFERENCES DEPARTMENT(DepartmentID)
);
CREATE TABLE SALES(
SalesID INT NOT NULL,
Profit INT NOT NULL,
Revenue INT NOT NULL,
PRIMARY KEY(SalesID)
);
CREATE TABLE AmountSold(
SalesId INT NOT NULL,
ProductId INT NOT NULL,
FOREIGN KEY (SalesID) REFERENCES SALES(SalesID),
FOREIGN KEY (ProductID) REFERENCES PRODUCT(ProductID)
);

Using foreign keys with multiple tables

I have a problem with creating a database. My fictional problem is to create database for tracking volleyball games. It should contain information about players (team they play in, one player can play only in one team), and played games( every game contains 6 players in each team).
I wanted to create following tables :
Player(Id P,Name,Surname,Team_Name F) ;
Team(Team_Name P,Coach);
Game(Date, Team1 F,Team2 F,Player1_Team1 F,...Player2_Team2 F,Player1_Team2 F,...,Player2_Team2 F).
I wanted that Team1 and Team2 are foreign keys Team_Name from Team table. Here is my attempt, I get that foreign key is incorrectly formed. I will be very glad for help.
CREATE TABLE Team (
Team_Name varchar(20) NOT NULL,
Coach varchar(20) NOT NULL,
CONSTRAINT Team_pk PRIMARY KEY (Team_Name)
);
CREATE TABLE Game (
id int NOT NULL,
Data date NOT NULL,
Team1 varchar(20) NOT NULL,
Team2 varchar(20) NOT NULL,
Score int ,
D1_Player1 int NOT NULL,
D1_Player2 int NOT NULL,
D1_Player3 int NOT NULL,
D1_Player4 int NOT NULL,
D1_Player5 int NOT NULL,
D1_Player6 int NOT NULL,
D2_Player1 int NOT NULL,
D2_Player2 int NOT NULL,
D2_Player3 int NOT NULL,
D2_Player4 int NOT NULL,
D2_Player5 int NOT NULL,
D2_Player6 int NOT NULL,
CONSTRAINT Game_pk PRIMARY KEY (id)
);
CREATE TABLE Player (
Id int NOT NULL,
Name varchar(30) NOT NULL,
Surname varchar(30) NOT NULL,
Team_Name varchar(20) NOT NULL,
CONSTRAINT Player_pk PRIMARY KEY (id)
);
ALTER TABLE Player ADD CONSTRAINT DZ
FOREIGN KEY (Team_Name)
REFERENCES Team (Team_Name)
;
ALTER TABLE Game ADD CONSTRAINT MD
FOREIGN KEY (Team1, Team2)
REFERENCES Team (Team_Name, Team_Name)
;
ALTER TABLE Mecz ADD CONSTRAINT MZ
FOREIGN KEY (D1_Player1, D1_Player2, D1_Player3, D1_Player4, D1_zPlayer5, D1_Player6, D2_Player1, D2_Player2, D2_Player3, D2_Player4, D2_Player5, D2_Player6)
REFERENCES Player (id, id, id, id, id, id, id, id, id, id, id, id)
;
You should add one by one all foreign keys.
ALTER TABLE Game ADD CONSTRAINT MZ
FOREIGN KEY (D1_Player1)
REFERENCES Player (id)
;
ALTER TABLE Game ADD CONSTRAINT MZ2
FOREIGN KEY (D1_Player2)
REFERENCES Player (id)
;
ALTER TABLE Game ADD CONSTRAINT MZ3
FOREIGN KEY (D1_Player3)
REFERENCES Player (id)
;
GO
✓
dbfiddle here

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.