I understand the ORA-00907 is indicating that I have a syntax error in my code, I just can't find it. Can someone help point out the problem? I'm using SQL Developer (Oracle12c).
CREATE TABLE equip
(equipid NUMBER(3),
edesc VARCHAR2(30),
purchdate DATE,
rating CHAR(1),
deptid NUMBER(2) NOT NULL,
etypeid NUMBER(2),
CONSTRAINT equip_equipid_pk PRIMARY KEY (equipid),
CONSTRAINT equip deptid_fk FOREIGN KEY (deptid) REFERENCES dept (deptid),
CONSTRAINT equip_etypeid_fk FOREIGN KEY (etypeid) REFERENCES etypes (etypeid),
CONSTRAINT equip_rating_ck CHECK (rating IN ('A','B','C')));
You have a space in the constraint name, which is probably confusing the syntax parser:
CONSTRAINT equip deptid_fk FOREIGN KEY (deptid) REFERENCES dept (deptid),
^
If you need a space in identifiers, delimit them in double-quotes like "equip deptid_fk". But it's easier if you can spell your identifiers without whitespace or punctuation.
Related
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
The following statement fails with an error "missing right parenthesis":
CREATE TABLE STUDENT
( Student# NUMBER(9),
FirstName VARCHAR2(52),
LastName VARCHAR2(50),
DeptID NUMBER(9) NOT NULL,
ProjectID NUMBER(5,2) NOT NULL,
PCID NUMBER(10) NOT NULL,
PR# NUMBER(10) NOT NULL,
Email VARCHAR(50)
CONSTRAINT student_student#_pk PRIMARY KEY (student#),
CONSTRAINT student_deptid_fk FOREIGN KEY (deptid)
REFERENCES department (deptid),
CONSTRAINT student_pcid_fk FOREIGN KEY (pcid)
REFERENCES projectcourse (pcid) ,
CONSTRAINT student_project#_fk FOREIGN KEY (project#)
REFERENCES project (project#),
CONSTRAINT student_pr#_fk FOREIGN KEY (pr#)
REFRENCES projectregisteration (pr#));
EDITED
After a few corrections I'm still getting the ORA-00942: table or view does not exist error. Below is what my code currently looks like. Any further suggestions will be appreciated.
CREATE TABLE STUDENT
( Student# NUMBER(9),
FirstName VARCHAR2(52),
LastName VARCHAR2(50),
DeptID NUMBER(9) NOT NULL,
Project# NUMBER(5,2) NOT NULL,
PCID NUMBER(10) NOT NULL,
PR# NUMBER(10) NOT NULL,
Email VARCHAR2(150),
CONSTRAINT student_student#_pk PRIMARY KEY (student#),
CONSTRAINT student_deptid_fk FOREIGN KEY (deptid)
REFERENCES department (deptid),
CONSTRAINT student_pcid_fk FOREIGN KEY (pcid)
REFERENCES projectcourse (pcid) ,
CONSTRAINT student_project#_fk FOREIGN KEY (project#)
REFERENCES project (project#),
CONSTRAINT student_pr#_fk FOREIGN KEY (pr#)
REFERENCES projectregisteration (pr#));
A , is missing after Email VARCHAR(50). There's a typo, REFRENCES instead of REFERENCES. And in CONSTRAINT student_project#_fk FOREIGN KEY (project#), the column project# isn't in the list of columns above.
The Oracle compiler throws missing right parenthesis when we have made a syntax error in our code.
Obviously the first thing to check is that we have a right parenthesis for every left parenthesis; this is easy if we're using an editing tool which supports bracket matching (say by highlighting matching pairs).
But often we have matched all the brackets, so why do we get this error? It happens when we have missed something, and the compiler interprets that as a missing bracket.
For instance a valid CREATE TABLE statement consists of a number of clauses defining columns and constraints, enclosed by a pair of brackets (optionally followed by a storage clause). The important things is that the column and constraint clauses are all separated by commas. In your statement you have missed the comma after the Email VARCHAR(50). The compiler interprets this as the end of the statement and expects a right parenthesis. But your statement kicks off a constraint clause instead. Hence the error message.
It would be neat if the compiler was clever enough to identify a missing comma, but that would require the compiler to additional work and the compiler writers opted to outsource that work to us instead :)
Looking at your last foreign key constraint, you have the table name entered as "projectregisteration". Are you certain that's correct? Please check that the table name is as you've typed it, and not "projectregistration" (without the "e" after the first "t" in "registration").
If I create all the tables as shown in your second CREATE TABLE statement, then the statement executes without a problem. However, if I make the last table PROJECTREGISTRATION (correct spelling of "registration") instead of PROJECTREGISTERATION (as shown in your CREATE TABLE) then the CREATE TABLE fails with ORA-00942: table or view does not exist, just as you stated.
So I suspect it's a simple typo.
dbfiddle here
Best of luck.
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.
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.
Hi I'm having a problem with one of the tables in my database. I'm loading the tables from a .txt file, when I load the database i get
ERROR NEAR line 49: near "(": syntax error
the table below starts from line 49
create table LEASE(
P_ID integer,
I_ID varchar2,
C_ID integer,
DATE date,
TRENT decimal(6,2),
RENTPM decimal(4,2),
RENTUTD varchar2 constraint rentutd_value (RENTUTD in ('Y','N')),
LENGTH varchar2(15),
SDATE date,
EDATE date,
NOTE varchar2(150),
G_ID integer,
A_ID integer,
constraint fkey_lea1 foreign key (P_ID) references PROPERTY(P_ID),
constraint fkey_lea2 foreign key (I_ID) references INSTITUTION(I_ID),
constraint fkey_lea3 foreign key (C_ID) references CLIENT(C_ID),
constraint fkey_lea4 foreign key (G_ID) references GUARANTOR(G_ID),
constraint fkey_lea5 foreign key (A_ID) references AGENT(A_ID),
constraint pkey_lea primary key (P_ID,I_ID,C_ID,DATE)
);
Looks like the syntax on your rentutd column needs to be a little different:
RENTUTD varchar2 constraint rentutd_value CHECK ( RENTUTD in ('Y','N'))
See this sqlfiddle.
The SQLite syntax diagrams are great to figure this stuff out.