How to Insert data to tables having references each other in ORDBMS? - oracle

CREATE TYPE Dept_t1
/
CREATE TYPE Emp_t1 AS OBJECT(
eno number(4),
ename varchar2(15),
edept ref dept_t1,
salary number(8,2));
/
CREATE TYPE Dept_t1 AS OBJECT(
dno number(2),
dname varchar2(12),
mgr ref emp_t1);
/
CREATE TYPE Proj_t1 AS OBJECT(
pno number(4),
pname varchar2(15),
pdept ref dept_t1,
budget number(10,2));
/
CREATE TABLE Emp1 OF Emp_t1(
eno PRIMARY KEY
);
CREATE TABLE Dept1 OF Dept_t1(
dno PRIMARY KEY
);
CREATE TABLE Proj1 OF Proj_t1(
pno PRIMARY KEY
);
ALTER TABLE Emp1 MODIFY (edept REFERENCES Dept1);
ALTER TABLE Dept1 MODIFY (mgr REFERENCES Emp1);
ALTER TABLE Proj1 MODIFY (pdept REFERENCES dept1);
INSERT INTO Emp1 VALUES(1,'A',(SELECT REF(d) FROM Dept1 d where d.dno=11),30)
/
INSERT INTO Emp1 VALUES(2,'B',(SELECT REF(d) FROM Dept1 d where d.dno=12),50)
/
INSERT INTO Dept1 VALUES(11,'D1',(SELECT REF(e) FROM Emp1 e where e.eno=1))
/
INSERT INTO Dept1 VALUES(12,'D2',(SELECT REF(e) FROM Emp1 e where e.eno=2))
/
INSERT INTO Proj1 VALUES(111,'P1',(SELECT REF(e) FROM Dept1 e where e.dno=11),25)
/
INSERT INTO Proj1 VALUES(112,'P2',(SELECT REF(e) FROM Dept1 e where e.dno=12),35)
/
When I am trying to execute following query,
SELECT p.pname,p.pdept.dno FROM Proj1 p WHERE p.budget>45
p.pdept.dno column is get empty. How to resolve that problem? I was already added all the values. But not sure why null values get displayed. I am new to object relational database management system. So I need a help from all of you.
Dept1 table image is as this.

Related

SQL: ORA-02291: integrity constraint violated - parent key not found

I am creating this code SQL code and I keep getting the same error from every line of the insert statements after the errors begin here statement. the error is, ORA-02291: integrity constraint violated - parent key not found. I think it has something to do with the employee section constraint EMPSUPERVRFK but, that is the only constraint that the error message would lead to.
-- keep these two commands at the top of every sql file
set echo on
set linesize 120
delete from Employee;
commit;
-- insert only managers first with their dno is null
INSERT INTO Employee VALUES
('James','E','Borg',888665555,'10-NOV-1937','450 Stone, Houston, TX','M',55000,null,null);
INSERT INTO Employee VALUES
('Franklin','T','Wong',333445555, to_date('1955-12-08', 'YYYY-MM-DD'), '638 Voss, Houston, TX','M',40000,888665555,null);
INSERT INTO Employee VALUES
('Jennifer','J','Zelaya',987654321,'20-JUN-1941','291 Berry, Bellaire, TX','F',43000,888665555,null);
delete from Department;
commit;
insert into Department values ('Research',5,333445555,'22-MAY-1988');
insert into Department values ('Headquarters',1,888665555,'19-JUN-1981');
insert into Department values ('Administration',1,888665555,'01-JAN-1995');
-- now, update employee.dno for managers
UPDATE Employee SET dno = 1 WHERE ssn = 888665555;
UPDATE Employee SET dno = 5 WHERE ssn = 333445555;
-- need to update the rest of managers
-- insert the rest of non-manager employees, supervisors first
--errors begin here
delete from Employee;
commit;
INSERT INTO Employee VALUES ('John','B','Smith',123456789,'09-JAN-1965','731 Fondren, Houston, TX','M',30000,333445555,5);
insert into Employee values ('Alica','J','Zelya',999887777,'19-JAN-1968','3321 Castle, Spring, TX','F',25000,987654321,4);
insert into Employee values ('Ramesh','K','Narayan',666884444,'15-SEP-1962', '975 Fire Oak, Humnle, TX','M',38000,333445555,5);
insert into Employee values ('Joyce','A','English',453453453,'31-JUL-1972','5631 Rice, Houston, TX','F',25000,333445555,5);
insert into Employee values ('Ahmad','V','Jabbar',987987987,'29-MAR-1969','980 Dallas, Houston, TC','M',25000,987654321,4);
--Project
delete from PROJECT;
commit;
insert into PROJECT values ('ProductY',2,'Sugarland',5);
insert into PROJECT values ('ProductZ',3,'Houston',5);
insert into PROJECT values ('Computerication',10,'Stafford',4);
insert into PROJECT values ('Reorganization',20,'Houston',1);
insert into PROJECT values ('Newbenefits',30,'Stafford',4);
--dept_Locations
delete from DEPT_LOCATIONS;
commit;
insert into DEPT_LOCATIONS values (1, 'Houston');
insert into DEPT_LOCATIONS values (4,'Stafford');
insert into DEPT_LOCATIONS values (5,'Bellaire');
insert into DEPT_LOCATIONS values (5,'Sugarland');
insert into DEPT_LOCATIONS values (5,'Houston');
--works_on 16
delete from WORKS_ON;
commit;
insert into WORKS_ON values (123456789,1,32.5);
insert into WORKS_ON values (123456789,2,7.5);
insert into WORKS_ON values (666884444,3,40.0);
insert into WORKS_ON values (453453453,1,20.0);
insert into WORKS_ON values (453453453,2,20.0);
insert into WORKS_ON values (333445555,2,10.0);
insert into WORKS_ON values (333445555,3,10.0);
insert into WORKS_ON values (333445555,10,10.0);
insert into WORKS_ON values (333445555,20,10.0);
insert into WORKS_ON values (999887777,30,30.0);
insert into WORKS_ON values (999887777,10,10.0);
insert into WORKS_ON values (987987987,10,35.0);
insert into WORKS_ON values (987987987,30,5.0);
insert into WORKS_ON values (987654321,30,20.0);
insert into WORKS_ON values (987654321,20,15.0);
insert into WORKS_ON values (888665555,20,null);
--dependent 7
delete from DEPENDENT;
commit;
insert into DEPENDENT values (333445555,'Alice','F','05-APR-1986','Daughter');
insert into DEPENDENT values (333445555,'Theodore','M','25-OCT-1983','Son');
insert into DEPENDENT values (333445555,'Joy','F', '03-MAY-1958','Spouse');
insert into DEPENDENT values (987654321,'Abner','M', '28-FEB-1942','Spouse');
insert into DEPENDENT values (123456789,'Michael','M','04-JAN-1988','Son');
insert into DEPENDENT values (123456789,'Alice','F','30-DEC-1988','Daughter');
insert into DEPENDENT values (123456789,'Elizabeth','F', '05-MAY-1967','Spouse');
-----------------------------------------------------------------------------------
set echo on
set linesize 120
drop table Employee cascade constraints;
commit;
create table Employee
(
fname varchar2(15),
minit varchar2(1), -- can be char
lname varchar2(15),
ssn number,
bdate date,
address varchar2(50),
sex varchar2(1) CHECK(Sex = 'M' or Sex = 'F'),
salary number CHECK(20000 <= salary AND 100000 >= salary),
superssn number,
dno number DEFAULT 0,
constraint EMPPK
primary key(ssn),
constraint EMPSUPERVRFK
foreign key(superssn) references Employee(ssn)
ON DELETE SET NULL
);
drop table Department cascade constraints;
commit;
create table Department
(
dname varchar2(15),-- NOT NULL,
dnumber number,
mgrssn number,
mgrstartdate date,
constraint DEPTPK
primary key(dnumber),
constraint DEPTMGRFK
foreign key(mgrssn) references Employee(ssn)
ON DELETE SET NULL
);
alter table Employee add
constraint EMPDEPTFK foreign key(dno) references Department(dnumber)
ON DELETE SET NULL;
drop table DEPT_LOCATIONS;
create table DEPT_LOCATIONS
(
Dnumber number,
Dlocation varchar2(15),
constraint PK_DnoDloc primary key(Dnumber,Dlocation)
);
COMMIT;
drop table PROJECT;
create table PROJECT
(
Pname varchar2(15),
Ponumber number primary key,
Plocation varchar2 (15),
Dnum number,
foreign key (Dnum) references Department(dnumber)
);
Commit;
drop table DEPENDENT;
create table DEPENDENT
(
Essn number,
Dependent_name varchar2(15),
Sex Char,
Bdate Date,
Relationship varchar2(15),
foreign key(Essn) references Employee(ssn) --
ON DELETE SET NULL,
constraint PK_essn
primary key(Essn,Dependent_name)
);
COMMIT;
drop table WORKS_ON;
create table WORKS_ON
(
Essn number,
Pno number,
Hours number,
foreign key(Essn) references Employee(ssn)--
ON DELETE SET NULL,
foreign key(Pno) references PROJECT(Ponumber)
ON DELETE SET NULL,
constraint PK_SSN
primary key(Essn, Pno)
);
commit;
----------------------------------------------------------------
-- keep these two commands at the top of every sql file
set echo on
set linesize 120
-- test queries, not to be submitted
select count(*) from employee;
select count(*) as DEPT_COUNT from department;
-- comment out the above queries for your homework
-- a the first name, last name of employees who work in department 5.
select fname, lname from employee where dno = 5;
-- b the first name, last name of every employee and name of his/her department
select E.fname as FIRST_NAME, E.lname LAST_NAME, D.dname DEPARTMENT_NAME
from employee E, department D
where E.dno = D.dnumber;
--c The first name, last name of employees who works at the 'Research' department
select e.fname , e.lname , Dname
from employee e inner join department d on e.dno=d.dname
where d.name ='Research';
--d. The first name, last name of employee who is the manager of the 'Research' department
select e.fname , e.lname , Dname
from employee e inner join department d on e.dno=d.dname
where d.name ='Research' and e.super_ssn=d.mgr_ssn;
--e. The first name, last name of employees who works on the 'Computerization' project.
select e.fname,e.lname
from employee e inner join department d on e.dno=d.dnumber inner join project p on d.dnumber=p.dnum
where p.pname="Computerization";
this happens because you are inserting values to table that depends on other table that is still empty, you should insert on that table first before this one
for example, suppose you have employee table, and department table, in which every employee must be assigned to a department, so when you input employee Bob to department IT, it would result in error, since there is no IT yet in department table. so you need to input IT on department table, then you can input Bob to employee table
or, you can disable integrity check when importing sql, but this setting is different for each DB engine, so I can't give you an example

I cannot display the values of the 2 table Ceremony and Referee

create table STADIUM
( stad_location varchar2(20) primary key
, stad_name varchar2(10)
, stad_capacity number(5)
, match_ID char(8)
, stall_ID char(4)
, foreign key (match_ID) references MATCH (match_ID) ON DELETE SET NULL
, foreign key (stall_ID) references STALL (stall_ID) ON DELETE SET NULL);
create table TRANSPORTATION
(registra_no char(6) primary key
, transp_type varchar2(10)
, capacity number(2)
);
create table CEREMONY
(cerem_type varchar2(10) primary key
, cerem_name varchar2(15)
, FIFA_theme_song varchar2(20)
, p_ID char(8)
, stad_location varchar2(20)
, foreign key (p_ID) references PERFORMER(p_ID) ON DELETE SET NULL
, foreign key (stad_location) references STADIUM(stad_location)ON DELETE SET NULL
);
create table REFEREE( ref_ID char(8) primary key
, ref_name varchar2(20)
, yo_exp number(2)
, match_ID char(8)
, registra_no char(6)
, foreign key (match_ID) references MATCH (match_ID) ON DELETE SET NULL
, foreign Key (registra_no) references TRANSPORTATION(registra_no) ON DELETE SET NULL);
insert into CEREMONY values('Opening', 'Speech', 'Colors', 'PP561475', 'Al-Waab Street');
insert into REFEREE values('RF503624','Mike Dean', 25, 'MM129456', 'QLM729'); select * from CEREMONY;
select * from REFEREE;
Bunch of tables are missing, referenced by foreign keys so I'm creating them first:
SQL> -- Missing tables, referenced by foreign keys
SQL> create table match (match_id char(8) primary key);
Table created.
SQL> create table stall (stall_id char(4) primary key);
Table created.
SQL> create table performer (p_id char(8) primary key);
Table created.
SQL>
Your tables:
SQL> create table STADIUM
2 ( stad_location varchar2(20) primary key
3 , stad_name varchar2(10)
4 , stad_capacity number(5)
5 , match_ID char(8)
6 , stall_ID char(4)
7 , foreign key (match_ID) references MATCH (match_ID) ON DELETE SET NULL
8 , foreign key (stall_ID) references STALL (stall_ID) ON DELETE SET NULL);
Table created.
SQL> create table TRANSPORTATION
2 (registra_no char(6) primary key
3 , transp_type varchar2(10)
4 , capacity number(2)
5 );
Table created.
SQL> create table CEREMONY
2 (cerem_type varchar2(10) primary key
3 , cerem_name varchar2(15)
4 , FIFA_theme_song varchar2(20)
5 , p_ID char(8)
6 , stad_location varchar2(20)
7 , foreign key (p_ID) references PERFORMER(p_ID) ON DELETE SET NULL
8 , foreign key (stad_location) references STADIUM(stad_location)ON DELETE SET NULL
9 );
Table created.
SQL> create table REFEREE( ref_ID char(8) primary key
2 , ref_name varchar2(20)
3 , yo_exp number(2)
4 , match_ID char(8)
5 , registra_no char(6)
6 , foreign key (match_ID) references MATCH (match_ID) ON DELETE SET NULL
7 , foreign Key (registra_no) references TRANSPORTATION(registra_no) ON DELETE SET NULL);
Table created.
SQL>
Inserts into tables referenced by ceremony and referee:
SQL> insert into performer values ('PP561475');
1 row created.
SQL> insert into stadium (stad_location) values ('Al-Waab Street');
1 row created.
SQL> insert into match values ('MM129456');
1 row created.
SQL> insert into transportation (registra_no) values ('QLM729');
1 row created.
SQL>
Your inserts & result of your select statements:
SQL> insert into CEREMONY values('Opening', 'Speech', 'Colors', 'PP561475', 'Al-Waab Street');
1 row created.
SQL> insert into REFEREE values('RF503624','Mike Dean', 25, 'MM129456', 'QLM729');
1 row created.
SQL> select * from CEREMONY;
CEREM_TYPE CEREM_NAME FIFA_THEME_SONG P_ID STAD_LOCATION
---------- --------------- -------------------- -------- --------------------
Opening Speech Colors PP561475 Al-Waab Street
SQL> select * from REFEREE;
REF_ID REF_NAME YO_EXP MATCH_ID REGIST
-------- -------------------- ---------- -------- ------
RF503624 Mike Dean 25 MM129456 QLM729
SQL>
Therefore, if you do everything right, everything is right.

cannot insert data into the 4th table

I used the following script to create 4 test tables: dept1, dept2, dept3, and dept4. The code is exactly the same for each table. All tables are created successfully but the insert into statements only worked for the first three tables. After dropping all tables and purge the recyclebin, I moved Create Table dept4 and the following insert into statements to the top of the code. Then the insert into statement does not work for Dept3. Basically, I can only insert data into the first three tables in the script and not able to insert data into the 4th table on the script.
The error message is the following:
SQL Error: ORA-00604: error occurred at recursive SQL level 1
ORA-30667: cannot drop NOT NULL constraint on a DEFAULT ON NULL column
00604. 00000 - "error occurred at recursive SQL level %s"
This happened after some users created identity columns in Oracle 12.1 database. Some users used:
GENERATED ALWAYS AS IDENTITY
Other users used:
GENERATED BY DEFAULT ON NULL AS IDENTITY
Code used:
CREATE TABLE DEPT1 (
DEPT1NO NUMBER(2) NOT NULL,
DNAME VARCHAR2(14),
LOC VARCHAR2(13),
CONSTRAINT DEPT1_PRIMARY_KEY PRIMARY KEY (DEPT1NO));
INSERT INTO DEPT1 VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT1 VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT1 VALUES (30,'SALES','CHICAGO');
INSERT INTO DEPT1 VALUES (40,'OPERATIONS','BOSTON');
CREATE TABLE DEPT2 (
DEPT2NO NUMBER(2) NOT NULL,
DNAME VARCHAR2(14),
LOC VARCHAR2(13),
CONSTRAINT DEPT2_PRIMARY_KEY PRIMARY KEY (DEPT2NO));
INSERT INTO DEPT2 VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT2 VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT2 VALUES (30,'SALES','CHICAGO');
INSERT INTO DEPT2 VALUES (40,'OPERATIONS','BOSTON');
CREATE TABLE DEPT3 (
DEPT3NO NUMBER(2) NOT NULL,
DNAME VARCHAR2(14),
LOC VARCHAR2(13),
CONSTRAINT DEPT3_PRIMARY_KEY PRIMARY KEY (DEPT3NO));
INSERT INTO DEPT3 VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT3 VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT3 VALUES (30,'SALES','CHICAGO');
INSERT INTO DEPT3 VALUES (40,'OPERATIONS','BOSTON');
CREATE TABLE DEPT4 (
DEPT4NO NUMBER(2) NOT NULL,
DNAME VARCHAR2(14),
LOC VARCHAR2(13),
CONSTRAINT DEPT4_PRIMARY_KEY PRIMARY KEY (DEPT4NO));
INSERT INTO DEPT4 VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT4 VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT4 VALUES (30,'SALES','CHICAGO');
INSERT INTO DEPT4 VALUES (40,'OPERATIONS','BOSTON');

How to move a table (with UDT as column) to another schema in oracle

I have a table which has a column of user defined type.There is a requirement to move this table along with UDT to another schema. I tried to create table as, the problem is it uses the UTD from original schema. Is there any way to achieve this?
Ex:
User 1:
CREATE TYPE employee_t AS OBJECT
(employee_id NUMBER
,employee_name VARCHAR2(30)
,salary NUMBER
,dept_id NUMBER);
CREATE TABLE department
(emp employee_t
,mgr varchar2(40)
,dept_id number
,dept_name varchar2(30));
In the above example, department table has a column called emp which is UDT.
Now on user2, if I do the following
create table department as select * from user1.department.
Table will be created as follows. Actually I want EMPLOYEE_T also to be moved to user2.
Desc department:
Name Null? Type
----------------------------------------- -------- -------------------
EMP USER1.EMPLOYEE_T
MGR VARCHAR2(40)
DEPT_ID NUMBER
DEPT_NAME VARCHAR2(30)

Oracle: how to get an object reference?

I have a sales object and a table (tab1) of sales objects. I want to create a second table (tab2) that contains references to the objects in tab1.
CREATE TYPE sales AS OBJECT
( marca NUMBER(4),
nume VARCHAR2(40),
orasp VARCHAR2(20),
nrv NUMBER(4)
);
/
DROP TABLE tab1 CASCADE CONSTRAINTS;
CREATE TABLE tab1
(
vanzator sales
);
I try to get the object ref, as so but hit the PL/SQL ORA-00904 "p" invalid identifier.
DECLARE
CURSOR c_pers_ref IS
SELECT REF (p)
FROM tab1 p;
rec_vanz_ref REF t2;
What am I doing wrong? Please help.
Ok. Got it. When creating tab1 changed the create line to CREATE TABLE tab1 OF sales;

Resources