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

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.

Related

Assistance with "Missing right parenthesis" error

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.

Missing keyword in Oracle SQL

What's wrong with this query?
I am getting following error:
ORA-00905: missing keyword
00905. 00000 - "missing keyword"
My SQL:
CREATE Table ORDERDET
(
ORDERID NUMBER,
CUSTID NUMBER,
PRODID NUMBER,
ORDPRIORITY VARCHAR2(15),
ORDDISCOUNT NUMBER(3,2),
ORDSHIPMODE VARCHAR2(15),
ORDDATE DATE,
ORDSHIPDATE DATE,
ORDSHIPCOST NUMBER(5,2),
ORDQTY NUMBER,
ORDSALES NUMBER(7,2),
CONSTRAINT ch_ORDPRIORITY
CHECK (ORDPRIORITY IN ('Low', 'Medium', 'High', 'Critical', 'Not Specified')),
CONSTRAINT ch_ORDSHIPMODE
CHECK (ORDSHIPMODE IN ('Regular Air','Delivery Truck','Express Air')),
CONSTRAINT pk_ORDERDET
PRIMARY KEY (ORDERID, CUSTID, PRODID),
CONSTRAINT fk_ORDERD
FOREIGN KEY (ORDERID) REFERENCES ORDERS (ORDERID) on DELETE RESTRICT,
CONSTRAINT fk_CUSTOMERORDER
FOREIGN KEY (CUSTID) REFERENCES CUSTOMERS (CUSTID) on DELETE RESTRICT,
CONSTRAINT fk_PRODUCTORDER
FOREIGN KEY (PRODID) REFERENCES PRODUCTS (PRODID) on DELETE RESTRICT
);
According to the documentation: CREATE TABLE
there is no on DELETE RESTRICT option, only CASCADE or SET NULL are allowed, please see attached syntax diagram below:
I guess you want to prevent from deletion of a row in the parent table in a case when there are rows in the child table that references this parent row - if yes, then skip ON DELETE clause completely, because this is default behaviour of the foreign key constraint.
----------
EDIT
----------
Error report - ORA-02264: name already used by an existing constraint
02264. 00000 - "name already used by an existing constraint" *Cause: The specified constraint name has to be unique. *Action: Specify a
unique constraint name for the constraint.
The error message says, that one of a constraint name you have used in the create table has already been creaded (used) and you cannot use it again.
I think you haven't showed us the whole error message, because Oracle should print this duplicate name.
Anyway, you can find which name is duplicate using this query:
select *
from user_objects
where object_name in (
'CH_ORDPRIORITY' ,
'CH_ORDSHIPMODE' ,
'PK_ORDERDET' ,
'FK_ORDERD' ,
'FK_CUSTOMERORDER' ,
'FK_PRODUCTORDER'
)
If one (or a few) of these names are already used, then use a different name, say FK_PRODUCTORDER_11 instead of FK_PRODUCTORDER

ORA missing right parenthesis and unimplemented feature

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

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!

Violated - parent key not found 02291. 00000 - "integrity constraint

Hi I am developing a database in Oracle SQL dev, that is trying to access foriegn keys from another table. I am currently working on the ItemOrdered table which I've created with the following CREATE statement
CREATE TABLE ItemOrdered(OrderID varchar2(9) REFERENCES Ords(OrderID),
BeltID varchar2(9) REFERENCES BeltID(BeltID),
Quantity varchar(4) NOT NULL,
PRIMARY KEY(OrderID, BeltID))
As you can See I have the following foriegn keys Ords and BeltID.
Now when I try to run the following statement
INSERT INTO ItemOrdered VALUES(401565981,234489212,'2')
It gives me the following error
violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
I have provided my Ords CREATE statement if its needed
CREATE TABLE Ords(OrderID varchar2(9) PRIMARY KEY,
CustomerID varchar(9) REFERENCES Customers(CustomerID),
Expected_Delivery_Date date DEFAULT sysdate NOT NULL,
Actual_Delivery_Date date DEFAULT sysdate NOT NULL,
Payment_Due_Date date DEFAULT sysdate NOT NULL,
Order_Date date DEFAULT sysdate NOT NULL, Price Varchar(10),
Order_Placed varchar2(1) CONSTRAINT OrderPlaced
CHECK(Order_Placed IN('Y','N')) NOT NULL,
Order_Confirmed varchar2(1)
CONSTRAINT Order_Confirmed CHECK(Order_Confirmed IN('Y','N')) NOT NULL,
Order_Completed varchar2(1) CONSTRAINT Order_Completed
CHECK(Order_Completed IN('Y','N')) NOT NULL)
And I have also provided my BeltID CREATE statement
CREATE TABLE BeltID(BeltID varchar2(9) PRIMARY KEY,
BeltLengthID varchar2(9) REFERENCES BeltLength(BeltLengthID),
ColourID varchar2(9) REFERENCES Colour(ColourID),
DesignID varchar2(9) REFERENCES Design(DesignID),ComponentID varchar2(9) REFERENCES Component(ComponentID))
I don't seem to quite understand why I am getting this error. Is there an clear explanation why?
Here is the http link of what I am trying to do.
link text
Due to the foreign key constraints you specified when you created table ItemOrdered, when you perform this insert:
INSERT INTO ItemOrdered VALUES(401565981,234489212,'2')
... the values 401565981 and 234489212 must correspond to key values in the Ords and BelitId tables respectively - i.e. these 2 queries should return rows:
select *
from Ords
where OrderId = 401565981;
select *
from BeltId
where BeltId = 234489212;
The error message suggests this is not the case.
I have no Oracle installation available so I can't test, but does it matter if you enclose the OrderID and BeltID in single quotes (which you should do anyway since the columns are declared as varchars)? I haven't tested it, but one idea would be that Oracle barfs on the fact that you are inserting a numeric value into a varchar column.
insert
into ItemOrdered VALUES('401565981', '234489212','2')
Another option would be that the order of the columns in the table does not correspond to the order you gave them in the insert statement. To rule this out, try rewrite the statement as:
insert
into ItemOrdered(OrderID,BeltID,Quantity) values('401565981', '234489212','2')