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

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)

Related

SQL set FOREIGN KEY to a non unique value throws error in sql developer

I have a N:M situation, but I can't use any of the 2 non unique values as a Foreign Key can anyone help?? Both Platform_Id and Station_Id can not be set as a foreign key in the Platform_Host table.
CREATE TABLE Repair_Platform
(
Platform_Id INT NOT NULL,
Station_Id INT NOT NULL,
Mechanic_Id INT NOT NULL UNIQUE,
Validation_Num INT NOT NULL,
CONSTRAINT Platform_fk1
FOREIGN KEY (Station_Id) REFERENCES Repair_Station (Station_Id),
CONSTRAINT Platform_fk2
FOREIGN KEY (Mechanic_Id) REFERENCES Engineer (Engineer_Id),
CONSTRAINT Platform_pk
PRIMARY KEY (Station_Id, Platform_Id)
);
CREATE TABLE Platform_Host
(
Station_Id INT NOT NULL,
Platform_Id INT NOT NULL,
Vehicle_Id VARCHAR(8) NOT NULL,
DateTime DATE NOT NULL,
CONSTRAINT Host_fk1
FOREIGN KEY (Station_Id) REFERENCES Repair_Platform (Station_Id),
CONSTRAINT Host_fk2
FOREIGN KEY (Vehicle_Id) REFERENCES Vehicle (License_Plate),
CONSTRAINT Host_fk3
FOREIGN KEY (Platform_Id) REFERENCES Repair_Platform (Platform_Id)
);
Error:
ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement gives a column-list for which there is no matching unique or primary key constraint in the referenced table.
< *Action: Find the correct column names using the ALL_CONS_COLUMNS catalog view
You need to reference the whole primary key of Repair_Platform - like this:
CREATE TABLE Platform_Host
(
-- columns
-- constraints
CONSTRAINT Host_fk3
FOREIGN KEY (StationId, Platform_Id)
REFERENCES Repair_Platform (StationId, Platform_Id)
);
You cannot reference only parts of a primary key - it's an all or nothing deal.

Where did ERROR at line 2: ORA-00907: missing right parenthesis here come from?

CREATE TABLE Film_Language (
Film_Id int FOREIGN KEY REFERENCES Film(Film_Id),
Language_Name varchar2(10) FOREIGN KEY REFERENCES Language(Language_Name),
CONSTRAINT pk_filmlang PRIMARY KEY(Film_Id,Language_Name)
);
CREATE TABLE Film_Language (
Film_Id int,
Language_Name varchar2(10),
constraint Film_Language_fk1 FOREIGN KEY(Film_Id) REFERENCES Film(Film_Id),
constraint Film_Language_fk2 FOREIGN KEY(Language_Name) REFERENCES Language(Language_Name),
CONSTRAINT pk_filmlang PRIMARY KEY(Film_Id,Language_Name)
);
You dont need the FOREIGN KEY here:
Film_Id int FOREIGN KEY REFERENCES Film(Film_Id)
, (similar for Language_Name ), just REFERENCES(...) is enough:
CREATE TABLE Film_Language (
Film_Id int REFERENCES Film(Film_Id)
, Language_Name varchar2(10) REFERENCES Language(Language_Name)
, CONSTRAINT pk_filmlang PRIMARY KEY(Film_Id,Language_Name)
);

Oracle error building table from 2 foreign keys

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

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)

I am creating a database and i keep getting "INVALID IDENTIFIER" Error

CREATE TABLE Review(
VersionID VARCHAR(5) PRIMARY KEY NOT NULL,
rating INT,
FOREIGN KEY (PaperID) REFERENCES Paper(PaperID),
FOREIGN KEY (Revi) REFERENCES Paper(PaperID)
);
You should do it this way:
CREATE TABLE Review(
VersionID VARCHAR(5) PRIMARY KEY NOT NULL,
rating INT,
constraint constraint_1_name FOREIGN KEY (PaperID) REFERENCES Paper(PaperID),
constraint constraint_2_name FOREIGN KEY (Revi) REFERENCES Paper(PaperID)
);
(Of course instead of paperId/Revi you should use existing field names...)