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

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

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

Missing Left Parenthesis Error - Creating Foreign Keys

I'm a bit confused where my missing left parenthesis is coming into play (ORA-00906: missing left parenthesis). Any suggestions? Below is an example of similar syntax with obvious test names. Thanks for any suggestions!
CREATE TABLE rosters (
rosterid NUMBER(5) PRIMARY KEY,
playerid NUMBER(5) NOT NULL,
teamid NUMBER(5) NOT NULL,
seasonid NUMBER(5) NOT NULL,
CONSTRAINT fk_player FOREIGN KEY playerid REFERENCES players(playerid),
CONSTRAINT fk_team FOREIGN KEY teamid REFERENCES teams(teamid),
CONSTRAINT fk_season FOREIGN KEY seasonid REFERENCES season(seasonid);
Seems fairly self-explanatory... you need the FK column name(s) to be in parentheses, as shown in the syntax diagram:
CREATE TABLE rosters (
rosterid NUMBER(5) PRIMARY KEY,
playerid NUMBER(5) NOT NULL,
teamid NUMBER(5) NOT NULL,
seasonid NUMBER(5) NOT NULL,
CONSTRAINT fk_player FOREIGN KEY (playerid) REFERENCES players(playerid),
-- ^ ^
CONSTRAINT fk_team FOREIGN KEY (teamid) REFERENCES teams(teamid),
-- ^ ^
CONSTRAINT fk_season FOREIGN KEY (seasonid) REFERENCES season(seasonid));
-- ^ ^ ^
I've added the missing closing parenthesis for the overall statement too.
Quick demo with made-up referenced tables:
CREATE TABLE players (playerid number(5) PRIMARY KEY);
Table PLAYERS created.
CREATE TABLE teams (teamid number(5) PRIMARY KEY);
Table TEAMS created.
CREATE TABLE season (seasonid number(5) PRIMARY KEY);
Table TEAMS created.
CREATE TABLE rosters (
rosterid NUMBER(5) PRIMARY KEY,
playerid NUMBER(5) NOT NULL,
teamid NUMBER(5) NOT NULL,
seasonid NUMBER(5) NOT NULL,
CONSTRAINT fk_player FOREIGN KEY (playerid) REFERENCES players(playerid),
CONSTRAINT fk_team FOREIGN KEY (teamid) REFERENCES teams(teamid),
CONSTRAINT fk_season FOREIGN KEY (seasonid) REFERENCES season(seasonid));
Table ROSTERS created.
You are missing a right parenthesis after season(seasonId).

missing left parenthesis, i cant find a mistake. can someone help me?

CREATE TABLE PRESCRIPTION
(
prescription_no NUMBER (7),
CONSTRAINT prescription_no_pk PRIMARY KEY,
pr_patient_no VARCHAR2(6),
CONSTRAINT pr_patient_no_fk FOREIGN KEY(patient) REFERENCES (patient_no),
pr_drug_no NUMBER(5),
CONSTRAINT pr_drug_no_fk FOREIGN KEY (drug) REFERENCES (drug_no),
drug_start_date DATE,
units_per_day NUMBER(3,2),
drug_end_date DATE
);
Try below query I altered your query :
CREATE TABLE PRESCRIPTION
(
prescription_no NUMBER (7) CONSTRAINT prescription_no_pk PRIMARY KEY,
pr_patient_no VARCHAR2(6) CONSTRAINT pr_patient_no_fk FOREIGN KEY(patient)
REFERENCES (patient_no),
pr_drug_no NUMBER(5) CONSTRAINT pr_drug_no_fk FOREIGN KEY (drug)
REFERENCES (drug_no),
drug_start_date DATE,
units_per_day NUMBER(3,2),
drug_end_date DATE
);
FOREIGN KEY constraint has the following syntax:
FOREIGN KEY (<this table's column name>) REFERENCES <another table>(<another table's column name>).
In your example, supposing that patient has the following schema:
CREATE TABLE patient (
patient_no VARCHAR2(6),
primary key (patient_no)
)
you reference it as follows:
CREATE TABLE PRESCRIPTION (
prescription_no NUMBER (7),
pr_patient_no VARCHAR2(6) ,
CONSTRAINT prescription_no_pk PRIMARY KEY (prescription_no),
CONSTRAINT pr_patient_no_fk FOREIGN KEY (pr_patient_no) REFERENCES patient(patient_no)
)
and so on.

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