ORA missing right parenthesis and unimplemented feature - sql

I'm trying to execute the following script on Oracle APEX:
CREATE TABLE employer (
ename VARCHAR2(30) NOT NULL,
essn CHAR(9),
bdate DATE,
dno INTEGER DEFAULT 1
CHECK (dno > 0 AND dno < 21),
superssn CHAR(9),
CONSTRAINT employer_pk
PRIMARY KEY (essn),
CONSTRAINT employer_fk
FOREIGN KEY (superssn) REFERENCES employer (essn)
ON DELETE SET NULL
ON UPDATE CASCADE
);
CREATE TABLE department (
dname VARCHAR2(10) NOT NULL,
dnumber INTEGER NOT NULL,
mgrssn CHAR(9) NOT NULL,
mgrstartdate CHAR(9) NOT NULL,
PRIMARY KEY (dnumber),
UNIQUE (dname),
FOREIGN KEY (mgrssn) REFERENCES employer (essn)
ON DELETE SET DEFAULT
ON UPDATE CASCADE
);
However, it gives 2 errors. ORA-00907: missing right parenthesis on the first table, and ORA-03001: unimplemented feature on the second.
When I tried to run the script without the ON DELETE/ON UPDATE statements, it didn't show any errors and the tables were created successfully. Do I need a parenthesis somewhere when I'm adding ON DELETE/ON UPDATE?

Oracle doesn't support the following features:
ON UPDATE CASCADE
ON DELETE SET DEFAULT
Presumably, your script will run fine if you remove them. Personally, I've never needed either of them (since I always use surrogate primary keys that are never changed).

Related

I'm getting an error on Oracle Apex ORA-00907: missing right parenthesis

I'm trying to run this snipped of SQL in Oracle Apex and keep receiving errors about right parenthesis. I have tried to remove the constraints or alter the tables later, but I keep coming up with the same sort of errors. I cannot seem to figure out what it is that's wrong with the table structure, and I can't find anything online about it that makes sense. Any help would be much appreciated, thanks. Code below...
DROP TABLE Employee_T
CASCADE CONSTRAINTS;
DROP TABLE TaxDepartment_T
CASCADE CONSTRAINTS;
DROP TABLE Location_T
CASCADE CONSTRAINTS;
CREATE TABLE Employee_T
(
EmployeeID NUMBER(11) NOT NULL,
EmployeeName VARCHAR2(25) NOT NULL,
EmployeeAddress VARCHAR2(30) ,
EmployeeCity VARCHAR2(20) ,
EmployeeState CHAR(2) ,
EmployeePostalCode VARCHAR2(10) ,
CONSTRAINT Employee_PK PRIMARY KEY(EmployeeID),
CONSTRAINT Employee_FK1 FOREIGN KEY(DepartmentID) REFERENCES (TaxDepartment_T),
CONSTRAINT Employee_FK2 FOREIGN KEY(BranchID) REFERENCES (Location_T)
);
CREATE TABLE TaxDepartment_T
(
DepartmentID INTEGER(11) NOT NULL,
BranchID INTEGER(11) NOT NULL,
CPAID INTEGER(11) NOT NULL,
EmployeeID INTEGER(11) NOT NULL,
BranchName VARCHAR2(50) NOT NULL,
CONSTRAINT TaxDepartment_PK PRIMARY KEY(DepartmentID, BranchID, CPAID),
CONSTRAINT TaxDepartment_FK1 FOREIGN KEY(BranchID) REFERENCES (Location_T),
CONSTRAINT TaxDepartment_FK2 FOREIGN KEY(EmployeeID) REFERENCES (Employee_T)
);
CREATE TABLE Location_T
(
BranchID INTEGER(11) NOT NULL,
BranchName VARCHAR2(50) NOT NULL,
ManagerName VARCHAR2(50) NOT NULL,
EmployeeID INTEGER(11) NOT NULL,
CONSTRAINT Location_PK PRIMARY KEY(BranchID),
CONSTRAINT Location_FK1 FOREIGN KEY(EmployeeID) REFERENCES (Employee_T)
);
Your foreign key constraint syntax is off.
What it should look like:
REFERENCES SCHEMA.TABLE (COLUMN)
and you just have:
REFERENCES (COLUMN)
If you look at this code in SQL Developer, the parser catches your issue right away, and even gives you a simple click to get to the Docs with syntax diagram for defining FK constraints.
This is your FIRST problem.
The fun with bugs is killing one only exposes the next one. You can't create FK constraints for tables you haven't created yet. So either you need to create the base tables first, OR you need to remove the FK constraints from your CREATE TABLE calls, and add them back later as
alter table TABLE_NAME add constraint CONSTRAINT_NAME foreign key(COLUMN_NAME) references TABLE_NAME2(COLUMN_NAME)
Place all of these ALTER TABLE ADD CONSTRAINT calls at the end of your script, once all the tables have already been created.
Someone else has also noticed that you're using INTEGER.
Which I do, ALL THE TIME...because I'm too lazy to type 'NUMBER(38,0)'
That's fine. But what you can't do is say INTEGER(9). That makes no sense in Oracle.
You should use NUMBER instead of INTEGER in TaxDepartment_T and Location_T tables
There are a few syntax issues.
integer is OK but not integer(11). Use number(11). (Also, while char is a valid type, you should stick to the standard varchar2 for strings to avoid unexpected behaviour.)
Foreign key constraints are written constraint fk references tablename, or optionally you can specify the referenced column in brackets: constraint fk references tablename (columnname). (Also if you write them inline as part of the column definition, you can let the datatype inherit from the parent.)
Employee FK1 and FK2 refer to DepartmentID and BranchID columns that the table doesn't have.
You need to put the parent before the child if you want to run it as a script.
I would write it like this:
drop table employee_t cascade constraints;
drop table taxdepartment_t cascade constraints;
drop table location_t cascade constraints;
create table Location_T
( BranchID number(11) not null constraint Location_PK primary key
, BranchName varchar2(50) not null
, ManagerName varchar2(50) not null );
create table TaxDepartment_T
( DepartmentID number(11) not null
, BranchID constraint TaxDepartment_Location_FK references location_t not null
, CPAID number(11) not null
, BranchName varchar2(50) not null
, constraint TaxDepartment_PK primary key(DepartmentID, BranchID, CPAID) );
create table Employee_T
( EmployeeID number(11) not null constraint Employee_PK primary key
, EmployeeName varchar2(25) not null
, EmployeeAddress varchar2(30)
, EmployeeCity varchar2(20)
, EmployeeState varchar2(2)
, EmployeePostalCode varchar2(10)
, DepartmentID constraint Employee_Department_FK references location_t
, BranchID constraint Employee_Branch_FK references Location_T );
I don't think Location or Tax Department should have EmployeeId columns so I removed them - say if you think that's wrong.
Personally I wouldn't put _T on the end of my table names, and I would avoid camelCase naming because the data dictionary doesn't retain it, and so describing a table gives for example:
SQL> #desc location_t
Name Null? Type
----------------------------------------- -------- ----------------------------
BRANCHID NOT NULL NUMBER(11)
BRANCHNAME NOT NULL VARCHAR2(50)
MANAGERNAME NOT NULL VARCHAR2(50)

Missing Parenthesis in Apex

I'm trying to write a script in Apex to create multiple tables. The first table is created with no issues but every table after that one gives me a missing parenthesis issue. Sometimes it's the left, sometimes it's the right. I've tried everything with no avail.
I have debugged it numerous times, spoken with the others and have not found the solution.
Create Table Employee -- Creates Employee table and references it to ProjDept table
(
EmployeeID Number(4) Not Null,
FirstName VarChar2(15) Not Null,
LastName VarChar2(20) Not Null,
ProjDeptID Number(4) Not Null,
PhoneNumber Number(10),
Constraint Employee_pk Primary Key (EmployeeID), -- sets primary key for table
Constraint Employee_FK Foreign Key References ProjDept(ProjDeptID)-- identifies foreign key
);
This is the second table in the script that won't work, the next 2 tables generate similar errors.
You forgot to include the name of the column which is referencing another in the foreign key:
Create Table Employee (
EmployeeID Number(4) Not Null,
FirstName VarChar2(15) Not Null,
LastName VarChar2(20) Not Null,
ProjDeptID Number(4) Not Null,
PhoneNumber Number(10),
Constraint Employee_pk Primary Key (EmployeeID), -- sets primary key for table
Constraint Employee_FK Foreign Key (ProjDeptId) References ProjDept(ProjDeptID)-- identifies foreign key
);
db<>fiddle
Where exactly in Apex are you executing those commands?
If SQL Workshop's SQL Commands, then you can't have more than a single command in there, i.e. you should create tables one by one:
create the first table
delete that create table command and write another one, for the second table; then create it
the same goes for other tables as well
Alternatively, go to SQL Workshop's SQL Scripts and put all your commands into a script, e.g.
create table a (id number, name varchar2(20), ...);
create table b (cdate date, ...);
save & run the script.

Troubles creating tables

I'm trying to make two tables execute but I keep getting errors on them. Transaction table says it can't execute without the other being created and then I try to create the broker table and it says missing right parenthesis
SQL Error: ORA-00907: missing right parenthesis
but I checked my code and it looks well. Would any of you know what is the problem? thanks to all who comment!
Create table Broker
(
Broker_Number Number(7,0)
Constraint PK_Broker_Broker_Number Primary Key
Constraint NN_Broker_Broker_Number Not Null,
First_Name Varchar2(25)
Constraint NN_Broker_First_Name Not Null,
Last_Name Varchar2(3O)
Constraint NN_Broker_Last_Name Not Null,
Area_Code Number(3,0)
Default '780'
Constraint NL_Broker_Area_Code Null,
Phone_Number Number(7,0)
Constraint NL_Broker_Phone_Number Null,
Email_Address Varchar2(50)
Constraint CK_Broker_Email_Address Check (REGEXP_like(Email_Address,'%#%.%'))
Constraint NN_Broker_Email_Address Not Null
);
Create table Transaction
(
Portfolio_Number Number(7,0) Not Null,
Constraint FK_Trans_Portfolio_Number
Foreign Key (Portfolio_Number)
References Portfolio(Portfolio_Number),
Stock_Code Varchar2(10) Not Null,
Constraint FK_Transaction_Stock_Code
Foreign Key (Stock_Code)
References Stock(Stock_Code),
Transaction_Date Date
Constraint PK_Trans_Transaction_Date Not Null,
Exchange_Code Varchar2(4) Not Null,
Constraint FK_Transaction_Exchange_Code
Foreign Key (Exchange_Code)
References Exchange(Exchange_Code),
Broker_Number Number(7,0) Not Null,
Constraint FK_Transaction_Broker_Number
Foreign Key (Broker_Number)
References Broker(Broker_Number),
Buy_Sell Char(1)
Constraint CK_Transaction_Buy_Sell Check (Buy_Sell in ('B','S') )
Constraint NN_Transaction_Buy_Sell Not Null,
Quantity Number(7,0)
Constraint NN_Transaction_Quantity Not Null,
Price_Per_Share Number(6,2)
Constraint NN_Transaction_Price_Per_Share Not Null
)
It's this line
Last_Name Varchar2(3O)
That's not 30, it is 3O, as in the letter O after the number three.
One way to debug such error statements is to remove one column at a time, run the statement, and repeat the process until you don't encounter the error any more. In this case, I deleted this specific line and it worked fine. That's when I noticed the O instead of 0 in the editor.

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.

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!