SQL - Missing right parenthesis - sql

I am trying to execute this script in Oracle 11g and getting the following error, I dont know where I am missing the paranthesis or what is the mistake kindly help me figure this out.
Script:
CREATE TABLE User_Role (
user_role_id INT NOT NULL ,
Users_user_id INT FOREIGN KEY REFERENCES Users(user_id),
User_Types_user_type VARCHAR(20) FOREIGN KEY REFERENCES User_Types(user_type),
PRIMARY KEY(user_role_id)
)
Error:
ORA-00907: missing right parenthesi

Delete FOREIGN KEY clause. Rewrite your CREATE TABLE statement as follows:
CREATE TABLE User_Role (
user_role_id INT NOT NULL ,
Users_user_id INT REFERENCES Users(user_id),
User_Types_user_type VARCHAR(20) REFERENCES User_Types(user_type),
PRIMARY KEY(user_role_id)
)
In this case constraint names will be generated by Oracle. If you want to give them more meaningful names you could write your create table statement as follows:
CREATE TABLE User_Role1 (
user_role_id INT NOT NULL ,
Users_user_id INT ,
User_Types_user_type VARCHAR(20) ,
constraint PK_YourTable PRIMARY KEY(user_role_id),
constraint FK_Table_1 foreign key(Users_user_id) REFERENCES Users(user_id),
constraint FK_Table_2 foreign key(User_Types_user_type) REFERENCES User_Types(user_type)
)

I think you need to define the columns first and then add the FOREIGN KEY constraint in the very same manner as you are adding PRIMARY KEY constraint as below:
CREATE TABLE User_Role (
user_role_id INT NOT NULL ,
Users_user_id INT,
User_Types_user_type VARCHAR(20),
FOREIGN KEY (Users_user_id) REFERENCES Users(user_id),
FOREIGN KEY (User_Types_user_type) REFERENCES User_Types(user_type),
PRIMARY KEY(user_role_id)
)

You can specify foreign keys inline, you just need to remove the foreign key keyword:
CREATE TABLE User_Role
(
user_role_id INT NOT NULL ,
Users_user_id INT REFERENCES Users,
User_Types_user_type VARCHAR(20) REFERENCES User_Types,
PRIMARY KEY(user_role_id)
);
SQLFiddle example: http://sqlfiddle.com/#!4/4ca9f/1
Listing the primary key columns in the "references" part is optional. If you prefer, you can also write REFERENCES Users(user_id)
This format also has the disadvantage that the constraint names will be generated by Oracle (with a meaningless name). In order to be able to properly specify a constraint name for the foreign key, you need to use the syntax in the accepted answer.

remove the size of int and recompile
Example :-
integer (20) not null
integer not null
User_Types_user_type VARCHAR(20),
User_Types_user_type VARCHAR,

Related

Add constraints for a foreign key that references multipleprimary keys from different tables in SQL Plus?

I'm a beginner learning SQL and am having trouble implementing this concept.
Suppose you create the following three tables:
CREATE TABLE dogOwner(
ownerNo VARCHAR(8) CONSTRAINT ownerNo_pk1 PRIMARY KEY,
ownerName VARCHAR(10)
);
CREATE TABLE catOwner(
ownerNo VARCHAR(8) CONSTRAINT ownerNo_pk2 PRIMARY KEY,
ownerName VARCHAR(10)
);
CREATE TABLE petsAdopted(
petNo VARCHAR(8) CONSTRAINT petNo_pk PRIMARY KEY,
ownerNo VARCHAR(8) CONSTRAINT ownerNo_fk1 REFERENCES dogOwner(ownerNo)
CONSTRAINT ownerNo_fk2 REFERENCES catOwner(ownerNo)
);
How do you properly create constraints for the foreign key ownerNo, which references ownerNo from two other tables?
You can't. You could have 2 columns in petsAdopted: dogOwnerNo and catOwnerNo and 2 foreign keys. But the table design doesn't seem to make sense: surely a pet either is a dog or a cat (or something else) regardless of who owns it?
Here is an alternative design:
CREATE TABLE owner(
ownerNo VARCHAR(8) CONSTRAINT ownerNo_pk2 PRIMARY KEY,
ownerName VARCHAR(10)
);
CREATE TABLE petsAdopted(
petNo VARCHAR(8) CONSTRAINT petNo_pk PRIMARY KEY,
petType VARCHAR2(10) NOT NULL CONSTRAINT petTypeChk (CHECK petType in ('CAT','DOG'))
ownerNo VARCHAR(8) CONSTRAINT ownerNo_fk REFERENCES owner(ownerNo)
);
This only address syntax part of your answer which You have got lot of syntax error, but this will not work, Please rethink your design.
CREATE TABLE dogOwner(
ownerNo VARCHAR(8) CONSTRAINT ownerNo_pk1 PRIMARY KEY,
ownerName VARCHAR(10)
);
CREATE TABLE catOwner(
ownerNo VARCHAR(8),
ownerName VARCHAR(10),
CONSTRAINT ownerNo_pk2 PRIMARY KEY (ownerNo),
CONSTRAINT ownerNo_fk1 FOREIGN KEY (ownerNo) REFERENCES dogOwner(ownerNo)
);
CREATE TABLE petsAdopted(
petNo VARCHAR(8) ,
ownerNo VARCHAR(8),
CONSTRAINT petNo_pk PRIMARY KEY (petNo),
CONSTRAINT ownerNo_fk_pet1 FOREIGN KEY (ownerNo) REFERENCES dogOwner(ownerNo),
CONSTRAINT ownerNo_fk_pet2 FOREIGN KEY (ownerNo) REFERENCES catOwner(ownerNo)
);

SQL Constraint names

If we have query for creating table like this..
create table if not exists food
(
id int not null auto_increment,
user_id int,
name varchar(30),
constraint pk_food primary key(id,name),
foreign key(user_id) references userss(id)
);
What does pk_food mean in this example? I know this is a constraint name, but for what we should be give a name for constraint, if its working without?
create table if not exists food
(
id int not null auto_increment,
user_id int,
name varchar(30),
primary key (id, name),
foreign key (user_id) references userss(id)
);
I mean.. how to use these names and for what we need it?
You gives constraints names for basically two reasons:
You can better understand the error message when the constraint is violated.
You can more easily find the constraint if you want to delete it.

SQL : ORA-00906: missing left parenthesis

I met a problem with CREATE TABLE:
Here is my instruction :
CREATE TABLE PRATICIEN (
num_pra INTEGER PRIMARY KEY,
nom_pra VARCHAR(30) NOT NULL,
FOREIGN KEY code_etage REFERENCES ETAGE(code_etage));
ORA-00906: missing left parenthesis 00906. 00000 - "missing left parenthesis"
ETAGE exists and this table was created without error :
CREATE TABLE ETAGE (
code_etage SMALLINT PRIMARY KEY,
designation VARCHAR(30));
You need parentheses around the foreign key reference. That is how the syntax is defined.
You also need to declare the column. The FOREIGN KEY is an attribute of a column, not a column definition:
CREATE TABLE PRATICIEN (
num_pra INTEGER PRIMARY KEY,
nom_pra VARCHAR(30) NOT NULL,
code_etage SMALLINT,
FOREIGN KEY (code_etage) REFERENCES ETAGE(code_etage)
);
Here is a db<>fiddle.
You could simply remove FOREIGN KEY keyword:
CREATE TABLE PRATICIEN (
num_pra INTEGER PRIMARY KEY,
nom_pra VARCHAR(30) NOT NULL,
code_etage REFERENCES ETAGE(code_etage) -- please note that type is inferred
);
db<>fiddle demo
Simplest would be
create table praticien
( num_pra integer primary key
, nom_pra varchar2(30) not null
, code_etage references etage );
However, specifying the referenced column might be considered best practice, in case ETAGE has more than one unique constraint:
create table praticien
( num_pra integer primary key
, nom_pra varchar2(30) not null
, code_etage references etage(code_etage) );
(By the way, note that it's varchar2 in Oracle, not varchar.)

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)

ORA-00904: : invalid identifier two references in Oracle

I cant find a good anwser too my question, I want to create a table that have references too one table but i use it twice in the code..
Vårdnadshavare VARCHAR(11),
FOREIGN KEY (Vårdnadshavare) REFERENCES Person(Personnummer),
Barn VARCHAR(11),
FOREIGN KEY (Barn) REFERENCES Person(Personnummer)
But then i get the error:
ORA-02256: number of referencing columns must match referenced columns
I understand that my reference is wrong some how but can´t figure it out...
Sorry for the swedish words!!
Create table Ärende(
Ärendenr VARCHAR(50) NOT NULL,
Handläggare VARCHAR(50),
FOREIGN KEY (Handläggare) REFERENCES Handläggare(Anställningsnr),
Vårdnadshavare VARCHAR(11),
FOREIGN KEY (Vårdnadshavare) REFERENCES Person(Personnummer),
Barn VARCHAR(11),
FOREIGN KEY (Barn) REFERENCES Person(Personnummer),
In Datum VARCHAR(50),
Ömmande Skäl VARCHAR(5),
Förskola VARCHAR(50),
FOREIGN KEY (Förskola) REFERENCES Förskola(IDnr),
PRIMARY KEY (Ärendenr)
);
I have used Person(Personnumer) as a reference before just not twice in createing a table..
I created this script which does work. Probably there is an issue with your indexes:
create table handläggare
( anställningsnr varchar(50) primary key
)
create table person
( personnummer varchar(11) primary key
)
;
create table förskola
( idnr varchar(50) primary key
);
create table ärende
( ärendenr varchar(50) not null
, handläggare varchar(50)
, foreign key (handläggare) references handläggare(anställningsnr)
, vårdnadshavare varchar(11)
, foreign key (vårdnadshavare) references person(personnummer)
, barn varchar(11)
, foreign key (barn) references person(personnummer)
, indatum varchar(50)
, ömmandeskäl varchar(5)
, förskola varchar(50)
, foreign key (förskola) references förskola(idnr)
, primary key (ärendenr)
);
There are two other issues in your script above which I edited:
In Datum should be one name, not separated by a space;
Ömmande Skäl, same as above.
In you create statement you have provided 4 referenes:
Handläggare VARCHAR(50),
FOREIGN KEY (Handläggare) REFERENCES Handläggare(Anställningsnr),
Vårdnadshavare VARCHAR(11),
FOREIGN KEY (Vårdnadshavare) REFERENCES Person(Personnummer),
Barn VARCHAR(11),
FOREIGN KEY (Barn) REFERENCES Person(Personnummer),
Förskola VARCHAR(50),
FOREIGN KEY (Förskola) REFERENCES Förskola(IDnr),
Please check that all 3 distinct columns are of same data type.
That means:
Handläggare(Anställningsnr) should be VARCHAR(50)
Person(Personnummer) should be VARCHAR(11)
Förskola(IDnr) should be VARCHAR(50).
As per me i think you are making a mistake in
Förskola(IDnr) should be VARCHAR(50)
which should have been a Number