SQL : ORA-00906: missing left parenthesis - sql

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

Related

Column exist but program don t see? [duplicate]

This question already has answers here:
SQLite Foreign Key
(5 answers)
Closed 1 year ago.
I created a 3 tables but in the last one shows errors
CREATE TABLE Student
(
St_Id char(7) PRIMARY KEY,
St_Fname varchar(15) NOT NULL,
St_Lname varchar(20) NOT NULL,
St_DOB date
)
CREATE TABLE Course
(
Course_code char(5) PRIMARY KEY,
Course_title varchar(30) NOT NULL,
Course_credit INTEGER NOT NULL
)
CREATE TABLE Registration
(
Reg_no INTEGER PRIMARY KEY AUTOINCREMENT ,
FOREIGN KEY (St_Id) REFERENCES Student(St_Id),
FOREIGN KEY (Course_code) REFERENCES Course(Course_code),
Mark_obtaines INTEGER
)
and error is
Execution finished with errors. Result: unknown column "St_Id" in
foreign key definition At line 1: CREATE TABLE Registration ( Reg_no
INTEGER PRIMARY KEY AUTOINCREMENT , FOREIGN KEY (St_Id) REFERENCES
Student(St_Id),
In order to define a foreign key, you need to define the column first -- and the types need to match the type in the referenced table:
CREATE TABLE Registration (
Reg_no INTEGER PRIMARY KEY AUTOINCREMENT,
St_Id CHAR(7),
Course_Code CHAR(5),
FOREIGN KEY (St_Id) REFERENCES Student(St_Id),
FOREIGN KEY (Course_code) REFERENCES Course(Course_code),
Mark_obtaines INTEGER
);
Then the FOREIGN KEY declaration provides more information about the column.

I'm trying to create a primary key from 2 columns, but it doesn't work well

I'm learning Oracle by myself.
Here's my code:
create table Schedule
(
Schedule_SN number(10) primary key,
ScreeningDate date not null,
Price number(6) not null
);
create table Seat
(
Schedule_SN number(10) REFERENCES Schedule(Schedule_SN),
Seat_SN varchar2(4) not null
);
create table Reservation
(
Reservation_SN number(15) primary key,
DCtype number(2) not null,
DCamount number(7),
PaymentMethod number(1) not null,
TotalPrice number(7) not null,
ReservationDate date not null
);
create table Reservation_details ** I need help here **
(
Reservation_SN number(15) REFERENCES Reservation(Reservation_SN),
Schedule_SN number(10) REFERENCES Schedule(Schedule_SN),
Seat_SN varchar2(10) REFERENCES Seat(Seat_SN),
CONSTRAINT Reservation_detailesPK primary key (Reservation_SN, Schedule_SN)
);
Error messages:
Errors - 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
How can I make my 2 columns (Reservation_SN, Schedule_SN) into a primary key?
The problem is with seat_sn. You want child column in reservation_details to reference parent column in seat, but the parent column is not a primary or unique key. Actually, seat has no primary key; just make seat_sn the primay key of this table (if this fits your use case), and the rest should run fine:
create table seat (
schedule_sn nmber(10) references schedule(schedule_sn),
seat_sn varchar3(4) primary key
)
Demo on DB Fiddle

Foreign key in the first table

I have a question about foreign keys.
How does it work when I want to add a foreign key to the first table that I make that references to the primary key of the second table I create?
CREATE TABLE table1
(
name_id INT NOT NULL,
team TEXT REFERENCES table2(team_id),
PRIMARY KEY(name_id)
);
CREATE TABLE table2
(
team_id INT NOT NULL,
teamname TEXT,
PRIMARY KEY(team_id)
);
If I try the code above I get the following error:
ERROR: relation "" does not exist
Thanks in advance.
Either create the second table first. Or use alter table. That is, create the first table without the reference and then do:
alter table table1 add constraint fk_table1_team
foreign key (team_id) REFERENCES table2(team_id);
The declaration for table1 would be:
CREATE TABLE table1 (
name_id INT NOT NULL,
team_id INT,
PRIMARY KEY(name_id)
);
The reference between the tables should be on the primary key and certainly not on a character column, if an integer is available.
here's the syntax of creating a table with Foreign key:
CREATE TABLE table11
(
name_id INT NOT NULL,
team INT,
PRIMARY KEY(name_id),
foreign key(team) references table22(team_id)
);
CREATE TABLE table22
(
team_id INT NOT NULL,
teamname TEXT,
PRIMARY KEY(team_id)
);
but there was another problem. a foreign key from a child table cannot reference to a primary key from a parent folder if they do not contain the same type. in your code team was of TEXT and team_id was of INT which cannot be.

ORA-01748: only simple column names allowed here in Oracle

What I am trying to do ?
I am trying to create two tables and at the same time i am trying to link them together using foreign and primary keys. However I successfully create my parent table ( Student with primary key ) but failed to create child table ( Attendence with foreign key ).
What is the problem ?
I get the following error while creating Attendence table:
ERROR at line 5: ORA-01748: only simple column names allowed here
My code:
Student table:
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Attendence table:
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk Attendence.ST_ROLLNO foreign key references Student(ST_ROLLNO)
);
Your foreign key constraint syntax is wrong; it should be:
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
You are preceding the FK column name with the table name, which is wrong in itself, but also have it in the wrong place.
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Table STUDENT created.
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
);
Table ATTENDENCE created.
According to oracle documentation,
ORA ERR
ORA-01748 only simple column names allowed here
The following is the cause of this error:
This SQL statement does not allow a qualified column name, such as
username.table.column or table.column.
Action you can take to resolve this issue: Remove the qualifications
from the column and retry the operation.
In your case, you are trying to refer to the table name while defining a constraint -
Attendence.ST_ROLLNO - WRONG.
It must contain a simple name without the table name or schema name.

SQL - Missing right parenthesis

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,