Which statement is needed to revise? - sql

I tried to create several tables, but only the server table was created. Please fix my work!
CREATE TABLE server
(
SERVER_ID varchar(25) NOT NULL,
SERVER_IP varchar(15) NOT NULL,
SERVER_LOCATION varchar(25) NOT NULL,
SERVER_BRAND varchar(15) NOT NULL,
CONSTRAINT server_pk
PRIMARY KEY (SERVER_ID)
);
CREATE TABLE application
(
APP_ID varchar(25) NOT NULL,
ACCOUNT_NUM varchar(25) NOT NULL,
RECORD_ID varchar(10) NOT NULL,
VERSION_ID varchar(10) NOT NULL,
LAST_UPDATED date NOT NULL,
CONSTRAINT application_pk
PRIMARY KEY (APP_ID),
CONSTRAINT application _fk_account
FOREIGN KEY (ACCOUNT_NUM)
REFERENCES account (ACCOUNT_NUM),
CONSTRAINT application _fk_record
FOREIGN KEY (RECORD_ID)
REFERENCES record (RECORD_ID)
);
CREATE TABLE record
(
RECORD_ID varchar(25) NOT NULL,
VIN_NUM varchar(25) NOT NULL,
SERVER_ID varchar(25) NOT NULL,
CONSTRAINT record_pk
PRIMARY KEY (RECORD_ID, VIN_NUM, SERVER_ID),
CONSTRAINT record_fk_vehicle
FOREIGN KEY (VIN_NUM)
REFERENCES vehicle (VIN_NUM),
CONSTRAINT record_fk_server
FOREIGN KEY (SERVER_ID)
REFERENCES server (SERVER_ID)
);
CREATE TABLE vehicle
(
VIN_NUM varchar(25) NOT NULL,
V_MILEAGE int NOT NULL,
V_GASUSED varchar(25) NOT NULL,
V_ELECTRICALMILES int NOT NULL,
DRIVER_ID varchar(25) NOT NULL,
CONSTRAINT vehicle_pk
PRIMARY KEY (VIN_NUM),
CONSTRAINT vehicle_fk_driver
FOREIGN KEY (DRIVER_ID)
REFERENCES driver (DRIVER_ID)
);
CREATE TABLE driver
(
DRIVER_ID varchar(25) NOT NULL,
LICENSE_NUM varchar(25) NOT NULL,
FIRST_NAME varchar(25) NOT NULL,
LAST_NAME varchar(25) NOT NULL,
INSURANCE_POLICY varchar(25) NOT NULL,
ACCOUNT_NUM int NOT NULL,
CONSTRAINT driver_pk
PRIMARY KEY (DRIVER_ID),
CONSTRAINT driver_fk_account
FOREIGN KEY (ACCOUNT_NUM)
REFERENCES account (ACCOUNT_NUM)
);
CREATE TABLE account
(
ACCOUNT_NUM int NOT NULL,
DRIVER_ID varchar(25) NOT NULL,
DEVICE_ID varchar(25) NOT NULL,
DATE_CREATED date NOT NULL,
ACCOUNT_STATUS varchar(10) NOT NULL,
CONSTRAINT account_pk
PRIMARY KEY (ACCOUNT_NUM, DRIVER_ID),
CONSTRAINT account_fk_driver
FOREIGN KEY (DRIVER_ID)
REFERENCES driver (DRIVER_ID)
);

There are several issues here.
You should create tables in order that matches foreign key constraint dependencies, i.e. you can't create detail table first, and then its master table because there's no table (or its primary key) which is to be referenced.
A good workaround for that is to remove foreign key constraints from create table and create them separately (using alter table ... add constraint) after all tables are created.
There's no problem if two tables reference each other (such as driver and account do), if there's a good reason to do it. However, as I previously said - you can't do it within create table statements; at least one foreign key has to be moved out.
If primary key consists of several columns (so it is a composite key), foreign key also must contain the same number & datatypes of columns that reference it. That's what you have for driver and account tables.
account's primary key is (account_num, driver_id)
driver's foreign key can't be just (account_num) - it has to contain driver_id as well
Finally, application table: its foreign keys can't be created because it references
account table whose primary key is (account_num, driver_id), but - application doesn't contain driver_id column at all which might mean that you wrongly set account's primary key (or application table)
record table which has a composite primary key on (record_id, vin_num, server_id), while application has only record_id. Remark I wrote for account is valid here as well
Now, if we fix what I mentioned, tables are created, but application misses all its foreign key constraints.
SQL> CREATE TABLE server
2 (
3 SERVER_ID varchar2(25) NOT NULL,
4 SERVER_IP varchar2(15) NOT NULL,
5 SERVER_LOCATION varchar2(25) NOT NULL,
6 SERVER_BRAND varchar2(15) NOT NULL,
7 CONSTRAINT server_pk
8 PRIMARY KEY (SERVER_ID)
9 );
Table created.
SQL>
SQL> CREATE TABLE driver
2 (
3 DRIVER_ID varchar2(25) NOT NULL,
4 LICENSE_NUM varchar2(25) NOT NULL,
5 FIRST_NAME varchar2(25) NOT NULL,
6 LAST_NAME varchar2(25) NOT NULL,
7 INSURANCE_POLICY varchar2(25) NOT NULL,
8 ACCOUNT_NUM int NOT NULL,
9 CONSTRAINT driver_pk
10 PRIMARY KEY (DRIVER_ID)
11 --CONSTRAINT driver_fk_account
12 --FOREIGN KEY (ACCOUNT_NUM)
13 --REFERENCES account (ACCOUNT_NUM)
14 );
Table created.
SQL>
SQL> CREATE TABLE account
2 (
3 ACCOUNT_NUM int NOT NULL,
4 DRIVER_ID varchar2(25) NOT NULL,
5 DEVICE_ID varchar2(25) NOT NULL,
6 DATE_CREATED date NOT NULL,
7 ACCOUNT_STATUS varchar2(10) NOT NULL,
8 CONSTRAINT account_pk
9 PRIMARY KEY (ACCOUNT_NUM, DRIVER_ID),
10 CONSTRAINT account_fk_driver
11 FOREIGN KEY (DRIVER_ID)
12 REFERENCES driver (DRIVER_ID)
13 );
Table created.
SQL>
SQL> alter table driver add constraint driver_fk_account
2 foreign key (account_num, driver_id)
3 references account (account_num, driver_id);
Table altered.
SQL>
SQL> CREATE TABLE vehicle
2 (
3 VIN_NUM varchar2(25) NOT NULL,
4 V_MILEAGE int NOT NULL,
5 V_GASUSED varchar2(25) NOT NULL,
6 V_ELECTRICALMILES int NOT NULL,
7 DRIVER_ID varchar2(25) NOT NULL,
8 CONSTRAINT vehicle_pk
9 PRIMARY KEY (VIN_NUM),
10 CONSTRAINT vehicle_fk_driver
11 FOREIGN KEY (DRIVER_ID)
12 REFERENCES driver (DRIVER_ID)
13 );
Table created.
SQL>
SQL> CREATE TABLE record
2 (
3 RECORD_ID varchar2(25) NOT NULL,
4 VIN_NUM varchar2(25) NOT NULL,
5 SERVER_ID varchar2(25) NOT NULL,
6 CONSTRAINT record_pk
7 PRIMARY KEY (RECORD_ID, VIN_NUM, SERVER_ID),
8 CONSTRAINT record_fk_vehicle
9 FOREIGN KEY (VIN_NUM)
10 REFERENCES vehicle (VIN_NUM),
11 CONSTRAINT record_fk_server
12 FOREIGN KEY (SERVER_ID)
13 REFERENCES server (SERVER_ID)
14 );
Table created.
SQL>
SQL> CREATE TABLE application
2 (
3 APP_ID varchar2(25) NOT NULL,
4 ACCOUNT_NUM varchar2(25) NOT NULL,
5 RECORD_ID varchar2(10) NOT NULL,
6 VERSION_ID varchar2(10) NOT NULL,
7 LAST_UPDATED date NOT NULL,
8 CONSTRAINT application_pk
9 PRIMARY KEY (APP_ID)
10 --CONSTRAINT application_fk_account
11 --FOREIGN KEY (ACCOUNT_NUM)
12 --REFERENCES account (ACCOUNT_NUM),
13 --CONSTRAINT application_fk_record
14 --FOREIGN KEY (RECORD_ID)
15 --REFERENCES record (RECORD_ID)
16 );
Table created.
SQL>

You have to add a / after every table definition end -
CREATE TABLE server
(
SERVER_ID varchar(25) NOT NULL,
SERVER_IP varchar(15) NOT NULL,
SERVER_LOCATION varchar(25) NOT NULL,
SERVER_BRAND varchar(15) NOT NULL,
CONSTRAINT server_pk
PRIMARY KEY (SERVER_ID)
);
/ ----> Here
CREATE TABLE application
(
APP_ID varchar(25) NOT NULL,
.....

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)

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

SQL name error with foreign key in sql code

I am getting ORA-00955 error which is name is already used by an existing object. I know it is in the foreign key constraint im trying to use. It happens in both foreign key constraints. I cant figure out why its happening. I tried renaming them to anything possible and I cant understand why it wont work.
DROP TABLE STUDENT;
CREATE TABLE STUDENT
(stuID VARCHAR (7) PRIMARY KEY NOT NULL,
major VARCHAR (15) NOT NULL,
firstName VARCHAR (15) NOT NULL,
lastName VARCHAR (15) NOT NULL,
yr VARCHAR(8) NOT NULL
);
DROP TABLE ENROLL;
CREATE TABLE ENROLL
(stuID VARCHAR (7) NOT NULL,
grade VARCHAR (1) NOT NULL,
courseID VARCHAR(7) NOT NULL,
CONSTRAINT fk_STUDENT
FOREIGN KEY (stuID)
REFERENCES STUDENT(stuid)
);
DROP TABLE CLASS;
CREATE TABLE CLASS
(instructorID VARCHAR(7) NOT NULL,
classRoomNumber VARCHAR(7) NOT NULL,
dateStarts DATE NOT NULL,
courseID VARCHAR(7) PRIMARY KEY NOT NULL,
description CHAR(100) NOT NULL,
CONSTRAINT fk_INSTRUCTOR
FOREIGN KEY (instructorID)
REFERENCES INSTRUCTOR(instructorid)
);
DROP TABLE INSTRUCTOR;
CREATE TABLE INSTRUCTOR
(firstName VARCHAR (15) NOT NULL,
lastName VARCHAR (15) NOT NULL,
departmentName VARCHAR (15) NOT NULL,
instructorID VARCHAR(7) PRIMARY KEY NOT NULL
);
In a comment, you said that there's still an error.
If you create (and/or drop) tables in a correct order, everything is OK. I prefer dropping them separately.
At first, tables don't exist so I'll just create them:
SQL> CREATE TABLE student (
2 stuid VARCHAR(7) PRIMARY KEY NOT NULL,
3 major VARCHAR(15) NOT NULL,
4 firstname VARCHAR(15) NOT NULL,
5 lastname VARCHAR(15) NOT NULL,
6 yr VARCHAR(8) NOT NULL
7 );
Table created.
SQL> CREATE TABLE enroll (
2 stuid VARCHAR(7) NOT NULL,
3 grade VARCHAR(1) NOT NULL,
4 courseid VARCHAR(7) NOT NULL,
5 CONSTRAINT fk_student FOREIGN KEY ( stuid )
6 REFERENCES student ( stuid )
7 );
Table created.
SQL> CREATE TABLE instructor (
2 firstname VARCHAR(15) NOT NULL,
3 lastname VARCHAR(15) NOT NULL,
4 departmentname VARCHAR(15) NOT NULL,
5 instructorid VARCHAR(7) PRIMARY KEY NOT NULL
6 );
Table created.
SQL> CREATE TABLE class (
2 instructorid VARCHAR(7) NOT NULL,
3 classroomnumber VARCHAR(7) NOT NULL,
4 datestarts DATE NOT NULL,
5 courseid VARCHAR(7) PRIMARY KEY NOT NULL,
6 description CHAR(100) NOT NULL,
7 CONSTRAINT fk_instructor FOREIGN KEY ( instructorid )
8 REFERENCES instructor ( instructorid )
9 );
Table created.
Drop tables in reverse order, so that detail is dropped before its master
SQL> DROP TABLE enroll;
Table dropped.
SQL> DROP TABLE student;
Table dropped.
SQL> DROP TABLE class;
Table dropped.
SQL> DROP TABLE instructor;
Table dropped.
SQL>
Table INSTRUCTOR is referenced by table CLASS, hence you heed to create table INSTRUCTOR before you create table CLASS.
Also, you should use DROP TABLE ... CASCADE CONSTRAINTS instead of just DROP TABLE .... This allows foreign and primary keys to be properly dropped at the same time as the table, and might avoid error name already used by existing object that you are currently getting.

ORA-00957 duplicate column name error, when trying to reference the same primary key with 3 foreign keys

I'm having problems with creating tables:
CREATE TABLE EMPLOYEE
(
employee_id NUMBER(5) NOT NULL UNIQUE,
position VARCHAR2(100) NOT NULL,
name VARCHAR2(255) NOT NULL,
salary NUMBER(6) NOT NULL
CONSTRAINT employee_pk PRIMARY KEY (employee_id)
);
CREATE TABLE PROJECT
(
project_id NUMBER(5) NOT NULL UNIQUE,
name VARCHAR(100) NOT NULL,
budget NUMBER(6) NOT NULL,
consultant_leader NUMBER(5) NOT NULL,
developer_leader NUMBER(5) NOT NULL,
project_leader NUMBER(5) NOT NULL,
CONSTRAINT project_pk PRIMARY KEY (PROJECT_ID),
CONSTRAINT fk_leader
FOREIGN KEY (consultant_leader, developer_leader, project_leader)
REFERENCES EMPLOYEE (employee_id, employee_id, employee_id)
);
In the last section, when I try to reference the employee's table employee_id, I'm getting ORA-00957. I think it's because the 3 different leader type foreign key references the same employee_id, but as far as I know, it should not be a problem. Is the syntax wrong?
Your immediate problem is that you need three foreign key relationships, not one with three columns.
But, there is no need to declare a primary key as being unique. So, I would recommend:
CREATE TABLE EMPLOYEE (
employee_id NUMBER(5) NOT NULL PRIMARY KEY,
position VARCHAR2(100) NOT NULL,
name VARCHAR2(255) NOT NULL,
salary NUMBER(6) NOT NULL
);
CREATE TABLE PROJECT (
project_id NUMBER(5) NOT NULL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
budget NUMBER(6) NOT NULL,
consultant_leader NUMBER(5) NOT NULL,
developer_leader NUMBER(5) NOT NULL,
project_leader NUMBER(5) NOT NULL,
CONSTRAINT fk_leader FOREIGN KEY (consultant_leader)
REFERENCES EMPLOYEE (employee_id),
CONSTRAINT fk_leader FOREIGN KEY (developer_leader)
REFERENCES EMPLOYEE (employee_id),
CONSTRAINT fk_leader FOREIGN KEY (project_leader)
REFERENCES EMPLOYEE (employee_id)
);
You don't need to put the PRIMARY KEY constraint in-line, of course. The advantage of declaring it separately is that you can give the constraint a name to your liking.
I think you should create three distinct FK: FK_Consultant, FK_developer, FK_projleader

Oracle SQL constraint, name entered must be a member

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