Error with oracle ORA-00907: missing right parenthesis - sql

I'm using Oracle10g Express edition and I tried to create this tables but an Error appeared and I need some help fixing the "ORA-00907: missing right parenthesis" problem. I searched for a solution to this error and it looks like the main reason is not the "missing right parenthesis" but I still can't fix the code.
CREATE TABLE Pays
(
codePays NUMBER(4) CONSTRAINT pk_Pays PRIMARY KEY,
nomPays VARCHAR(20)
);
CREATE TABLE Equipe
(
codeEquipe NUMBER(4) CONSTRAINT pk_Equipe PRIMARY KEY,
nomEquipe VARCHAR(4),
);
CREATE TABLE Etape
(
numEtape NUMBER(4) CONSTRAINT pk_Etape PRIMARY KEY,
);
CREATE TABLE Coureur
(
numCoureur NUMBER(4) CONSTRAINT pk_Coureur PRIMARY KEY,
codeEquipe NUMBER(4),
codePays NUMBER(4),
CONSTRAINT FK_Equipe_Coureur FOREIGN KEY(codeEquipe) REFERENCES Equipe(codeEquipe);
CONSTRAINT FK_Pays_Coureur FOREIGN KEY(codePays) REFERENCES Pays(codePays);
);

You have two semicolons in the code for creating the Coureur table. You also have a dangling comma in the code for creating the Equipe table. Replace your code with this:
CREATE TABLE Pays
(
codePays NUMBER(4) CONSTRAINT pk_Pays PRIMARY KEY,
nomPays VARCHAR(20)
);
CREATE TABLE Equipe
(
codeEquipe NUMBER(4) CONSTRAINT pk_Equipe PRIMARY KEY,
nomEquipe VARCHAR(4)
);
CREATE TABLE Etape
(
numEtape NUMBER(4) CONSTRAINT pk_Etape PRIMARY KEY
);
CREATE TABLE Coureur
(
numCoureur NUMBER(4) CONSTRAINT pk_Coureur PRIMARY KEY,
codeEquipe NUMBER(4),
codePays NUMBER(4),
CONSTRAINT FK_Equipe_Coureur FOREIGN KEY(codeEquipe) REFERENCES Equipe(codeEquipe),
CONSTRAINT FK_Pays_Coureur FOREIGN KEY(codePays) REFERENCES Pays(codePays)
);
Here is what I believe is happening leading to the exact error you are seeing. Your Oracle workbench is parsing the Coureur table definition, and it hits a semicolon on the line for the FK_Equipe_Courer constraint. It interprets this as the end of the table definition, but it doesn't see a closing right parenthesis before this semicolon, so it gives you the error you see.

Related

ORA-00907: missing right parenthesis but I cant see it?

So this is my code for a homework
CREATE TABLE workorders
(
wo# NUMBER(5) PRIMARY KEY,
proj# VARCHAR(10) NOT NULL FOREIGN KEY REFERENCES project(proj#),
wo_desc VARCHAR(30) NOT NULL UNIQUE,
wo_assigned VARCHAR(30),
wo_hours NUMBER(5) NOT NULL CHECK(wo_hours>0),
wo_start DATE,
wo_due DATE,
wo_complete CHAR(1),
CONSTRAINT workorders_wo_complete_chk CHECK(wo_complete in('Y','N'))
);
I could not figure out why oracle apex won't let me create this table, it says
ORA-00907: missing right parenthesis
But I double-checked so many times and I think I do have all the parenthesis? What did I do wrong here?
Thanks in advance
I just want to create this table under these constreaints but I could find any errors that I know of.
Oracle's error messages are often not particularly helpful, and the best thing to do is to go through your code, line by line, commenting out each line until you isolate the problem.
In this case the offending line is
proj# VARCHAR(10) NOT NULL FOREIGN KEY REFERENCES project(proj#),
and the problem is that you don't use the words FOREIGN KEY to define a foreign key - you just need to specify what it references. So this should be
proj# VARCHAR(10) NOT NULL REFERENCES project(proj#),
and then you'll be fine.
db<>fiddle here
When defining an inline constraint, the FOREIGN KEY terms are not used:
proj# VARCHAR2(10) NOT NULL
REFERENCES project(proj#)
Note: Oracle uses VARCHAR2 and VARCHAR is an alias to VARCHAR2 and it is considered best-practice to use VARCHAR2 throughout.
However, those keywords are required when you define an out-of-line foreign key (but then you would be missing a comma and the column identifier):
proj# NOT NULL,
FOREIGN KEY (proj#) REFERENCES project (proj#)
Note: If you are using a foreign key constraint then you do not need to define the data type and Oracle will implicitly use the data type of the column being referenced.
The complete code using (named) inline constraints throughout would be:
CREATE TABLE workorders
(
wo# NUMBER(5)
CONSTRAINT workorders__wo#__pk PRIMARY KEY,
proj# -- Note: do not need the data type as the FK will define it.
NOT NULL
CONSTRAINT workorders__proj#__fk REFERENCES project(proj#),
wo_desc VARCHAR2(30)
NOT NULL
CONSTRAINT workorders__wo_desc__u UNIQUE,
wo_assigned VARCHAR2(30),
wo_hours NUMBER(5)
NOT NULL
CONSTRAINT workorders__wo_hours__chk CHECK(wo_hours>0),
wo_start DATE,
wo_due DATE,
wo_complete CHAR(1)
CONSTRAINT workorders_wo_complete_chk CHECK(wo_complete in('Y','N'))
);
and using (named) out-of-line constraints throughout:
CREATE TABLE workorders
(
wo# NUMBER(5),
proj# -- Note: do not need the data type as the FK will define it.
NOT NULL,
wo_desc VARCHAR2(30)
NOT NULL,
wo_assigned VARCHAR2(30),
wo_hours NUMBER(5)
NOT NULL,
wo_start DATE,
wo_due DATE,
wo_complete CHAR(1),
CONSTRAINT workorders__wo#__pk PRIMARY KEY(wo#),
CONSTRAINT workorders__proj#__fk FOREIGN KEY (proj#) REFERENCES project(proj#),
CONSTRAINT workorders__wo_desc__u UNIQUE(wo_desc),
CONSTRAINT workorders_wo_complete_chk CHECK(wo_complete in('Y','N')),
CONSTRAINT workorders__wo_hours__chk CHECK(wo_hours>0)
);
fiddle

Getting "missing right parenthesis' error SQL--Cannot find the issue

I am trying to create four tables below. My first two tables run correctly, but my last two error out with the same error message:
ORA-00907: missing right parenthesis
ORA-06512: at "SYS.WWV_DBMS_SQL_APEX_200200", line 626
ORA-06512: at "SYS.DBMS_SYS_SQL", line 1658
ORA-06512: at "SYS.WWV_DBMS_SQL_APEX_200200", line 612
ORA-06512: at "APEX_200200.WWV_FLOW_DYNAMIC_EXEC", line 1749
I cannot figure out what is wrong with this code. I recognize that it's probably a syntax error somewhere, but I cannot find it. What should I do to fix this error?
create table item
(
itemID number(5) constraint CAMP_itemID_pk primary key,
itemDescription varchar2(100),
cleaned varchar2(1),
stock number(5)
);
create table people
(
peopleID number(10) constraint CAMP_peopleID_pk primary key,
firstName varchar2(20),
lastName varchar2(20),
phone number(10),
discount varchar2(20)
);
create table checkout
(
checkoutID constraint CAMP_checkoutID_pk primary key number(10),
peopleID number(10),
checkoutDate date(),
returnDate date(),
saleTotal number(10),
returned varchar(1),
constraint CAMP_peopleID_fk foreign key (peopleID) references people(peopleID)
);
create table checkoutDetail
(
orderDetailID constraint CAMP_orderDetailID_pk primary key number(10),
checkoutID number(10),
itemID,
quantity number(5),
constraint CAMP_checkoutID_fk foreign key (checkoutID) references checkout(checkoutID),
constraint CAMP_itemID_fk foreign key (itemID) references item(itemID)
);
The syntax is to define a column with inline constraints is:
COLUMN_NAME DATATYPE CONTRAINTS
but you have the data type after the constraints.
Date has no precision (and is not a function) so should not have braces after it.
itemID needs a data type.
Like this:
create table checkout (
checkoutID number(10) constraint CAMP_checkoutID_pk primary key,
peopleID number(10),
checkoutDate date,
returnDate date,
saleTotal number(10),
returned varchar(1),
constraint CAMP_peopleID_fk foreign key (peopleID) references people(peopleID)
);
create table checkoutDetail (
orderDetailID number(10) constraint CAMP_orderDetailID_pk primary key,
checkoutID number(10),
itemID number(5),
quantity number(5),
constraint CAMP_checkoutID_fk foreign key (checkoutID) references checkout(checkoutID),
constraint CAMP_itemID_fk foreign key (itemID) references item(itemID)
);
db<>fiddle here

"no matching unique or primary key for this column-list". The primary key does exist though

So i'm practicing some sql coding for a test and I can't get a foreign key to reference a primary key.
Here's the table that doesn't work:
CREATE TABLE ASSIGNMENT(
ASSIGN_ID NUMBER(2) NOT NULL,
START_DATE DATE,
END_DATE DATE,
BUDGET NUMBER (10,2),
MANAGER_ID NUMBER(2),
PRIMARY KEY (ASSIGN_ID,MANAGER_ID),
FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEE(EMP_ID)
);
Here's the table it is referencing:
CREATE TABLE EMPLOYEE(
EMP_ID NUMBER(2) NOT NULL,
NAME VARCHAR(40),
OFFICE VARCHAR(20),
EXPERT_ID NUMBER(2),
PRIMARY KEY (EMP_ID,EXPERT_ID),
FOREIGN KEY (EXPERT_ID) REFERENCES EXPERTISE(EXPERT_ID)
);
Whenever I try to run the script it always comes back with:
Error report -
SQL 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
I've looked around but can't seem to find the problem. Any help would be appreciated.
Here's the full code (haven't tested the last table yet):
CREATE TABLE EXPERTISE(
EXPERT_ID NUMBER(2) NOT NULL,
DESCRIPTION VARCHAR(50),
HOURLY_RATE NUMBER(3,2),
CHARGE_RATE NUMBER(3,2),
PRIMARY KEY(EXPERT_ID)
);
CREATE TABLE EMPLOYEE(
EMP_ID NUMBER(2) NOT NULL,
NAME VARCHAR(40),
OFFICE VARCHAR(20),
EXPERT_ID NUMBER(2),
PRIMARY KEY (EMP_ID,EXPERT_ID),
FOREIGN KEY (EXPERT_ID) REFERENCES EXPERTISE(EXPERT_ID)
);
CREATE TABLE ASSIGNMENT(
ASSIGN_ID NUMBER(2) NOT NULL,
START_DATE DATE,
END_DATE DATE,
BUDGET NUMBER (10,2),
MANAGER_ID NUMBER(2),
PRIMARY KEY (ASSIGN_ID,MANAGER_ID),
FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEE(EMP_ID)
);
CREATE TABLE ALLOCATION(
EMP_ID NUMBER(3) NOT NULL,
ASSIGN_ID NUMBER(3) NOT NULL,
DAYS_WORKED_ON DATE,
HOURS_WORKED_ON DATE,
PRIMARY KEY(EMP_ID,ASSIGN_ID),
FOREIGN KEY(EMP_ID) REFERENCES EMPLOYEE(EMP_ID),
FOREIGN KEY(ASSIGN_ID) REFERENCES ASSIGNMENT(ASSIGN_ID)
);
I'm using Oracle SQL Developer to make it
*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.
The problem is that EMP_ID (by itself) isn't a primary or unique key of table Employees, instead, you have a compound primary key (EMP_ID, EXPERT_ID).
To fix the issue either make EMP_ID the primary key of the Employees table (which seems intuitive as each employee ought to have a unique id) or add a separate unique constraint on EMP_ID.
As pointed out in the comments, if you make EMP_ID the primary key, then (EMP_ID, EXPERT_ID) will also be unique by extension.
As the error suggest, the column you've referenced a foreign key doesn't match a unique constraint/pk on the parent table. Specifically for the primary key EMP_ID, EXPERT_ID you reference only EMP_ID.

Oracle SQL Database error: "no matching unique or primary key for this column-list"

I'm trying to set up a database in Oracle sql developer, I've got these 3 tables.
I need the table "GuyAddress" to have 3 primary keys, which are all foreign keys as well. This is where I'm running into an error which I can't get my head around.
CREATE TABLE Guy
(
id NUMBER(10) PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE Address
(
zipcode VARCHAR(6),
"number" NUMBER(10),
CONSTRAINT PK_Address PRIMARY KEY(zipcode, "number")
);
CREATE TABLE GuyAddress
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),
CONSTRAINT FK_GuyAddress_Address_number FOREIGN KEY(Address_number) REFERENCES Address("number"),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
);
This is the error, hopefully someone can spot the mistake I made because I can't...
Error starting at line : 18 in command -
CREATE TABLE "GuyAddress"
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),
CONSTRAINT FK_GuyAddress_Address_number FOREIGN KEY(Address_number) REFERENCES Address("number"),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
)
Error report -
SQL 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
Thanks!
You don't need separate foreign keys for each column in the referenced table's primary key - you can have multiple columns in a foreign key, e.g.:
CREATE TABLE Guy
(
id NUMBER(10) PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE Address
(
zipcode VARCHAR(6),
address_number NUMBER(10),
CONSTRAINT PK_Address PRIMARY KEY(zipcode, address_number)
);
CREATE TABLE GuyAddress
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address FOREIGN KEY(Address_zipcode, Address_number) REFERENCES Address(zipcode,address_number),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
);
Note that I've updated your address.number column to be address.address_number, as it's not recommended that you use a column based on a keyword. Using doublequotes to get around this (and also enforce case sensitivity) is not recommended either; you'll have to remember to use them every time you reference that column!
As an aside, I assume that your address table has other columns? Because as things stand, the address table is pointless and could be skipped!

Oracle: Make a composite Key containing three Foregin keys

This is my code:
create table orderline
(
Order_No number(4) constraint orderno_fk references order_detail(Order_No),
Product_Code varchar2(6) constraint productcode2_fk references product(Product_Code),
Product_Size char(1) constraint productsize_fk references product_stock(Product_Size),
Product_Quantity number(4) not null
constraint orderline_comp primary key (Order_No,Product_Code, Product_Size)
);
I get the error (with the star underneath the left parenthesis before 'Order'):
ERROR at line 7:
ORA-00907: missing right parenthesis
You need a , before the constraint orderline...