1 Object Oriented Databases Simple Examples 2 Object Oriented Databases Nested Tables 3 Distributed Databases Replication 4 Distributed Databases- Horizontal Fragmentation 5 Distributed Databases - Vertical Fragmentation 6 XML Databases 7 Temporal Databases 8 Active Databases- Row level triggers 9 Active Databases- Statement level triggers 10 Spatial Databases PRACTICAL NO: 1 SQL> Create or replace type AddrType1 as object( 2 Pincode number(5), 3 Street char(20), 4 City varchar2(50), 5 state varchar2(40), 6 no number(4) ) 7 / Type created. SQL> create or replace type BranchType as object( 2 address AddrType1, 3 phone1 integer, 4* phone2 integer ) 5 / Type created. SQL> create or replace type BranchTableType as table of BranchType; 2 / Type created. SQL> create or replace type AuthorType as object( 2 name varchar2(50), 3* addr AddrType1 ) 4 / Type created. SQL> create table authors of AuthorType; Table created. SQL> create or replace type AuthorListType as varray(10) of ref AuthorType 2 / Type created. SQL> create or replace type PublisherType as object( 2 name varchar2(50), 3* addr AddrType1, branches BranchTableType) 4 / Type created. SQL> create table Publishers of PublisherType NESTED TABLE branches STORE as branchtable 2 / Table created. SQL> create table books( 2 title varchar2(50), 3 year date, 4 published_by ref PublisherType, 5* authors AuthorListType) 6 / Table created. SQL> insert into Authors values('Ravi',AddrType1(5002,'sstreet','pune','mha',04)); 1 row created. SQL> insert into Authors values('Ravi',AddrType1(7007,'sstreet','mumbai','mha',1007)); 1 row created. SQL> insert into Authors values('Sanjay',AddrType1(7008,'nstreet','nasik','mha',08)); 1 row created. SQL> insert into Authors values('Jharna',AddrType1(7003,'dstreet','mumbai','mha',1003)); 1 row created. SQL> insert into Authors values('Paul',AddrType1(7008,'sstreet','mumbai','mha',1007)); 1 row created. SQL> insert into Authors values('Eena',AddrType1(7006,'nstreet','mumbai','mha',1006)); 1 row created. SQL> insert into Authors values('Rama',AddrType1(8002,'dstreet','pune','mha',1003)); 1 row created. SQL> insert into Authors values('Jharna',AddrType1(8003,'dstreet','pune','mha',1003)); 1 row created. SQL> insert into Publishers values('Priya',AddrType1(4002,'rstreet','mumbai','mha',03),BranchTableTy pe(BranchType(AddrType1(5002,'fstreet','mumbai','mha',03),23406,69896))); 1 row created. SQL> insert into Publishers values('eklavya',AddrType1(7007,'sstreet','mumbai','mha',1007),BranchTab leType(BranchType(AddrType1(7007,'sstreet','mumbai','mha',1007),4543545, 8676775))); 1 row created. SQL> insert into Publishers values('john',AddrType1(7008,'sstreet','mumbai','mha',1007),BranchTableTy pe(BranchType(AddrType1(1002,'sstreet','nasik','mha',1007),456767,7675757 ))); 1 row created. SQL> insert into Publishers values('eklavya',AddrType1(7002,'sstreet','pune','mha',1007),BranchTable Type(BranchType(AddrType1(1002,'sstreet','pune','mha',1007),4543545,8676 775))); 1 row created. SQL> insert into Publishers values('william',AddrType1(6002,'sstreet','nasik','mha',1007),BranchTabl eType(BranchType(AddrType1(6002,'sstreet','nasik','mha',1007),4543545,867 6775))); 1 row created. SQL> insert into books select 'DSP','28-may- 1983',ref(pub),AuthorListType(ref(aut)) from Publishers pub,Authors aut where pub.name='john' and aut.name='Eena'; 1 row created. SQL> insert into books select 'compiler','09-jan- 1890',ref(pub),AuthorListType(ref(aut)) from Publi shers pub,Authors aut where pub.name='william' and aut.name='Jharna'; 2 rows created. SQL> insert into books select 'c','25-may- 1983',ref(pub),AuthorListType(ref(aut)) from Publishers pu b,Authors aut where pub.name='Priya' and aut.name='Ravi'; 2 rows created. a) List all of the authors that have the same address as their publisher: SQL> select a.name from authors a, publishers p where a.addr = p.addr; NAME -------------------------------------------------- Ravi Paul b) List all of the authors that have the same pin code as their publisher: SQL> select a.name from authors a, publishers p where a.addr.pincode = p.addr.pincode; NAME -------------------------------------------------- Ravi Sanjay Paul c) List all books that have 2 or more authors: SQL> select * from books b where 1<(select count(*) from table(b.authors)); no rows selected d) List the title of the book that has the most authors: SQL> Select title from books b, table(b.authors) group by title having count(*)=(select max(count(*) ) from books b, table(b.authors) group by title) 2 / TITLE -------------------------------------------------- c compiler e) List the name of the publisher that has the most branches: SQL> Select p.name from publishers p, table(p.branches) group by p.name having count(*)>=all (select count(*) from publishers p, table(p.branches) group by name); NAME -------------------------------------------------- eklavya f) Name of authors who have not published a book: SQL> select a.name from authors a where not exists(select b.title from books b, table(select authors from books b1 where b.title = b1.title) a2 where a.name = name); no rows selected g) Move all the branches that belong to the publisher 'tata' to the publisher joshi SQL> insert into table(select branches from publishers where name='william') select b.* from publishers p, table(p.branches) b where name = 'eklavya'; 2 rows created. h) List all authors who have published more than one book: SQL> select a.name from authors a, books b, table(b.authors) v where v.column_value = ref(a) group b y a.name having count(*) > 1; NAME -------------------------------------------------- Jharna Ravi i) List all books (title) where the same author appears more than once on the list of authors (assuming that an integrity constraint requiring that the name of an author is unique in a list of authors has not been specified). SQL> select title from authors a, books b, table(b.authors) v where v.column_value = ref(a) group by title having count(*) > 1; TITLE -------------------------------------------------- c compiler PRACTICAL NO: 2 SQL> create or replace type state61 as object 2 ( 3 st_code number(5), 4 st_name varchar2(40), 5 st_district varchar2(50), 6 st_pincode number(7) 7 ) 8 / Type created. SQL> create or replace type contact_detail61 as object 2 ( 3 residence_no number(10), 4 office_no number(10), 5 email varchar2(30), 6 fax number(10), 7 mobile number(10) 8 ) 9 / Type created. SQL> create or replace type address61 as object 2 ( 3 road_no varchar2(7), 4 road_name varchar2(40), 5 landmark varchar2(40), 6 state state61, 7 contact contact_detail61 8* ) 9 / Type created. SQL> create or replace type staff61 as object( 2 staff_id number(6), 3 staff_name varchar2(40), 4 staff_address address61, 5 staff_deptno number(3), 6 staff_sal number(6), 7 staff_other varchar2(40), 8 dob date, 9 member function getAge return number) 10 / Type created. SQL> create or replace type body staff61 as member function getAge return number as 2 begin 3 return trunc(months_between(sysdate,dob)/12); 4 end getAge; 5 end; 6 / Type body created. SQL> create or replace type staffTableType as table of staff61 2 / Type created. SQL> create or replace type dept61 as object 2 ( 3 dept_id number(3), 4 location varchar2(30), 5 dept_name varchar2(20), 6 emp staffTableType 7 ) 8 / Type created. SQL> create table dpt_refernce of dept61 nested table emp store as NTrelation 2 / Table created. SQL> insert into dpt_refernce values(1,'Mumbai','Sales',staffTableType(staff61(1,'EklavyaSingh',addr ess61('A-1','L.T. road','Status Hotel',state61(1,'Maharashtra','Mumbai',400092),contact_detail61(289 94177,28182729,'eklavyasingh@yahoo.com',28994177,9818967345)),1,10000,' HOD','17-aug-1984'))); 1 row created. SQL> insert into dpt_refernce values(1,'Mumbai','Sales',staffTableType(staff61(2,'SanjayShukla',addr ess61('C-1','yari road','',state61(1,'Maharashtra','Mumbai',400069),contact_detail61(26321115 ,263317 39,'sanjayshukla@rediffmail.com',26321116,918967345)),1,6000,'HOD','04- nov-1984'))); 1 row created. SQL> insert into dpt_refernce values(2,'Mumbai','Accounts',staffTableType(staff61(3,'Sharmila Dave', address61('C-1','M.G. road','Sanjeevani Hospital',state61(1,'Maharashtra','Mumbai',400078),contact_d etail61(28331112,28987058,'sam_dave@hotmail.com',28982430,9833196734)), 2,2000,'clerk','28-sep-1984') )); 1 row created. SQL> insert into dpt_refernce values(3,'Mumbai','Purchase',staffTableType(staff61(4,'AnshuDubey',add ress61('E-2','B.S.road','Vikas Kendra',state61(2,'Goa','Panji',419832),contact_detail61(26831112,268 97058,'anshudubey@gmail.com',26897059,9820636448)),3,7000,'','09-sep- 1984'))); 1 row created. SQL> insert into dpt_refernce values(3,'Mumbai','Purchase',staffTableType(staff61(5,'ParthJoshi',add ress61('E-2','Nehru road','HDFC Bank',state61(1,'Maharashtra','Vileparle',400056),contact_detail61(2 6149172,26157058,'parthjoshi@yahoo.co.in',26897059,9820739488)),3,9000,'',' 29-sep-1985'))); 1 row created. a) Display staff ID and department name of all employees. SQL> select p.dept_name, q.staff_id from dpt_refernce p,table(p.emp) q; DEPT_NAME STAFF_ID -------------------- ---------- Sales 1 Sales 2 Accounts 3 Purchase 4 Purchase 5 b) How many workers are in particular department. SQL> select p.dept_id,p.dept_name,count(q.staff_id) as number_of_employees from dpt_refernce p,table (p.emp) q where p.dept_name='Purchase' group by dept_id,dept_name; DEPT_ID DEPT_NAME NUMBER_OF_EMPLOYEES ---------- -------------------- ------------------- 3 Purchase 2 c) Find department name for particular staff name SQL> select p.dept_id,p.dept_name from dpt_refernce p,table(p.emp) q where q.staff_name='EklavyaSing h'; DEPT_ID DEPT_NAME ---------- -------------------- 1 Sales d) Display department-wise report SQL> select p.dept_id,p.dept_name,count(q.staff_id) as number_of_employees from dpt_refernce p,table (p.emp) q group by p.dept_id,p.dept_name ; DEPT_ID DEPT_NAME NUMBER_OF_EMPLOYEES ---------- -------------------- ------------------- 1 Sales 2 2 Accounts 1 3 Purchase 2 e) Display age and birth date of particular employee SQL> select q.dob,q.getAge() as Age from dpt_refernce p,table(p.emp) q where q.staff_name='EklavyaSi ngh'; DOB AGE --------- ---------- 17-AUG-84 28 PRACTICAL NO: 3 SQL> connect scott/tiger @replic Connected. SQL> ed Wrote file afiedt.buf 1 create table employee(eno number(3),ename varchar2(10),address varchar2(15), 2* email varchar2(15),salary number(10)) SQL> / Table created. SQL> connect scott/tiger @replic as sysdba Connected. SQL> ed Wrote file afiedt.buf 1 create public database link r1 connect to scott identified 2* by tiger using 'replic1' SQL> / Database link created. SQL> connect scott/tiger @replic1 Connected. SQL> ed Wrote file afiedt.buf 1 create table employee1(eno number(3),ename varchar2(10),address varchar2(15), 2* email varchar2(15),salary number(10)) SQL> / Table created. SQL> connect scott/tiger @replic1 as sysdba Connected. SQL> create public database link r connect to scott identified by tiger using'replic' ; Database link created. SQL> connect scott/tiger @replic Connected. SQL> ed Wrote file afiedt.buf 1 create or replace trigger trig after insert on employee for each row 2 begin 3 insert into employee1@r1 4 values(:new.eno,:new.ename,:new.address,:new.email,:new.salary); 5* end; SQL> / Trigger created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 1 Enter value for ename: anshu Enter value for address: nsp Enter value for email: a@yahoo.com Enter value for salary: 1000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(1,'anshu','nsp','a@yahoo.com',1000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 2 Enter value for ename: ravi Enter value for address: mira road Enter value for email: r@yahoo.com Enter value for salary: 8000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(2,'ravi','mira road','r@yahoo.com',8000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 3 Enter value for ename: shree Enter value for address: bhynder Enter value for email: s@yahoo.com Enter value for salary: 15000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(3,'shree','bhynder','s@yahoo.com ',15000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 4 Enter value for ename: Bhavesh Enter value for address: vasai Enter value for email: b@yahoo.com Enter value for salary: 3500 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(4,'Bhavesh','vasai','b@yahoo.com',3500) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 5 Enter value for ename: Jharna Enter value for address: naigaon Enter value for email: j@yahoo.com Enter value for salary: 10000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(5,'Jharna','naigaon','j@yahoo.com',10000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 6 Enter value for ename: karishma Enter value for address: borivali Enter value for email: k@yahoo.com Enter value for salary: 11000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(6,'karishma','borivali','k@yahoo.com',11000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 7 Enter value for ename: Ivan Enter value for address: kandivali Enter value for email: I@yahoo.com Enter value for salary: 2000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(7,'Ivan','kandivali','I@yahoo.com',2000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 8 Enter value for ename: shiv Enter value for address: andheri Enter value for email: sh@yahoo.com Enter value for salary: 13000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(8,'shiv','andheri','sh@yahoo.com',13000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 9 Enter value for ename: rashmi Enter value for address: jogeshwari Enter value for email: ra@yahoo.com Enter value for salary: 15000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(9,'rashmi','jogeshwari','ra@yahoo.com',15000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 10 Enter value for ename: bhavan Enter value for address: dahisar Enter value for email: bh@yahoo.com Enter value for salary: 16000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(10,'bhavan','dahisar','bh@yahoo.com',16000) 1 row created. SQL> select * from employee ; ENO ENAME ADDRESS EMAIL SALARY ---------- ---------- --------------- --------------- ---------- 1 anshu nsp a@yahoo.com 1000 2 ravi mira road r@yahoo.com 8000 3 shree bhynder s@yahoo.com 15000 4 Bhavesh vasai b@yahoo.com 3500 5 Jharna naigaon j@yahoo.com 10000 6 karishma borivali k@yahoo.com 11000 7 Ivan kandivali I@yahoo.com 2000 8 shiv andheri sh@yahoo.com 13000 9 rashmi jogeshwari ra@yahoo.com 15000 10 bhavan dahisar bh@yahoo.com 16000 10 rows selected. SQL> select * from employee1@r1 ; ENO ENAME ADDRESS EMAIL SALARY ---------- ---------- --------------- --------------- ---------- 1 anshu nsp a@yahoo.com 1000 2 ravi mira road r@yahoo.com 8000 3 shree bhynder s@yahoo.com 15000 4 Bhavesh vasai b@yahoo.com 3500 5 Jharna naigaon j@yahoo.com 10000 6 karishma borivali k@yahoo.com 11000 7 Ivan kandivali I@yahoo.com 2000 8 shiv andheri sh@yahoo.com 13000 9 rashmi jogeshwari ra@yahoo.com 15000 10 bhavan dahisar bh@yahoo.com 16000 10 rows selected. SQL> ed Wrote file afiedt.buf 1 select eno,salary from employee union select eno,salary from employee1@r1 SQL> / ENO SALARY ---------- ---------- 1 1000 2 8000 3 15000 4 3500 5 10000 6 11000 7 2000 8 13000 9 15000 10 16000 10 rows selected. SQL> ed Wrote file afiedt.buf 1 select eno,email from employee where salary=15000 union select eno,email 2* from employee1@r1 where salary=15000 SQL> / ENO EMAIL ---------- --------------- 3 s@yahoo.com 9 ra@yahoo.com SQL> ed Wrote file afiedt.buf 1 select ename,email from employee where eno=10 union 2* select ename,email from employee1@r1 where eno=10 SQL> / ENAME EMAIL ---------- --------------- bhavan bh@yahoo.com SQL> ed Wrote file afiedt.buf 1 select ename,address from employee where eno=8 union 2* select ename,address from employee1@r1 where eno=8 SQL> / ENAME ADDRESS ---------- --------------- shiv andheri PRACTICAL NO: 4 SQL> connect scott/tiger @horiz Connected. SQL> ed Wrote file afiedt.buf 1 create table employee(eno number(3),ename varchar2(10),address varchar2(15), 2* email varchar2(15),salary number(10)) SQL> / Table created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 1 Enter value for ename: anshu Enter value for address: nsp Enter value for email: a@yahoo.com Enter value for salary: 1000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(1,'anshu','nsp','a@yahoo.com',1000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 2 Enter value for ename: ravi Enter value for address: mira road Enter value for email: r@yahoo.com Enter value for salary: 8000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(2,'ravi','mira road','r@yahoo.com',8000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 3 Enter value for ename: shree Enter value for address: bhynder Enter value for email: s@yahoo.com Enter value for salary: 15000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(3,'shree','bhynder','s@yahoo.com',15000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 4 Enter value for ename: Bhavesh Enter value for address: vasai Enter value for email: b@yahoo.com Enter value for salary: 3500 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(4,'Bhavesh','vasai','b@yahoo.com',3500) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 5 Enter value for ename: Jharna Enter value for address: naigaon Enter value for email: j@yahoo.com Enter value for salary: 10000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(5,'Jharna','naigaon','j@yahoo.com',10000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 6 Enter value for ename: karishma Enter value for address: borivali Enter value for email: k@yahoo.com Enter value for salary: 11000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(6,'karishma','borivali','k@yahoo.com',11000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 7 Enter value for ename: Ivan Enter value for address: kandivali Enter value for email: I@yahoo.com Enter value for salary: 2000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(7,'Ivan','kandivali','I@yahoo.com',2000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 8 Enter value for ename: shiv Enter value for address: andheri Enter value for email: sh@yahoo.com Enter value for salary: 13000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(8,'shiv','andheri','sh@yahoo.com',13000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 9 Enter value for ename: rashmi Enter value for address: jogeshwari Enter value for email: ra@yahoo.com Enter value for salary: 15000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(9,'rashmi','jogeshwari','ra@yahoo.com',15000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 10 Enter value for ename: bhavan Enter value for address: dhaisar Enter value for email: bh@yahoo.com Enter value for salary: 16000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(10,'bhavan','dhaisar','bh@yahoo.com',16000) 1 row created. SQL> select * from employee 2 ; ENO ENAME ADDRESS EMAIL SALARY ---------- ---------- --------------- --------------- ---------- 1 anshu nsp a@yahoo.com 1000 2 ravi mira road r@yahoo.com 8000 3 shree bhynder s@yahoo.com 15000 4 Bhavesh vasai b@yahoo.com 3500 5 Jharna naigaon j@yahoo.com 10000 6 karishma borivali k@yahoo.com 11000 7 Ivan kandivali I@yahoo.com 2000 8 shiv andheri sh@yahoo.com 13000 9 rashmi jogeshwari ra@yahoo.com 15000 10 bhavan dhaisar bh@yahoo.com 16000 10 rows selected. SQL> connect scott/tiger @horiz as sysdba Connected. SQL> create public database link link12 connect to scott identified by tiger using 'horiz1' ; Database link created. SQL> connect scott/tiger @horiz Connected. SQL> ed Wrote file afiedt.buf 1* create table emp1 as select * from employee where salary <=15000 SQL> / Table created. SQL> select * from emp1 2 ; ENO ENAME ADDRESS EMAIL SALARY ---------- ---------- --------------- --------------- ----------------------------------- 1 anshu nsp a@yahoo.com 1000 2 ravi mira road r@yahoo.com 8000 3 shree bhynder s@yahoo.com 15000 4 Bhavesh vasai b@yahoo.com 3500 5 Jharna naigaon j@yahoo.com 10000 6 karishma borivali k@yahoo.com 11000 7 Ivan kandivali I@yahoo.com 2000 8 shiv andheri sh@yahoo.com 13000 9 rashmi jogeshwari ra@yahoo.com 15000 9 rows selected. SQL> ed Wrote file afiedt.buf 1* create table emp2 as select * from employee where salary>15000 SQL> / Table created. SQL> select * from emp2; ENO ENAME ADDRESS EMAIL SALARY ---------- ---------- --------------- --------------- ---------- 3 shree bhynder s@yahoo.com 15000 4 Bhavesh vasai b@yahoo.com 35000 5 Jharna naigaon j@yahoo.com 100000 7 Ivan kandivali I@yahoo.com 20000 8 shiv andheri sh@yahoo.com 13000 9 rashmi jogeshwari ra@yahoo.com 150000 10 Bhavan dahisar bh@yahoo.com 160000 7 rows selected. SQL> select eno,salary from emp1 union select eno,salary from emp2 @link12; ENO SALARY ---------- ---------- 1 10000 2 8000 3 15000 4 35000 5 100000 6 11000 7 20000 8 13000 9 150000 10 160000 10 rows selected. SQL> ed Wrote file afiedt.buf 1 select eno,email from emp1 where salary=15000 union 2* select eno,email from emp2 @link12 where salary=15000 SQL> / ENO EMAIL ---------- --------------- 3 s@yahoo.com SQL> ed Wrote file afiedt.buf 1 select ename,email from emp1 where eno=8 union 2* select ename,email from emp2 @link12 where eno=8 SQL> / ENAME EMAIL ---------- --------------- shiv sh@yahoo.com SQL> ed Wrote file afiedt.buf 1 select ename,address from emp1 where eno=7 union 2* select ename,address from emp2 @link12 where eno=7 SQL> / ENAME ADDRESS ---------- --------------- Ivan kandivali PRACTICAL NO: 5 SQL> connect scott/tiger @verti Connected. SQL> create table employee(eno number(3),ename varchar2(10),address varchar2(15), 2 email varchar2(15),salary number(10)) ; Table created. SQL> insert into employee(eno,ename,address,email,salary) 2 values(&eno,'&ename','&address','&email',&salary); Enter value for eno: 1 Enter value for ename: anshu Enter value for address: nsp Enter value for email: a@yahoo.com Enter value for salary: 100000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(1,'anshu','nsp','a@yahoo.com',100000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 2 Enter value for ename: ravi Enter value for address: mira road Enter value for email: r@yahoo.com Enter value for salary: 140000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(2,'ravi','mira road','r@yahoo.com',140000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 3 Enter value for ename: shree Enter value for address: bhynder Enter value for email: s@yahoo.com Enter value for salary: 15000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(3,'shree','bhynder','s@yahoo.com',15000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 4 Enter value for ename: Bhavesh Enter value for address: vasai Enter value for email: b@yahoo.com Enter value for salary: 35000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(4,'Bhavesh','vasai','b@yahoo.com',35000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 5 Enter value for ename: Jharna Enter value for address: naigaon Enter value for email: j@yahoo.com Enter value for salary: 10000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(5,'Jharna','naigaon','j@yahoo.com',10000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 6 Enter value for ename: jarishma Enter value for address: borivali Enter value for email: k@yahoo.com Enter value for salary: 11000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(6,'jarishma','borivali','k@yahoo.com',11000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 7 Enter value for ename: Ivan Enter value for address: kandivali Enter value for email: I@yahoo.com Enter value for salary: 2000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(7,'Ivan','kandivali','I@yahoo.com',2000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 8 Enter value for ename: shiv Enter value for address: andheri Enter value for email: sh@yahoo.com Enter value for salary: 1500000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(8,'shiv','andheri','sh@yahoo.com',1500000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 9 Enter value for ename: rashmi Enter value for address: jogeshwari Enter value for email: ra@yahoo.com Enter value for salary: 160000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(9,'rashmi','jogeshwari','ra@yahoo.com',160000) 1 row created. SQL> ed Wrote file afiedt.buf 1 insert into employee(eno,ename,address,email,salary) 2* values(&eno,'&ename','&address','&email',&salary) SQL> / Enter value for eno: 10 Enter value for ename: Bhavan Enter value for address: dahisar Enter value for email: bh@yahoo.com Enter value for salary: 16000 old 2: values(&eno,'&ename','&address','&email',&salary) new 2: values(10,'Bhavan','dahisar','bh@yahoo.com',16000) 1 row created. SQL> select * from employee; ENO ENAME ADDRESS EMAIL SALARY ---------- ---------- --------------- --------------- ---------- 1 anshu nsp a@yahoo.com 100000 2 ravi mira road r@yahoo.com 140000 3 shree bhynder s@yahoo.com 15000 4 Bhavesh vasai b@yahoo.com 35000 5 Jharna naigaon j@yahoo.com 10000 6 jarishma borivali k@yahoo.com 11000 7 Ivan kandivali I@yahoo.com 2000 8 shiv andheri sh@yahoo.com 1500000 9 rashmi jogeshwari ra@yahoo.com 160000 10 Bhavan dahisar bh@yahoo.com 16000 10 rows selected. SQL> connect scott/tiger @verti1 as sysdba Connected. SQL> create public database link a1 connect to scott identified by tiger 2 using 'verti'; Database link created. SQL> connect scott/tiger @verti; Connected. SQL> create table emp1 as select eno,ename,address from employee; Table created. SQL> select * from emp1; ENO ENAME ADDRESS ---------- ---------- --------------- 1 anshu nsp 2 ravi mira road 3 shree bhynder 4 Bhavesh vasai 5 Jharna naigaon 6 jarishma borivali 7 Ivan kandivali 8 shiv andheri 9 rashmi jogeshwari 10 Bhavan dahisar 10 rows selected. SQL> connect scott/tiger @verti1; Connected. SQL> ed Wrote file afiedt.buf 1* create table emp2 as select eno,email,salary from employee @a1 SQL> / Table created. SQL> select * from emp2; ENO EMAIL SALARY ---------- --------------- ---------- 1 a@yahoo.com 100000 2 r@yahoo.com 140000 3 s@yahoo.com 15000 4 b@yahoo.com 35000 5 j@yahoo.com 10000 6 k@yahoo.com 11000 7 I@yahoo.com 2000 8 sh@yahoo.com 1500000 9 ra@yahoo.com 160000 10 bh@yahoo.com 16000 10 rows selected. SQL> select emp1.eno,emp2.salary from emp2,emp1 @a1 2 where emp1.eno=5 and emp1.eno=emp2.eno; ENO SALARY ---------- ---------- 5 10000 SQL> select emp1.eno,emp2.email from emp2,emp1 @a1 2 where emp1.ename='anshu' and emp1.eno=emp2.eno 3 ; ENO EMAIL ---------- --------------- 1 a@yahoo.com SQL> select emp1.ename,emp2.email from emp2 , emp1 @a1 2 where emp1.eno=3 and emp1.eno=emp2.eno; ENAME EMAIL ---------- --------------- shree s@yahoo.com SQL> select emp1.ename,emp2.salary from emp2 , emp1 @a1 2 where emp1.eno=emp2.eno and emp2.salary>2000 3 ; ENAME SALARY ---------- ---------- anshu 100000 ravi 140000 shree 15000 Bhavesh 35000 Jharna 10000 jarishma 11000 shiv 1500000 rashmi 160000 Bhavan 16000 9 rows selected. PRACTICAL NO: 6 Create different Types and Tables SQL> create table emp_xml15( 2 dept_id number(4), 3 employee_spec XMLtype); Table created. Insertion of different values in the tables Table Name1: emp_xml15 Format : emp_xml15 (dept_id, employee_spec) SQL> insert into emp_xml15 values(1,XMLtype( 2 '<emp id="1"> 3 <name> sharmila </name> 4 <email>dave@yahoo.com</email> 5 <acc_no>23456</acc_no> 6 <mgr_email>rekha.shah@hotmail.com</mgr_email> 7 <doj>12/12/2003</doj> 8 </emp>')); 1 row created. SQL> insert into emp_xml15 values(1,XMLtype( 2 '<emp id="2"> 3 <name> anita </name> 4 <email>ani@yahoo.com</email> 5 <acc_no>234346</acc_no> 6 <mgr_email>rekha.shah@hotmail.com</mgr_email> 7 <doj>2/6/2003</doj> 8 </emp>')); 1 row created. SQL> insert into emp_xml15 values(1,XMLtype( 2 '<emp id="3"> 3 <name> ekta </name> 4 <email>ektabhatt@yahoo.com</email> 5 <acc_no>2343456</acc_no> 6 <mgr_email>ekta.bhatt@hotmail.com</mgr_email> 7 <doj>24/5/2001</doj> 8 </emp>')); 1 row created. SQL> insert into emp_xml15 values(1,XMLtype( 2 '<emp id="4"> 3 <name> nancy </name> 4 <email>nancyshah@yahoo.com</email> 5 <acc_no>2343678</acc_no> 6 <mgr_email>ekta.shah@hotmail.com</mgr_email> 7 <doj>21/5/2002</doj> 8 </emp>')); 1 row created. SQL> insert into emp_xml15 values(1,XMLtype( 2 '<emp id="5"> 3 <name> falguni </name> 4 <email>falgunishah@yahoo.com</email> 5 <acc_no>2343345</acc_no> 6 <mgr_email>falguni.shah@hotmail.com</mgr_email> 7 <doj>1/8/2002</doj> 8 </emp>')); 1 row created. SQL> insert into emp_xml15 values(1,XMLtype( 2 '<emp id="6"> 3 <name> sweta </name> 4 <email>swetamehta@yahoo.com</email> 5 <acc_no>2343890</acc_no> 6 <mgr_email>sweta.mehta@hotmail.com</mgr_email> 7 <doj>2/1/2001</doj> 8 </emp>')); 1 row created. SQL> insert into emp_xml15 values(2,XMLtype( 2 '<emp id="7"> 3 <name> aarti </name> 4 <email>aartigupta@yahoo.com</email> 5 <acc_no>23433898</acc_no> 6 <mgr_email>falguni.shah@hotmail.com</mgr_email> 7 <doj>4/9/2002</doj> 8 </emp>')); 1 row created. SQL> insert into emp_xml15 values(2,XMLtype( 2 '<emp id="8"> 3 <name> sandy </name> 4 <email>sagupta@yahoo.com</email> 5 <acc_no>23567898</acc_no> 6 <mgr_email>sweta.shah@hotmail.com</mgr_email> 7 <doj>4/4/2004</doj> 8 </emp>')); 1 row created. Firing queries on the created tables a) Retrieve the names of employee: SQL> select e.employee_spec.extract('//name/text()').getStringVal() "EMP_NAME" from emp_xml15 e; Output: EMP_NAME -------------------------------------------------------------------------------- sharmila anita ekta nancy falguni sweta aarti sandy 8 rows selected. b) Retrieve the acc_no of employees: SQL>select e.employee_spec.extract('//acc_no/text()').getStringVal() 2* "Acc_No" from emp_xml15 e; Output: Acc_No ------------------------------------------------------------------------------ 23456 234346 2343456 2343678 2343345 2343890 23433898 23567898 8 rows selected. c) Retrieve the names, acc_no, email of employees: SQL> select e.employee_spec.extract('//name/text()').getStringVal() 2 "NAME",e.employee_spec.extract('//acc_no/text()').getStringVal() 3 "ACC_NO",e.employee_spec.extract('//email/text()').getStringVal() 4 "EMAIL" from emp_xml15 e 5 / Output: NAME ACC_NO EMAIL -------------------------------------------------------------------------------- sharmila 23456 dave@yahoo.com anita 234346 ani@yahoo.com ekta 2343456 ektabhatt@yahoo.com nancy 2343678 nancyshah@yahoo.com falguni 2343345 falgunishah@yahoo.com sweta 2343890 swetamehta@yahoo.com aarti 23433898 aartigupta@yahoo.com sandy 23567898 sagupta@yahoo.com 8 rows selected. d) Update the 3 rd record from the table and display the name of an employee: SQL> update emp_xml15 e set employee_spec=XMLtype('<emp id="3"> 2 <name> ekta </name> 3 <email>ektabhatt@yahoo.com</email> 4 <acc_no>2343456</acc_no> 5 <mgr_email>ekta.bhatt@hotmail.com</mgr_email> 6 <doj>24/5/2001</doj> 7 <update>This is the updated record</update> 8 </emp>') 9 where 10 e.employee_spec.extract('//name/text()').getStringVal() 11 ='ekta' ; 1 row updated. SQL> select e.employee_spec.extract('//name/text()').getStringVal()"NAME", 2 e.employee_spec.getClobVal() "EMP_SPECIFICATION" 3 from emp_xml15 e where 4 e.employee_spec.extract('//name/text()').getStringVal()='ekta ' ; Output: NAME ------------------------------------------------------------------------------- EMP_SPECIFICATION ------------------------------------------------------------------------------- ekta <emp id="3"> <name> ekta </name> <email>ektabhatt@yahoo.com</email> <acc_no>2343456</acc_no> <mgr_email>ekta.bhatt@hotmail.com</mgr_email> <doj>24/5/2001</doj> <update>This is the updated record</update> </emp> e) Delete the 4 th record from the table: SQL> delete from emp_xml15 e 2 where e.employee_spec.extract('//name/text()').getStringVal() 3 ='nancy '; 1 row deleted. SQL> select e.employee_spec.extract('//name/text()').getStringVal() "NAME" from emp_xml151 e; Output: NAME -------------------------------------------------------------------------------- sharmila anita ekta falguni sweta aarti 6 rows selected. PRACTICAL NO: 7-A SQL> create table customer 2 ( 3 cust_name varchar(10), 4 customer_no number(5), 5 open_date timestamp(2), 6 transac_time timestamp(2) 7 ) 8 ; Table created. SQL> insert into customer values('keyur',1,'12-aug-2011 1.00.00.000000 pm','14-sep-2012 10.00 am'); 1 row created. SQL> insert into customer values('shlok',2,'18-feb-1983 8.45.00.000000 pm','14-jan-2011 9.23.01 am' ); 1 row created. SQL> insert into customer values('shruti',2023,'16-jan-2000 10.30.00.000000 am','10-june-2004 3.30 p m'); 1 row created. SQL> insert into customer values('dhara',2002,'23-feb-1992 5.30.00.000000 pm','13-dec-2006 12.00 pm' ); 1 row created. SQL> insert into customer values('akash',2012,'24-sep-2002 4.30.00.000000 pm','12-july-2005 9.30 am' ); 1 row created. SQL> select * from customer; CUST_NAME CUSTOMER_NO ---------- ----------- OPEN_DATE --------------------------------------------------------------------------- TRANSAC_TIME --------------------------------------------------------------------------- keyur 1 12-AUG-11 01.00.00.00 PM 14-SEP-20 12.10.00.00 AM shlok 2 18-FEB-83 08.45.00.00 PM 14-JAN-20 11.09.23.01 AM CUST_NAME CUSTOMER_NO ---------- ----------- OPEN_DATE --------------------------------------------------------------------------- TRANSAC_TIME --------------------------------------------------------------------------- shruti 2023 16-JAN-00 10.30.00.00 AM 10-JUN-20 04.03.30.00 PM dhara 2002 23-FEB-92 05.30.00.00 PM CUST_NAME CUSTOMER_NO ---------- ----------- OPEN_DATE --------------------------------------------------------------------------- TRANSAC_TIME --------------------------------------------------------------------------- 13-DEC-20 06.12.00.00 PM akash 2012 24-SEP-02 04.30.00.00 PM 12-JUL-20 05.09.30.00 AM SQL> select * from customer where to_char(open_date,'HH.MI AM') 2 LIKE '10.30 am' 3 ; no rows selected SQL> select * from customer where to_char(open_date,'hh.mi am') like '10.30 am'; CUST_NAME CUSTOMER_NO ---------- ----------- OPEN_DATE --------------------------------------------------------------------------- TRANSAC_TIME --------------------------------------------------------------------------- shruti 2023 16-JAN-00 10.30.00.00 AM 10-JUN-20 04.03.30.00 PM SQL> select * from customer where to_char(transac_time,'hh.mi am') like '8.46 pm'; no rows selected SQL> select * from customer where to_char(transac_time,'hh.mi pm') like '8.46 pm'; no rows selected SQL> select * from customer where to_char(open_date,'dd-mm-yyyy hh.mi am') like '27-10-1999 11.30 am; no rows selected SQL> select * from customer where to_char(open_date,'dd')='18'; CUST_NAME CUSTOMER_NO ---------- ----------- OPEN_DATE --------------------------------------------------------------------------- TRANSAC_TIME --------------------------------------------------------------------------- shlok 2 18-FEB-83 08.45.00.00 PM 14-JAN-20 11.09.23.01 AM SQL> select * from customer where to_char(open_date,'yy')='92'; CUST_NAME CUSTOMER_NO ---------- ----------- OPEN_DATE --------------------------------------------------------------------------- TRANSAC_TIME --------------------------------------------------------------------------- dhara 2002 23-FEB-92 05.30.00.00 PM 13-DEC-20 06.12.00.00 PM SQL> select count(open_date) from customer where to_char(open_date,'mm')='02'; COUNT(OPEN_DATE) ---------------- 2 PRACTICAL NO: 7-B Table definition : SQL> Create table tbl_shares15 2 ( 3 cname varchar2(20), 4 nofshares number(5), 5 pricepshare number(5), 6 transtime timestamp(6) 7 ); Table created. Insertion of different values in the tables Table Name: tbl_shares15 Format : tbl_shares(cname, noofshares, pricepshare , transtime) SQL> insert into tbl_shares15 values(Reliance Infocom,250,25,systimestamp); 1 row created. SQL> insert into tbl_shares15 values('Tata',205,20,'05-jun-04 11.45.00.000000 am'); 1 row created. SQL> insert into tbl_shares15 values('Wipro',250,25,'10-mar-03 06.15.00.000000 pm'); 1 row created. SQL> insert into tbl_shares15 values('Patni',115,15,'08-may-01 07.25.00.000000 am'); 1 row created. SQL> insert into tbl_shares15 values('TCS',140,12,'14-apr-05 05.30.00.000000 pm'); 1 row created. SQL> insert into tbl_shares15 values('Google',310,30,'12-sep-03 10.30.00.000000 am'); 1 row created. SQL> insert into tbl_shares15 values('Hero Honda',100,250,'21-aug-04 05.30.00.000000 pm'); 1 row created. SQL> select * from tbl_shares15; Output: CNAME NOFSHARES PRICEPSHARE TRANSTIME ------------- ---------------------- ---------------------- -------------------- Infosys 110 10 01-JAN-03 04.00.00.000000 PM Tata 205 20 05-JUN-04 11.45.00.000000 AM Wipro 250 25 10-MAR-03 06.15.00.000000 PM Patni 115 15 08-MAY-01 07.25.00.000000 AM TCS 140 12 14-APR-05 05.30.00.000000 PM Google 310 30 12-SEP-03 10.30.00.000000 AM Hero Honda 100 250 21-AUG-04 05.30.00.000000 PM 5 rows selected. QUERIES : 1) Find all the names of a company whose share price is more than Rs.100 at 11:45 A.M. SQL> select cname from tbl_shares15 2 where pricepshare>15 3 and to_char(transtime,'HH12:MI:AM')='11:45:AM' SQL> / Output: cname -------------------- Tata 2) Find the name of company which has highest shares price at 5.00 P.M. SQL> select cname from tbl_shares15 where pricepshare in 2 (select max(pricepshare) fromtbl_shares15 3 where to_char(transtime,'HH12:MI:AM')='05:30:PM') SQL> / Output: cname -------------------- Hero Honda PRACTICAL NO: 8 Create different Types and Tables SQL> create table Empl( eno number(8) primary key, ename varchar(20), hrs number(8), pno number(8), super_no number(8) CONSTRAINT sup UNIQUE ); Table created. SQL> create table project( pno number(8) primary key, pname varchar(20), thrs number(8), super_no number(8) CONSTRAINT supfk references empl(super_no) ); Table created. Insertion of different values in the tables Table Name1: Empl Format : Empl(eno, ename, hrs, pno , super_no) SQL> insert into Empl values(1,'ekta',7,10,1001); 1 row created. SQL> insert into Empl values(2,'sweta',5,20,1002); 1 row created. SQL> insert into Empl values(3,'sharmila',3,10,1003); 1 row created. SQL> insert into Empl values(4,'anita',1,20,1004); 1 row created. SQL> insert into Empl values(5,'sandeep',5,30,1005); 1 row created. SQL> insert into Empl values(6,'gautam',8,40,1006); 1 row created. SQL> insert into Empl values(7,'akshay',3,30,1007); 1 row created. SQL>insert into Empl values(8,'sagar',12,40,1008); 1 row created. SQL> insert into Empl values(9,'aarti',1,10,1009); 1 row created. SQL> insert into Empl values(10,'bhakti',9,20,1010); 1 row created. Table Name2: Project Format : Project(pno, pname, thrs ,super_no) SQL> insert into project values(10,'distributed',10,1001); 1 row created. SQL> insert into project values(20,'parallel',6,1002); 1 row created. SQL> insert into project values(30,'active',5,1005); 1 row created. SQL> insert into project values(40,'temporal',5,1008); 1 row created. Firing queries on the created tables Create trigger according to events specified. a) Creating a trigger to insert new employee tuple and display the new total hours from project table. Event1: Insert a new employee tuple Event: New Employee is assigned to a project Condition: pno value is not Null when emp is assigned to a project Action: update the total thrs of project by adding hrs to old thrs. Trigger: SQL> create or replace Trigger thrs after insert on Empl for each row when(New.pno IS NOT NULL) begin update project set thrs=thrs + :New.hrs where pno=:New.pno; end; Trigger created. /*Inserting values in Empl to so that trigger will be fired on project table &will update thrs value since trigger is fired after inserting value in empl table*/ SQL> insert into Empl values(11,'nancy',4,30,1011); 1 row created. Output: SQL> select * from project; PNO PNAME THRS SUPER_NO ---------- ------------------- ---------- ----------------- 10 distributed 10 1001 20 parallel 6 1002 30 active 9 1005 40 temporal 5 1008 b) Creating a trigger to change the hrs of existing employee and display the new total hours from project table. Event2: Changing the hrs of existing employee. Event: employee is assigned new hrs Condition: pno value is not NULL when emp is assigned new hrs. Action: update thrs of project by adding hrs to thrs & subtract old hrs from it. Trigger: SQL> create Trigger thrs1 after update of hrs on Empl for each row when(New.pno IS NOT NULL) begin update project set thrs=thrs+:New.hrs-:Old.hrs where pno=:New.pno; end; Trigger created. SQL> update Empl set hrs=10 where eno=11; 1 row updated. Ouput: SQL> select * from project; PNO PNAME THRS SUPER_NO ---------- ------------------- ---------- ----------------- 10 distributed 10 1001 20 parallel 6 1002 30 active 15 1005 40 temporal 5 1008 C) Creating a trigger to change the project of an employee and display the new total hours from project table. Event3: Changing the project of an employee. Event: Change of Project Condition: No condition Action: update the thrs of new project by adding new hrs & subtract old hrs from thrs from old project. Trigger: SQL> create Trigger thrs2 after update of pno on Empl for each row when(New.pno IS NOT NULL) begin update project set thrs=thrs+:New.hrs-:Old.hrs where pno=:New.pno; end; Trigger created. SQL> update Empl set pno=10 where eno=2; 1 row updated. SQL> update Empl set pno=20 where eno=7; 1 row updated. Output: Same output for 2 update: SQL> select * from project; PNO PNAME THRS SUPER_NO ---------- ------------------- ---------- ----------------- 10 distributed 10 1001 20 parallel 6 1002 30 active 15 1005 40 temporal 5 1008 d) Creating a trigger to delete the project of an employee. Event4: Deleting 1 or more project of an employee. Event: Deleting the project. Condition: OLD.pno is not NULL. Action: update the thrs by subtracting old hrs. Trigger: SQL> create trigger thrs4 after delete on Empl for each row when(OLD.pno IS NOT NULL) begin update project set thrs=thrs-:OLD.hrs where pno=:OLD.pno; end; Trigger created. SQL> delete from Empl where eno=11; 1 row deleted. SQL> select * from project; PNO PNAME THRS SUPER_NO ---------- ------------------- ---------- ----------------- 10 distributed 10 1001 20 parallel 6 1002 30 active 5 1005 40 temporal 5 1008 PRACTICAL NO: 9 Create different Types and Tables SQL> create table Empl( eno number(8) primary key, ename varchar(20), hrs number(8), pno number(8), super_no number(8) CONSTRAINT sup UNIQUE ); Table created. SQL> create table project( pno number(8) primary key, pname varchar(20), thrs number(8), super_no number(8) CONSTRAINT supfk references empl(super_no) ); Table created. Insertion of different values in the tables Table Name1: Empl Format : Empl(eno, ename, hrs, pno , super_no) SQL> insert into Empl values(1,'ekta',7,10,1001); 1 row created. SQL> insert into Empl values(2,'sweta',5,20,1002); 1 row created. SQL> insert into Empl values(3,'sharmila',3,10,1003); 1 row created. SQL> insert into Empl values(4,'anita',1,20,1004); 1 row created. SQL> insert into Empl values(5,'sandeep',5,30,1005); 1 row created. SQL> insert into Empl values(6,'gautam',8,40,1006); 1 row created. SQL> insert into Empl values(7,'akshay',3,30,1007); 1 row created. SQL>insert into Empl values(8,'sagar',12,40,1008); 1 row created. SQL> insert into Empl values(9,'aarti',1,10,1009); 1 row created. SQL> insert into Empl values(10,'bhakti',9,20,1010); 1 row created. Table Name2: Project Format : Project(pno, pname, thrs ,super_no) SQL> insert into project values(10,'distributed',10,1001); 1 row created. SQL> insert into project values(20,'parallel',6,1002); 1 row created. SQL> insert into project values(30,'active',5,1005); 1 row created. SQL> insert into project values(40,'temporal',5,1008); 1 row created. Firing queries on the created tables SQL> select * from Empl where pno=10; ENO ENAME HRS PNO SUPER_NO ---------- ---------- --------------- --------------- ----------------------------------- 1 ekta 7 10 1001 3 sharmila 3 10 1003 9 aarti 1 10 1009 Create trigger according to events specified. a) Creating a trigger to increase hours of all employees working on the project number 10 by 2. Event1: Changing the hrs of existing employee. Event: Employee is assigned new hrs Condition: pno value is not NULL when emp is assigned new hrs. Action: Update thrs of employees working on project number 10 by 2 Trigger: SQL> create Trigger thrs after update of hrs on Empl for each statement when(New.pno IS NOT NULL) begin update Empl set hrs=:hrs+2; where pno=10; end; Trigger created. SQL> select * from Empl where pno=10; ENO ENAME HRS PNO SUPER_NO ---------- ---------- --------------- --------------- ----------------------------------- 1 ekta 9 10 1001 3 sharmila 5 10 1003 9 aarti 3 10 1009 PRACTICAL NO: 10 SQL> create table university_15 ( 2 mkt_id number primary key, 3 name varchar2(32), 4 shape mdsys.sdo_geometry); Table created. SQL> insert into university_15 values( 2 1, 3 'abc', 4 mdsys.sdo_geometry( 5 2003, -- 2-dimensional polygon 6 null, 7 null, 8 mdsys.sdo_elem_info_array(1,1003,3), -- one rectangle (1003 =exterior) 9 mdsys.sdo_ordinate_array(1,1, 5,7) 10 -- only 2 points needed to 11 -- define rectangle (lower left and upper right) with 12 -- cartesian-coordinate data 13 ) 14 ); 1 row created. SQL> insert into university_15 values( 1 2, 2 'pqr', 3 mdsys.sdo_geometry( 4 2003, -- 2-dimensional polygon 5 null, 6 null, 7 mdsys.sdo_elem_info_array(1,1003,1), 8 -- one polygon (exterior polygon ring) 9 mdsys.sdo_ordinate_array(5,1, 8,1, 8,6, 5,7, 5,1) 10 ) 11 ); 1 row created. insert into university_15 values( 1 3, 2 'mno', 3 mdsys.sdo_geometry( 4 2003, -- 2-dimensional polygon 5 null, 6 null, 7 mdsys.sdo_elem_info_array(1,1003,1), 8 -- one polygon (exterior polygon ring) 9 mdsys.sdo_ordinate_array(3,3, 6,3, 6,5, 4,5, 3,3) 10 ) 11 ); 1 row created. SQL>insert into university_15 values( 1 4, 2 xyz, 3 mdsys.sdo_geometry( 4 2003, -- 2-dimensional polygon 5 null, 6 null, 7 mdsys.sdo_elem_info_array(1,1003,4), -- one circle 8 mdsys.sdo_ordinate_array(8,7, 10,9, 8,11) 9 ) 10 ); 1 row created. SQL>insert into user_sdo_geom_metadata values ( 1 university_15, 2 shape, 3 mdsys.sdo_dim_array( 4 mdsys.sdo_dim_element(x, 0, 20, 0.005), 5 mdsys.sdo_dim_element(y, 0, 20, 0.005) 6 ), 7 null -- srid 8 ); 1 row created. SQL > create index university_spatial_idx on university_15(shape) indextype is mdsys.spatial_index; Index created. 1) Find the topological intersection of two geometries. SQL> select sdo_geom.sdo_intersection(c_a.shape, c_c.shape, 0.005) from university_15 c_a, university_15 c_c where c_a.name = 'abc' and c_c.name = 'mno'; Output: SDO_GEOM.SDO_INTERSECTION(C_A.SHAPE,C_C.SHAPE,0.005)(SDO _GTYPE, SDO_SRID, SDO_PO -------------------------------------------------------------------------------- SDO_GEOMETRY(2003, NULL, NULL, SDO_ELEM_INFO_ARRAY(1, 1003, 1), SDO_ORDINATE_ARR AY(4, 5, 3, 3, 5, 3, 5, 5, 4, 5)) 2) Find whether two geometric figures are equivalent to each other. SQL>select sdo_geom.relate(c_b.shape, 'anyinteract', c_d.shape, 0.005) from university_15 c_b, university_15 c_d where c_b.name = 'pqr' and c_d.name = 'xyz'; Output: SDO_GEOM.RELATE(C_B.SHAPE,'ANYINTERACT',C_D.SHAPE,0.005) -------------------------------------------------------------------------------- FALSE SQL>select sdo_geom.relate(c_b.shape, 'anyinteract', c_a.shape, 0.005) from university_15 c_b, university_15 c_a where c_b.name = 'pqr' and c_a.name = 'abc'; Output: SDO_GEOM.RELATE(C_B.SHAPE,'ANYINTERACT',C_A.SHAPE,0.005) -------------------------------------------------------------------------------- TRUE 3) Find the areas of all direction locations. SQL>select name, sdo_geom.sdo_area(shape, 0.005) from university_15; Output: NAME SDO_GEOM.SDO_AREA(SHAPE,0.005) -------------------------------- ------------------------------ abc 24 pqr 16.5 mno 5 xyz 12.5663706 4) Find the area of only one location abc. SQL>select c.name, sdo_geom.sdo_area(c.shape, 0.005) from university_15 c where c.name = 'abc'; Output: NAME SDO_GEOM.SDO_AREA(C.SHAPE,0.005) -------------------------------- -------------------------------- abc 24 5) Find the distance between two geometries. SQL>select sdo_geom.sdo_distance(c_b.shape, c_d.shape, 0.005) from university_15 c_b, university_15 c_d where c_b.name = 'pqr' and c_d.name = 'xyz'; Output: SDO_GEOM.SDO_DISTANCE(C_B.SHAPE,C_D.SHAPE,0.005) ------------------------------------------------ .846049894