Oracle error building table from 2 foreign keys - sql

I'm in the process of building a database and have already successfully created 2 primary key tables however when I try to bring them in as 2 foreign keys to another table I am running into a
"CLIENTID" invalid identifier
Unsure how to resolve as not the best at this.
CREATE TABLE Booking(
BookingID number(10) NOT NULL,
CONSTRAINT Client_FK FOREIGN KEY (ClientID) REFERENCES client (ClientID),
CONSTRAINT Course_FK FOREIGN KEY (CourseID) REFERENCES course (CourseID),
CONSTRAINT Booking_PK PRIMARY KEY (ClientID, CourseID)
);

You are missing the columns on which your primary key and foreign keys are created; you need something like the following, to be edited with the right type for your columns:
CREATE TABLE Booking(
BookingID number(10) NOT NULL,
ClientId number, -- missing
CourseID number, -- missing
CONSTRAINT Client_FK FOREIGN KEY (ClientID) REFERENCES client (ClientID),
CONSTRAINT Course_FK FOREIGN KEY (CourseID) REFERENCES course (CourseID),
CONSTRAINT Booking_PK PRIMARY KEY (ClientID, CourseID)
)

Related

How to solve ORA-00907: missing right parenthesis error?

I am unable to find an error in this code, it shows ORA-00907: missing right parenthesis for both. I'm doing this on Oracle live SQL.
CREATE table Final_chart
(
T_id int FOREIGN KEY REFERENCES Train(T_id),
User_id varchar(10) FOREIGN KEY REFERENCES Passenger(User_id),
Seat_id int FOREIGN KEY REFERENCES Train_Seats(Seat_id),
CONSTRAINT PNR PRIMARY KEY (T_id,User_id,Seat_id)
)
CREATE table Train_seats
(
T_id int FOREIGN KEY REFERENCES Train(T_id),
Seat_id int PRIMARY KEY,
Waiting int NOT NULL,
Available int NOT NULL,
Booked_seat int NOT NULL
)
Working example for one table. And please use varchar2 instead on varchar in oracle.
CREATE table Final_chart
(T_id integer,
User_id varchar2(10),
Seat_id integer,
CONSTRAINT t2_fk FOREIGN KEY (T_id) REFERENCES Train(T_id),
CONSTRAINT t1_fk FOREIGN KEY (User_id) REFERENCES Passenger(User_id),
CONSTRAINT t3_fk FOREIGN KEY (Seat_id) REFERENCES Train_Seats(Seat_id),
CONSTRAINT Pkr PRIMARY KEY (T_id, User_id, Seat_id)
)
The problem is your reference to the foreign key. this is the way you should do it
CONSTRAINT FK_Train FOREIGN KEY (T_id) REFERENCES Train(T_id)

Make attribute primary and foreign key in sql

How can I make an attribute a primary key within a table but also as a foreign key which references another table using sql in sql developer?
I know how to make it an attribute as a foreign key and a primary separate but not as a primary key as well as a foreign key
That's perfectly normal. For example:
create table employee (
id number(6) primary key not null,
name varchar2(50)
);
create table employee_desk (
desk_id number(6) primary key not null, -- PK and FK!
location varchar2(20),
constraint fk1 foreign key (desk_id) references employee (id)
);
The column desk_id is the primary key of the table employee_desk, and also a foreign key that points to the table employee.
Below is the example of primary key with foreign key
create table animals (id integer primary key);
create table cats (
id integer primary key
, name varchar(100) not null
, constraint d_cats_animals_fk foreign key (id) references animals (id)
);

Recursive relationship SQL error

I am pretty new to SQL and I have a problem. I want to make a recursive relationship (a table that relates to itself), but I get an error when I try to execute my code. It's working fine without the Coordinator_Office_ID foreign key.
The error is:
The number of columns in the foreign-key referencing list is not equal
to the number of columns in the referenced list.
Create table Logistican (
Office_ID Number(10) Constraint nb_office Not NULL,
Worker_ID Number(15) Constraint lg_worker not null,
Name_logistican Varchar(20),
Room Varchar(10) constraint log_room UNIQUE,
Coordinator_Office_ID Integer,
Primary key (Office_ID, Worker_ID),
Constraint work_id Foreign key (Worker_ID) References worker(worker_ID) on delete cascade,
Constraint lg_cord_id Foreign key (Coordinator_Office_ID) References Logistican(Office_ID)
);
Yes, that's cause you have defined composite primary key like Primary key (Office_ID, Worker_ID) and thus your FK should include both of them else it will result in PFD (partial functional dependency)
Add the constraint with alter table:
Create table Logistican (
Office_ID Number(10) Constraint nb_office Not NULL,
Worker_ID Number(15) Constraint lg_worker not null,
Name_logistican Varchar(20),
Room Varchar(10) constraint log_room UNIQUE,
Coordinator_Office_ID Integer,
Primary key (Office_ID, Worker_ID),
Constraint work_id Foreign key (Worker_ID) References worker(worker_ID) on delete cascade
);
alter table Logistican
add Constraint lg_cord_id
Foreign key (Coordinator_Office_ID, Worker_Id) References Logistican(Office_ID, Worker_Id);
The relationship needs all elements of the primary key to be valid. I'm not sure if it needs to be a separate statement in Oracle.

Invalid FK and PK reference

Unable to create table as oracle shows ' no matching unique or primary key for this column-list' when I did label the primary key reference for the required table.
First table created successfully:
CREATE TABLE TEST
(
TESTno VARCHAR2(6) NOT NULL,
ExamNo VARCHAR2(6) NOT NULL,
TEST_Date DATE NOT NULL,
ACTUAL DATE,
PREDICTED Date,
CONSTRAINT TESTPKs PRIMARY KEY (TEST_Date, TESTno, ExamNo),
CONSTRAINT TTESTNO_Fk FOREIGN KEY (TESTno) REFERENCES TESTPAPER (Flightno)
CONSTRAINT TEXAMNo_FK FOREIGN KEY (ExamNo) REFERENCES Exam (ExamNo)
);
Here's the table i want to create and gives me error:
CREATE TABLE Assignment
(
TEST_Date DATE NOT NULL,
ExamNo VARCHAR2(6) NOT NULL,
TestNo VARCHAR2(6) NOT NULL,
Type VARCHAR2(20),
Hours_Spent Decimal(4,2),
CONSTRAINT ASSIGNPKS PRIMARY KEY (TEST_Date, TestNo , ExamNo),
CONSTRAINT ASSIGNTESTDATE_FK FOREIGN KEY (TEST_Date) REFERENCES TEST(TEST_Date) ON
DELETE CASCADE,
CONSTRAINT ASSIGNTESTNO_FK FOREIGN KEY (TESTno) REFERENCES TESTPAPER (Flightno)
CONSTRAINT TEXAMNo_FK FOREIGN KEY (ExamNo) REFERENCES Exam (ExamNo)
);
May i know where's the issue that it keeps giving me no matching unique primary keys? I already tried to recreate and labelled the 'test_Date' as my primary key. But oracle can't seems to find.
Thanks
The PK you refer to is PRIMARY KEY (TEST_Date, TESTno, ExamNo) — hence the foreign key should be FOREIGN KEY (TEST_Date, TESTno, ExamNo) as well. The error you're getting is due to your attempt to refer to a part of TEST's PK.
See also http://download.oracle.com/docs/cd/B10500_01/server.920/a96524/c22integ.htm
Check the tables you are referencing in your foreign keys. Those columns must be the primary key or otherwise unique on the foreign table.

SQL Script Foreign Key Error

I am getting the error
"Number of referencing columns in foreign key differs from number of referenced columns, table 'StudentGrade'" when trying to execute the following SQL script
CREATE TABLE StudentGrade
(
StudentID INT NOT NULL
CONSTRAINT FK_SG_StudentID FOREIGN KEY (StudentID)
REFERENCES Student(StudentID),
ClassID VARCHAR (6) NOT NULL
CONSTRAINT FK_Class FOREIGN KEY (ClassID, CourseID)
REFERENCES Class(ClassID),
CourseID VARCHAR (64) NOT NULL
CONSTRAINT FK_Course FOREIGN KEY (CourseID)
REFERENCES Course(CourseID),
FacultyID INT NOT NULL
CONSTRAINT FK_Faculty FOREIGN KEY (FacultyID)
REFERENCES Faculty(FacultyID),
Grade NUMERIC NULL,
CONSTRAINT PK_StudentID PRIMARY KEY (StudentID, ClassID, CourseID, FacultyID)
)
I know that there is something I am doing wrong with the foreign keys though I can't find anywhere where it explains how to use foreign keys and composite keys together. Any help would be greatly appreciated. Thank you so much!
change your second foreign key from
CONSTRAINT FK_Class FOREIGN KEY (ClassID, CourseID)
to
CONSTRAINT FK_Class FOREIGN KEY (ClassID)
In FK_Class you are referencing the columns StudentGrade.ClassID and StudentGrade.CourseID into a single one Class.ClassID and this cannot work.
Your FK_Course already refers CourseID, so you can simply delete CourseID from FK_Class as I said above.
Edit
Add CourseID to your definition of FK_Class as
CONSTRAINT FK_Class FOREIGN KEY (ClassID, CourseID)
REFERENCES Class(ClassID, CourseID)