Assistance with "Missing right parenthesis" error - sql

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.

Related

SQL Syntax Error: "Missing Right Parenthesis"

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.

Are these FKs necessary -- and are they stopping me?

I'm having some difficulties with a database I'm creating for a summer camp, specifically with the PK and FK constraints. When I declare the FK constraint (e.g. FOREIGN KEY(PID) references Campers(CamperID)) I get an error running my code through pgAdmin (I'm using PostgreSQL). I understand that, for example, the Campers table is not yet created, and this is most likely part/all of the roadblock, however I feel like my FKs are still wrong somehow. To my understanding, a FK is a PK in another table -- but I feel like there is some redundancy or disconnect between my tables.
I've put the syntax for some of my CREATE statements below. I'm not sure if I'll get reprimanded for the quality of my (somewhat vague) question, but I feel a bit lost and would appreciate any help or advice. Thank you in advance!
DROP TABLE IF EXISTS People;
CREATE TABLE People (
PID VARCHAR(10) NOT NULL UNIQUE,
FName TEXT NOT NULL,
LName TEXT NOT NULL,
DOB DATE NOT NULL,
ArrivalDate DATE NOT NULL DEFAULT CURRENT_DATE,
DepartureDate DATE,
US_PhoneNum VARCHAR(11) NOT NULL,
StreetAddress VARCHAR(200) NOT NULL,
Sex GENDER NOT NULL,
ZIP VARCHAR(10) NOT NULL,
PRIMARY KEY(PID),
FOREIGN KEY(PID) REFERENCES Campers(CamperID),
FOREIGN KEY(PID) REFERENCES Counselors(CounselorID),
FOREIGN KEY(ZIP) REFERENCES Zip(ZIP)
);
DROP TABLE IF EXISTS Zip;
CREATE TABLE Zip (
ZIP VARCHAR(10) NOT NULL,
City TEXT NOT NULL,
State VARCHAR(2) NOT NULL,
PRIMARY KEY(ZIP)
);
DROP TABLE IF EXISTS Campers;
CREATE TABLE Campers (
CamperID VARCHAR(10) NOT NULL REFERENCES People(PID),
AgeGroup AGES NOT NULL,
CabinID VARCHAR(2) NOT NULL,
Bed BEDTYPES NOT NULL,
GroupID VARCHAR(3) NOT NULL,
PRIMARY KEY(CamperID),
FOREIGN KEY(CamperID) REFERENCES People(PID),
FOREIGN KEY(CabinID) REFERENCES Cabins(CabinID),
FOREIGN KEY(Bed) REFERENCES Beds(Bed),
FOREIGN KEY(GroupID) REFERENCES Groups(GroupID)
);
DROP TABLE IF EXISTS Counselors;
CREATE TABLE Counselors (
CounselorID VARCHAR(10) NOT NULL REFERENCES People(PID),
GroupID VARCHAR(3) NOT NULL,
CabinID VARCHAR(2) NOT NULL,
PRIMARY KEY(CounselorID),
FOREIGN KEY(GroupID) REFERENCES Groups(GroupID),
FOREIGN KEY(CabinID) REFERENCES Cabins(CabinID)
);
ERROR message for further clarification:
ERROR: relation "campers" does not exist
********** Error **********
ERROR: relation "campers" does not exist
SQL state: 42P01
There are more tables (obviously) which I can provide the create statements for, if needed.
You should really start here: Foreign key.
In the context of relational databases, a foreign key is a field (or
collection of fields) in one table that uniquely identifies a row of
another table.
What you are trying to do in your script is to create a circular link between People, Campers and Counselors. Having a Primary Key field also a Foreign Key mandates that IDs across all referenced tables are identical.
... and to create a Foreign Key the referenced table must already exist in the database. So you should start with the table that does not have any Foreign Keys and create tables that reference only those tables created previously. Alternatively you can create all tables without Foreign Keys and add them later, when all the tables are present.
... and to answer the question, Foreign Keys are never necessary, but they might help.

What is the notation of recursive relation in oracle 11g?

I cannot find the solution on the web. I am wondering how I write the recursive relation in oracle. At the moment this is what I got:
create table medewerkers
(medewerker_ID varchar(15) primary key,
naam varchar(50) not null,
adres varchar(50) not null,
telefoon_nummer varchar(10) not null,
salaris number(4) not null,
functie varchar(50) not null,
manager varchar(15) constraint FK_Manager references medewerkers (medewerker_ID) on delete cascade,
werknemer_winkel_nummer number(15) constraint FK_W_winkel references winkel (winkel_nummer) on delete cascade,
constraint check_salaris check (salaris < 3000)
);
at the moment I created a manager column as FK for this recursive relation. I did not create an extra table because I am told that if they are 1-to-many with employee then I could place the FK within the table.
Now I am inserting a value like this one:
insert into MEDEWERKERS (medewerker_id, naam, adres, telefoon_nummer, salaris, functie, werknemer_winkel_nummer, manager)
values(11159112, 'Joost', 'Eindhoven Langloopstraat 1', 0678765478, 1500, 'baliemedewerker', 10, 'nee');
Oracle db gives an error back:
SQL Error: ORA-02291: integrity constraint (MAXIME.FK_MANAGER) violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
*Cause: A foreign key value has no matching primary key value.
*Action: Delete the foreign key or add a matching primary key.
How am I else supposed to get values into the manager column?
I hope my question is not too vague.
You need to make sure the manager is there before you add their underlings.
The CEO/Manager/Owner can be added with a NULL manager:
INSERT INTO MEDEWERKERS (medewerker_id, naam, adres, telefoon_nummer, salaris, functie, werknemer_winkel_nummer, manager)
VALUES ( 'nee', 'The Boss Man', 'Home Office', '0000000001', 9999, 'Owner', 10, NULL );
The employees can then be added with the correct foreign key references (as per the OP).
Also - if you are entering telephone numbers with a leading 0 then you probably want to wrap the data in quotes '' otherwise you may find that the conversion from number to varchar will lose it.
[As an aside: do you really want ON DELETE CASCADE on the foreign keys? If you delete a manager then all their employees will be deleted as well.]
Your question was perfectly formed.
Have you considered temporarily disabling the FK_Manager constraint so you can add the top-level of management? Then enable the constraint while you add the next level down of employees?
Just a thought.

create foreign key in oracle

is there anyone who can help me to create a foreign key for my Status table. I need to PLACE a foreign key constraint on the code in the status table, referring to the id in the Building table.
TABLE building
(
build_name VARCHAR2(50,0) NOT NULL,
id NUMBER (38,0) NOT NULL,
mapid NUMBER (10,0) NOT NULL
);
TABLE STATUS
(
code VARCHAR(2 BYTE) NOT NULL,
status_name VARCHAR2(40 BYTE) NOT NULL,
);
Bulding table has constraint building_gmidx with id as primary key.
Here is a quick syntax for your current requirement, however I recommend you to go through the oracle documentation for a proper understanding of what this means.
ALTER TABLE STATUS ADD (
CONSTRAINT status_fk_building FOREIGN KEY (code)
REFERENCES building (id)
ENABLE VALIDATE);
Did you leave out CREATE TABLE *name* to save time when writing the question? The first thing I see is that your Foreign key has to have the same data type and size as your Primary key. If the Tables already exist the code would be:
ALTER TABLE status MODIFY (code NUMBER(38));
From there you would need to
ALTER TABLE status ADD FOREIGN KEY (code) REFERENCES (building_gmidx)
Why is the constraint for building Primary key named building_gmidx and the column plain id?? All Normalized table attributes should be Unique. Wouldn't it be better if you just named the column "id" building_gmidx instead. Just in case there are other types of id's added later you do not have to think about which table "id" pertains to. Let me know if this works.

ORA-00905: missing keyword (constraint foreign key)

hi everyone I'm completely new to SQL and I'm trying to answer this question in my book:
7.5 Write a CREATE TABLE statement for the EMPLOYEE table. Email is required and is an alternate key, and the default value of Department is Human Resources. Cascade updates but not deletions from DEPARTMENT to EMPLOYEE.
I'm running the query in Oracle iSQL*Plus, I successfully created the department table, but when i tried creating the employee table while meeting these requirements I get the missing keyword error at line 12
constraint DepartmentFK FOREIGN KEY(DepartmentName),
this is the whole query (I dropped the department table before attempting the whole thing, and it still gives the same error)
CREATE TABLE DEPARTMENT (
DepartmentName char(35) NOT NULL,
BudgetCode char(30) NOT NULL,
OfficeNumber char(15) NOT NULL,
Phone char(12) NOT NULL,
Constraint DepartmentPK PRIMARY KEY(DepartmentName)
);
CREATE TABLE EMPLOYEE (
ProjectID int NOT NULL,
Name char(30) NOT NULL,
Department char(15) NOT NULL,
MaxHours int NOT NULL,
StartDate char(8) NULL,
EndDate char(8) NULL,
Email char(30) DEFAULT 'Human Resources' NOT NULL,
Constraint EmployeePK PRIMARY KEY(ProjectID),
Constraint EmployeeAK1 UNIQUE(Email),
constraint DepartmentFK FOREIGN KEY(DepartmentName),
references DEPARTMENT(DepartmentName)
ON UPDATE CASCADE
ON DELETE no ACTION
);
I tried following the most similar example in the book and looking up foreign key constraint and references but i can't understand why I'm getting this error...
EDIT:
I took out the comma but i still got these two errors:
CREATE TABLE DEPARTMENT (
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
ON UPDATE CASCADE
*
ERROR at line 14:
ORA-00905: missing keyword
You have to remove the comma you have used after
constraint DepartmentFK FOREIGN KEY(DepartmentName),
This is one unit
constraint DepartmentFK FOREIGN KEY(DepartmentName) references DEPARTMENT(DepartmentName)
EDIT:
Since, you have edited your question -
ON UDDATE CASCADE option is not available in Oracle Database, and that's why you're getting an error.
You're getting an error for Department table, since the table already exists, probably from the last run!