0% found this document useful (0 votes)
230 views117 pages

Adms Final

The document discusses distributed database concepts like horizontal and vertical fragmentation. It provides examples of creating distributed database links and fragmenting tables across different databases. Queries are shown to retrieve distributed data by joining tables located on different databases. The document contains detailed examples to illustrate key concepts of distributed databases.

Uploaded by

ARAFAT
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
230 views117 pages

Adms Final

The document discusses distributed database concepts like horizontal and vertical fragmentation. It provides examples of creating distributed database links and fragmenting tables across different databases. Queries are shown to retrieve distributed data by joining tables located on different databases. The document contains detailed examples to illustrate key concepts of distributed databases.

Uploaded by

ARAFAT
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 117

P

AP ER

IV

SE
CT IO N

II

ADVANCED DATABASE SYSTEM

ADVANCED DATABASE SYSTEM


S r. N 1 2 3 4 5 6 7 8 9 1 0 Date Top ic Distributed Database - Horizontal Fragmentation Distributed Database Vertical Fragmentation Distributed Database Replication Temporal Database Multimedia Database Active Database Object Oriented Database Spatial Database XML Database Prolong Programming P g. N Sign

I N D E X

A] D istributed Databa ses - V ertical Fragmentation


Data Base Link : SQL> Create database link utot connect to scott identified by tiger using 'pc12'; Database link created. SQL> Create database link ttou connect to scott identified by tiger using 'pc8'; Database link created. Table Structure : SQL> connect scott/tiger@p c8 Connected. SQL> create table Employee1 2 ( 3 eno number(5), 4 ename varchar2(10), 5 address varchar2(10) 6 ); Table created. SQL> desc Employee1 Name ----------------------------ENO ENAME ADDRESS SQL > con nect Enter username: scott Enter password: ***** Connected. SQL> create table Employee2 2 (

Null ? -------

Type --------------------------NUMBER(5) VARCHAR2(10) VARCHAR2(10)

3 eno number(5), 4 email varchar2(15), 5 salary number(10) 6 ); Tabl e crea ted.

SQL> desc Employee2 Name Null? Type -------------------------- ------------------------------------- ENO NUMBER(5) EMAIL VARCHAR2(15) SALARY NUMBER(5) Inserting records into the Employee1 table: SQL> connect scott/tiger@p c8 Connected. SQL> insert into Employee1 values(1,'Jinesh','Dahanu'); 1 row created. SQL> insert into Employee1 values(2,'Devendra','Umbergaon'); 1 row created. SQL> insert into Employee1 values(3,'Vanita','Mumbai'); 1 row created. SQL> insert into Employee1 values(4,'Suvarna','Mumbai'); 1 row created. SQL> insert into Employee1 values(5,'Neha','Dahanu'); 1 row created. SQL> insert into Employee1 values(6,'Priya','Baroda'); 1 row created. SQL> insert into Employee1 values(7,'Laxmi','Vangaon'); 1 row created. SQL> insert into Employee1 values(8,'Bhavika','Dahanu'); 1 row created. SQL> insert into Employee1 values(9,'Rachana','Umbergaon'); 1 row created. SQL> insert into Employee1 values(10,'Hiral','Bordi'); 1 row created. SQL> select * from Employee1; ENO ENAME ADDRESS ---------- -------------- ------------------1 Jinesh Dahanu 2 Devendr Umberga a on

Vanita

Mumbai

4 5 6 7 8 9 10 10 rows selecte d.

Suvarn a Neha Priya Laxmi Bhavik a Rachan a Hiral

Mumbai Dahanu Baroda Vangaon Dahanu Umberga on Bordi

Inserting records Employee2 table: SQL > con nect Enter username: scott Enter password: ***** Connected.

into

th e

SQL> insert into values(1,'jin@ yahoo.com',20000); 1 row crea ted. SQL> insert into values(2,'d [email protected]',20000); 1 row crea ted. SQL> insert into values(3,'[email protected] m',15000); 1 row crea ted. SQL> insert into values(4,'su v@ya hoo.co m',10000); 1

Employee2

Employee2

Employee2

Employee2

row crea ted. SQL> insert into values(5,'neha@ho tmail',9900); 1 row crea ted. SQL> insert into values(6,'p riya@ yahoo ',7000); 1 row crea ted. SQL> insert into values(7,'laxmi@ ya hoo',22000); 1 row crea ted. SQL> insert into values(8,'b hav@ ho tmail',25000); 1 row crea ted. SQL> insert into values(9,'rach@rediff',10500); 1 row crea ted. SQL> insert into values(10,'hiral@gmail',1900); 1 row crea ted. SQL> select * from Employee2; ENO EMAIL SALARY Employee2

Employee2

Employee2

Employee2

Employee2

Employee2

--------- -------------------------------1 jin@ yahoo. com 20000 2 dev@hotmail. com 20000 3 [email protected] 15000 4 su v@ yahoo.co m 10000 5 neha@hotmail 9900 6 priya@ yaho o 7 laxm i@ yahoo 22000 8 bhav@hotmail 9 rach@red iff 10500 10 hiral@gmail 10 rows selected.

7000 25000 1900

Q u eri es: SQL > con nect Enter username: scott Enter password: ***** Connected. 1. Find the salary of an Employee where employee number is known. SQL> select e1.eno, e2.salary 2 from Employee1@ttou e1, Employee2 e2 3 where e1.eno=1 and e1.eno = e2.eno; ENO SALAR Y ----------------------1

20 00 0 2. Find the Email where the employee name is known. SQL> select e1.ename,e2.email 2 from Emplo yee1@ttou e1,Employee2 e2 3 where e1.ename='Neha' and e1.eno=e2.eno; ENAME EMAIL --------------------------Neha neha@ho tmail 3.Find the employee name and Email where employee number is known. SQL> select e1.ename,e2.email 2 from Emplo yee1@ttou e1,Employee2 e2 3 where e1.eno=1 and e1.eno=e2.eno; ENAME EMAIL -----------------------

Jinesh

jin@ ya hoo.com

4.Find the employee name whose salary is > 10000 SQL> select e1.ename,e2.salary 2 from Employee1@ttou e1,Employee2 e2 3 where e1.eno = e2.eno and e2.salary>10000; ENAME -----------Jinesh Devendra Vanita Laxmi Bhavika Rachana SALAR Y --------------20000 20000 15000 22000 25000 10500

6 rows selected.

B] Distributed Data bases - Vertical Fragmentatio n Data Base Link : SQL> Create database link lk1 connect to scott identified by tiger using 'PC8'; Database link created. SQL> Create database link lk2 connect to scott identified by tiger using 'PC12'; Database link created. SQL> Create database link lk3 connect to scott identified by tiger using 'PC12'; Database link created. SQL> Create database link lk4 connect to scott identified by tiger using 'PC8'; Database link created. Table Structure: SQL> create table product_log 2 ( 3 pro_id varchar2(5), 4 pro_name varchar2(20), 5 pro_desc varchar2(50), 6 pro_cost number(10), 7 pro_profit number(10) 8 ); Table created. SQL> connect scott/tiger@PC8 ; Connected. SQL> create table product_m1 2 ( 3 pro_id varchar2(5), 4 pro_name varchar2(20), 5 pro_desc varchar2(50) 6 ); Table created. SQL> create table product_m2 2 ( 3 pro_id varchar2(5), 4 pro_cost number(10), 5 pro_profit number(10) 6 );

Table created.

Crea ting trigg er: SQL> connect scott/tiger; Connected. SQL> create or replace trigger product 2 after insert on product_log 3 for each row 4 begin 5 insert into produ ct_m1@lk1 values(:new.pro_id,:new.pro_name,:new.pro_desc); 6 insert into produ ct_m2@lk4 values(:new.pro_id,:new.pro_cost,:new.pro_profit); 7 end; 8 / Trigger created. Inserting records into the table product_log : SQL> insert into product_log values('1','Kitkat','Choc',10,2); 1 row created. SQL> insert into product_log values('2','Anand','Choc',10,2); 1 row created. SQL> insert into product_log values('3','Vasudhara','Choc',10,2); 1 row created. SQL> insert into product_log values('4','MilkBar','Choc',10,2); 1 row created. SQL> insert into product_log values('5','Gokul','Choc',10,3); 1 row created. SQL> insert into product_log values('6','Amul','Choc',15,4); 1 row created. SQL> insert into product_log values('7','DairyMilk','Choc',20,5); 1 row created. SQL> insert into product_log values('8','5Star','Choc',5,2); 1 row created. SQL> insert into product_log values('9','KissMiss','Choc',5,1); 1 row created. SQL> insert into product_log values('10','Perk','Choc',10,2); 1 row created.

SQL> select * from product_log; PRO_ID PRO_NAME PRO_DESC PRO_COST PRO_PROFIT ---------- --------------------- ----------------- ---------------- ---------------------1 Kitkat Choc 1 2 0 2 Anand Choc 10 2 3 Vasudhara Choc 1 2 0 4 MilkBar Choc 1 2 0 5 Gokul Choc 1 3 0 6 Amul Choc 1 4 5 7 DairyMilk Choc 2 5 05 8 5Star Choc 2 9 KissMiss Choc 5 1 10 Perk Choc 10 2 10 rows selected. Q u eri es: 1. Select the cost and profit for each product SQL> select a.pro_name,b.pro_cost,b.pro_profit 2 from product_m1@lk1 a, product_m2@lk4 b 3 where a.pro_id=b.pro_id and b.pro_id=a.pro_id; PRO_NAME PRO_COST PRO_PROFIT -------------------- ----------------- ---------Kitkat 10 2 Perk 10 2 Anand 10 2 Vasudhara 10 2 MilkBar 10 2 Gokul 10 3 Amul 15 4 DairyMilk 20 5 5Star 5 2 KissMiss 5 1 10 rows selected. 2. Display product name where profit is less than Rs.3 SQL> select a.pro_name,b.pro_profit 2 from product_m1@lk1 a,product_m2@lk4 b 3 where a.pro_id=b.pro_id and b.pro_id=a.pro_id and b.pro_profit<=3; PRO_NAME PRO_PROFIT ------------------- ---------Kitkat 2

Perk

Anand Vasudhara MilkBar Gokul 5Star KissMiss 8 rows selected.

2 2 2 3 2 1

3. Display product name and description where product cost is in between 10 and 15 SQL> select a.pro_name,a.pro_desc,b.pro_cost 2 from product_m1 @lk1 a,product_m2@lk4 b 3 where a.pro_id=b.pro_id and b.pro_id=a.pro_id and b.pro_cost>=10 and b.pro_cost<=15; PRO_NAME PRO_DESC PRO_COST ------------------ --------------------------------- Kitkat Choc 10 Perk Choc 10 Anand Choc 10 Vasudhara Choc 10 MilkBar Choc 10 Gokul Choc 10 Amul Choc 15 7 rows selected. 4. Display product name beginning with Am and profit is 10% of the product cost SQL> select a.pro_name,b.pro_cost,b.pro_profit 2 from product_m1@lk1 a, product_m2@lk4 b 3 where a.pro_id=b.pro_id and a.pro_name like 'Am%' and b.pro_profit>=0.1*b.pro_cost; PRO_NAME PRO_COST PRO_PROFIT -------------------- ----------------------------------- Amul 15 4 5. Display product name beginning with K and profit is 10% of the product cost SQL> select a.pro_name,b.pro_cost,b.pro_profit 2 from product_m1 @lk1 3 a, product_m2@l k4 b 4 where a.pro_id=b.pro_id and a.pro_name like 'K%' and b.pro_profit>=0.1*b.pro_cost;

PRO_NAME PRO_COST PRO_PROFIT ------------------ ------------------------------------- Kitkat 10 2 KissMiss 5 1

A] D istributed Databa ses - H orizontal Fragmentation


Data Base Link : SQL> Create database link utot connect to scott identified by tiger using 'pc12'; Database link created. SQL> Create database link ttou connect to scott identified by tiger using 'pc8'; Database link created. Table Structure: SQL> create table Employee12_15 2 ( 3 emp_no varchar2(5), 4 ename varchar2(25), 5 addr varchar2(50), 6 email varchar2(25), 7 sal number 8 ) 9 ; Table created. SQL> create table log_Employee12_15 2 ( 3 emp_no varchar2(5), 4 ename varchar2(25), 5 addr varchar2(50), 6 email varchar2(25), 7 sal number 8 ); Table created. SQL> connect scott/tiger@pc8 ; Connected. SQL> create table Employee8_se15 2 ( 3 emp_no varchar2(5), 4 ename varchar2(25), 5 addr varchar2(50), 6 email varchar2(25), 7 sal number 8 ); Table created.

SQL> describe Employee8_se15; Name Null? Type ---------------------------------- ----------------------------------- EMP_NO VARCHAR2(5) ENAME VARCHAR2(25) ADDR VARCHAR2(50) EMAIL VARCHAR2(25) SAL NUMBER SQL> connect Enter username: scott Enter password: ***** Connected. SQL> describe Employee12_15; Name Null? ------------------------------- ----------------------------------- EMP_NO VARCHAR2(5) ENAME VARCHAR2(25) ADDR VARCHAR2(50) EMAIL VARCHAR2(25) SAL NUMBER Type

SQL> describe log_Employee12_15; Name Null? Type ---------------------------------- ----------------------------------- EMP_NO VARCHAR2(5) ENAME VARCHAR2(25) ADDR VARCHAR2(50) EMAIL VARCHAR2(25) SAL NUMBER

Crea ting Trigg er: SQL> create or replace trigger TREmp12_15 2 after insert on Employee12_15 3 for each row 4 when(new.sal is not null) 5 begin 6 if :new.sal<10000 then 7 insert into log_Employee12_15 values(:new.emp_no,:new.ename,:new.addr, :new.email,:new.sal); 8 else

9 insert into Emp lo yee8 _se15 @ttou values(:new.emp_no, :new.ename, :new.addr, :new.email, :new.sal); 10 end if ; 11 end; 12 / Trigger created.

Inserting records in the ta bles: SQL> insert into Employee12_15 values('1','ABC','Dahanu','[email protected]',9999); 1 row created. SQL> insert into Employee12_15 values('2','XYZ','Umbergaon','d ev@ yahoo.com',15000); 1 row created. Q u eri es: SQL> select * from Employee12_15; EMP_NO ENAME ---------------- ---------------------------------------1 ABC 2 XYZ ADDR EMAIL ------------------ ------------------Dahanu Umbergaon [email protected] d ev@ yahoo.com SAL 9999 15000

SQL> select * from log_Employee12_15; EMP_NO ENAME ADDR EMAIL ---------------- ----------------- ------------------ -----------------------------------------1 ABC Dahanu [email protected] SQL> select * from E mplo yee8 _se15@ttou; EMP_NO ENAME ADDR EMAIL ---------------- ----------------- ------------------ -----------------------------------------2 XYZ Umbergaon d ev@ yahoo.com Q u eri es: 1.List the salaries of all the employees SQL> select ename,sal from Employee12_15; ENAME SAL ------------------------- ---------ABC 99 99 XYZ 150 00 OR SQL> select a.ename,a.sal from log_Employee12_15 a 2 union all 3 select b.ename,b.sal from Employee8_se15@ttou b; SAL 15000 SAL 9999

ENAME ------------------------ ---------- ABC 9999 XYZ

SAL

15000

2. Select the email of all employees where sal=15000 SQL> select a.ename,a.email from log_Employee12_15 a where a.sal=15000 2 union all 3 select b.ename,b.email from Emplo yee8_se15@ttou b where b.sal=15000; ENAME EMAIL ------------------------------------------------- XYZ d e v@ ya ho o . c o m 3.Find name and emp no where emp no is known SQL> select a.emp_no, a.ename from log_Employee12_15 a where a.emp_no='1' 2 union all 3 select b.emp_no, b.ename from Employee8_se15@ttou b where b.emp_no='1' ; EMP_N ENAME ----- ------------------------1 ABC 4.Find name and address where emp no is known SQL> select a.ename,a.addr from log_Employee12_15 a where a.emp_no='2' 2 union all 3 select b.ename,b.addr from Employee8_se15@ttou b where b.emp_no='2' ; ENAME ADDR ------------------------------------------ XYZ Umbergaon

B] Distributed Databases - Horizontal Fragmentation


Data b a se Link: SQL> create database link link1 connect to scott identified by tiger using 'pc12'; Database link created. SQL> create database link link2 connect to scott identified by tiger using 'pc12'; Database link created. Table Struct ure: SQL> create table customer_pdtls 2 ( 3 cust_id varchar2(10) primary key, 4 cust_name varchar2(20), 5 cust_addr varchar2(20) 6 ); Tabl e crea ted. SQL> connect scott/tiger@pc8 ; Connected. SQL> create table customer_bill 2 ( 3 cust_id varchar2(10), 4 cust_mobile number(10), 5 cust_billamt number(10) 6 ); Tabl e crea ted. SQL> create table customer_totbill 2 ( 3 cust_id varchar2(10), 4 cust_totalamt number(20) 5 ); Tabl

e crea ted. SQL > con nect Enter username: scott Enter password: ***** Connected.

Crea te Trigg er: SQL> create trigger entcustomer 2 after insert on customer_pdtls 3 for each row 4 begin 5 insert into customer_totbill@link2 values(:new.cust_id,0); 6 end; 7 / Trigger created. SQL> connect scott/tiger@pc8 ; Connected. SQL> create trigger customer_ent 2 after insert on customer_bill 3 for each row 4 when(new.cust_id is not null) 5 update customer_totbill 6 set cust_totalamt=cust_totalamt + :new.cust_billamt 7 where cust_id=:new.cust_id 8 / Trigger created. SQL> create trigger customer_upd 2 after update on customer_bill 3 for each row 4 when(old.cust_id is not null) 5 update customer_totbill 6 set cust_totalamt=cust_totalamt + :new.cust_billamt - :old.cust_billamt 7 / Trigger created. SQL> create trigger customer_del 2 after delete on customer_bill 3 for each row 4 when(old.cust_id is not null) 5 update customer_totbill 6 set cust_totalamt=cust_totalamt - :old.cust_billamt 7 where cust_id=:old.cust_id 8 / Trigger created. Inserting records into tables: SQL

> con nect Enter username: scott Enter password: ***** Connected.

SQL> insert into customer_pdtls values('1','Jinesh','Dahanu'); 1 row created. SQL> insert into customer_pdtls values('2','Dev','Umbergaon'); 1 row created. SQL> insert into customer_pdtls values('3','Vanita','Mumbai'); 1 row created. SQL> insert into customer_pdtls values('4','Suvarna','Mumbai'); 1 row created. SQL> insert into customer_pdtls values('5','Priya','Baroda'); 1 row created. SQL> select * from customer_pdtls; CUST_ID CUST_NAME CUST_ADDR ---------- -------------------- -------------------1 Jinesh Dahanu 2 D Umberga ev on 3 Vanita Mumbai 4 Suvarna Mumbai 5 Pri Baroda ya SQL> connect scott/tiger@pc8 ; Connected. SQL> insert into customer_bill values('1', 9890474542,1500); 1 row created. SQL> insert into customer_bill values('2', 9819107334,1220); 1 row created. SQL> insert into customer_bill values('1', 9867698801,1500); 1 row created. SQL> insert into customer_bill values('2', 9833525445,1220); 1 row created. SQL> insert into customer_bill values('3',9820112345,220); 1 row created. SQL> insert into customer_bill values('4',9819789023,2980); 1 row created.

SQL> insert into customer_bill values('5',9867723413,1000); 1 row created.

SQL> insert into customer_bill values('5',9867723414,1000); 1 row created. SQL> insert into customer_bill values('1',9867698802,1000); 1 row created. SQL> insert into customer_bill values('1',9867698711,800); 1 row created. SQL> select * from customer_bill; CUST_ID CUST_MOBILE CUST_BILLAMT -------------- -------------------- ------------------1 9890474542 1500 2 9819107334 1220 1 9867698801 1500 2 9833525445 1220 3 9820112345 220 4 9819789023 2980 5 9867723413 1000 5 9867723414 1000 1 9867698802 1000 1 9867698711 800 10 rows selected. SQL> select * from customer_totbill; CUST_ID CUST_TOTALAMT ---------- ------------1 48 00 2 24 40 3 2 2 4 29 80 5 20 00 Q u eri es: SQL > con nect Enter username: scott

Enter password: ***** Connected. 1. List out the customer names operating more than 2 mobiles. SQL> select cust_id,count(cust_id) "total no. of mobiles" cu stomer_b ill@ link2 2 having count(cust_id)>2 group by cust_id;

from

CUST_ID total no. of mobiles ---------- -------------------1 4 2. Display the customer name where the total bill is greater than 2000. SQL> select a.cust_name,b.cust_totalamt,c.cust_mobile 2 from customer_pdtls a,cu stomer_to tb ill@link2 b,customer_bill@link2 c 3 where a.cust_id=b.cust_id and b.cust_id=c.cust_id 4 and b.cust_totalamt>2000 5 ; CUST_NAME -------------------Jinesh Jinesh Jinesh Jinesh Dev Dev Suvarna 7 rows selected. 3. Display the total bill for all the customers. SQL> select a.cust_name,b.cust_totalamt from customer_pdtls a,customer_totbill@link2 b 2 where a.cust_id=b.cust_id; CUST_NAME CUST_TOTALAMT -------------------- ------------Jinesh 480 0 Dev 244 022 Vanita 0 Suvarna 29 80 Priya 200 0 4.Display the customer name who is with us for the last 4 months. SQL> select count(cust_id) from customer_b ill@link2 having count(cust_id)>=4 2 group by cust_id; COUNT(CUST_ID) -------------4 CUST_TOTALAM T ------------------------4800 4800 4800 4800 2440 2440 2980 CUST_MOBI LE ----------------9890474542 9867698801 9867698802 9867698711 9819107334 9833525445 9819789023

Distributed Database Replication


Data base Link: SQL> Create database link ln1 connect to scott identified by tiger using 'OEMREP'; Database link created. SQL> Create database link ln2 connect to scott identified by tiger using 'OEMREP'; Database link created. SQL> Create database link ln3 connect to scott identified by tiger using 'RUP'; Database link created. SQL> Create table emp19_rup 2 ( 3 Eno int, 4 Ename varchar2(50), 5 Address varchar2(50), 6 Email varchar2(25), 7 Salary int 8 ); Table created. SQL> connect scott/tiger@OEMRE P Connected. SQL> Create table emp19_oemrep 2 ( 3 Eno int, 4 Ename varchar2(50), 5 Address varchar2(50), 6 Email varchar2(25), 7 Salary int 8 ); Table created. Crea te Trigger: SQL> create or replace trigger emp19 2 after insert on emp19_rup 3 for each row 4 begin 5 insert into emp19_oemrep@ln1 values(:new.Eno,:new.Ename,:new.Address,:new.Email,:ne w.Salary); 6 end; 7 / Trigger created.

SQL> insert into emp19_rup values(1,'A','pqr','[email protected] m',5000); 1 row created.

SQL> insert into emp19_rup values(2,'B','abc','[email protected] m',5000); 1 row created. SQL> insert into emp19_rup values(3,'C','pqr','[email protected] m',7000); 1 row created. SQL> insert into emp19_rup values('4','D','pqr','ac@gma il.com',7000); 1 row created. SQL> insert into emp19_rup values('5','E','xyz','[email protected]',15000); 1 row created. Q u eri es: 1. Find the salary of all employees. SQL> select salary from emp19_rup; SALARY ---------5000 5000 7000 7000 15000 2. Find the email of all employees where salary = 15000. SQL> select Email from emp19_rup where salary=15000; EMAIL -----------------------a b @ ya ho o . co m 3.Find the employee name and email where employee number is known. SQL> select Ename,Email from emp19_oemrep@ln2 where Eno=2; ENAME EMAIL -------------------------------------------------------------------------- B ab @ya hoo.com 4. Find the employee name and address where employee number is known. SQL> select Ename, Address from emp19_oemrep@ln2 where Eno=4; ENAME ADDRESS ------------------------------------------------- D

pqr

7. A] O bject-Oriented Da tabases
Crea te different Types and Tables 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 or replace type publishertype as object( 2 name varchar2(50), 3 address AddrType1, 4 branches BranchTableType); 5 / Type created. SQL> Create or replace type AuthorListType19 as table of AuthorType; 2 / Type created. SQL> Create or replace type authors as table of AuthorType; 2 / Type created. SQL> Create or replace type publisher as table of publishertype;

2 / Type created. SQL> Create or replace type AuthorListType 2 / Type created. SQL> Create or replace type AuthorListType as varray(10) of ref AuthorType; 2 / Type created. SQL> create table book100( 2 title varchar2(50), 3 yea r dat e, 4 published_by ref PublisherType, 5 authors AuthorListType); Table created. Inserting records in the ta bles SQL> insert into authors values('abc',AddrType1(00001,'abc','mumbai','maharashtra',1234)); 1 row created. SQL> insert into authors values('pqr',AddrType1(00002,'lmn','jaipur','rajasthan',2564)); 1 row created. SQL> insert into authors values('A',AddrType1(00003,'w','pune','maharashtra',4568)); 1 row created. SQL> insert into authors values('xyz',AddrType1(00004,'ghi','mumbai','maharashtra',7895)); 1 row created. SQL> insert into authors values('pqr',AddrType1(00005,'ghh','mumbai','maharashtra',4646)); 1 row created. SQL> insert into authors values('klm',AddrType1(00006,'ffg','mumbai','maharashtra',4561)); 1 row created. SQL> select * from authors; NAME ADDR(PINCODE, STREET, CITY, STATE, NO)

------------------------------------------------------------------------------- Abc ADDRTYPE1(1, 'abc ', 'mumbai', 'maharashtra', 1234) Pqr ADDRTYPE1(2, 'lmn', 'jaipur', 'rajasthan', 2564) A ADDRTYPE1(3, 'w ', 'pune', 'maharashtra', 4568) Xyz ADDRTYPE1(4, 'ghi ', 'mumbai', 'maharashtra', 7895) Pqr ADDRTYPE1(5, 'ghh', 'mumbai', 'maharashtra', 4646) Klm ADDRTYPE1(6, 'ffg', 'mumbai', 'maharashtra', 4561) 6 rows selected. SQL> insert into publisher values('B',Addrtype1(00002,'lmn','jaipur','rajasthan',2654), 2 BranchTableType(BranchType(AddrType1(00002,'lmn','jaipur','rajasthan',2564),

3 0222451245,9865871245))); 1 row created. SQL> insert into publisher values('C',Addrtype1(00003,'w','pune','maharashtra',4568), 2 BranchTableType(BranchType(AddrType1(00003,'w','pune','maharashtra',4568), 3 022454787,9865741245))); 1 row created. SQL> insert into publisher values('D',Addrtype1(00004,'ghi','mumbai', 'maharashtra',7895), 2 BranchTableType(Branchtype(AddrType1(00004,'ghi','mumbai', 'maharashtra',7895),022454545,987655565))); 1 row created. 1 insert into publisher values('D',Addrtype1(00004,'ghi','mumbai', 'maharashtra',7895), 2 BranchTableType(Branchtype(AddrType1(00004,'ghi','mumbai', 'maharashtra',7895), 022454545,987655565))) 1 row created. SQL> insert into book100 select 'DWD','15-july-1982',ref(pub), AuthorListType(ref(auth)) 2 from publisher pub, Authors auth where pub.name='C' and auth.name='xyz'; 1 rows created. SQL> select * from book100; TITLE YEAR PUBLISHED_BY AUTHORS ------------------------------------------------------------------------------- DWD 15-JUL-82 00002202087C508D95B8E24B829055BC03CF799D1E9B898E6894124359AF3C0 FF82456 0974 AUTHORLISTTYPE(00002802092F1C363E8AEA483BA098910D3E07971C5593 7376199 A43B3B92D659 36592FCBD004145920003) DWD 15-JUL-82 00002202087C508D95B8E24B829055BC03CF799D1E9B898E6894124359AF3C0 FF82456 0974 AUTHORLISTTYPE(00002802095E1C56EC5FEC490E947CA754B256503B5593 7376199 A43B3B92D659 36592FCBD004145920009)

Q u eri es; 1.List all of the authors that have the same address as their publisher. SQL> select auth.name 2 from authors auth, publisher pub

3 where auth.addr=pub.address; NAME ----------------A A x y z x y z x y z x y z 6 rows selected. 2. List all of the authors that have the same pin code as their publisher. SQL> select auth.name from authors auth,publisher pub where auth.addr.pincode=pub.address.pincode; NAME ----------------pqr pqr A A x y z x y z x y

z x y z 8 rows selected. 3. List all books that have 2 or more authors. SQL> select a.title from book100 a 2 where 1<(select count(title) from book100 b where a.title=b.title); TITLE -----------------DWD DWD 4. List the title of the book that has the most authors. SQL> select title from book100 b,table(b.authors) group by title 2 having count(*) = 4 (select max(count(*)) 5 from book100 b, table(b.authors) 6 group by title); TITLE -------------------- DWD 5. List the name of the publisher that has the most branches. SQL> Select p.name 2 from publisher p, table(p.branches)

3 group by p.name having count(*)> = 4 all (select count(*) 5 from publisher p, table(p.branches) 6 group by name); NAME -------------------- D 6. Name of authors who have not published a book. SQL> select a.name 2 from authors a 3 where not exists( select b.title 4 from book100 b, table(b.authors) 5 where a.name = name); no rows selected 7. Move all the branches that belong to the publisher 'B' to the publisher D' SQL> insert into table( 2 select branches 3 from publisher 4 where name = 'B') 5 select b.* from publisher p, table(p.branches) b 6 where name = 'D'; 2 rows created. 8 List all authors who have published more than one book. SQL> select a.name 2 from authors a, book100 b, table(b.authors) v 3 where v.column_value = ref(a) 4 group by a.name having count(*) > 1; NAME ----------------------xyz 9. List all books (title) where the same author appears more than once on the list of authors SQL> select title 2 from authors a, book100 b, table(b.authors) v 3 where v.column_value = ref(a) 4 group by title having count(*) > 1; TITLE -

D W D B] Object Oriented Databases Crea te different Types and Tables SQL> create or replace type state61 as object
2 3 4 ( st_code number(5), 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 3 4 5 6 7 8 9 10 11 ( staff_id number(6), staff_name varchar2(40), staff_address address61, staff_deptno number(3), staff_sal number(6), staff_other varchar2(40), dob date, member function getAge return number )

12 / 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. Inserting records in the ta bles SQL> insert into dpt_refernce values(1,'Mumbai','Sales',
2 staffTableType(staff61(1,'Ankita Bhatt', 3 address61('A-1','L.T. road','Status Hotel', 4 state61(1,'Maharashtra','Mumbai',400092), 5 contact_detail61(28994177,28182729,' ankitabhatt@ yahoo. com', 28994177,9818967345)),1,10000,'HOD','17-aug-1984')));

1 row created. SQL> insert into dpt_refernce values(1,'Mumbai','Sales',


2 staffTableType(staff61(2,'Smita Mehta', 3 address61('C-1','D. N. road','', 4 state61(1,'Maharashtra','Mumbai',400069), 5 contact_detail61(26321285,26331739,'sweta. meht a@ rediffmail.com', 26321116,918967345)),1,6000,'HOD','04-nov-1984')));

1 row created. SQL> insert into dpt_refernce values(2,'Mumbai','Accounts',


2 staffTableType(staff61(3,'Seema Duggal ', 3 address61('C-1','M.G. road','Hinduja Hospital', 4 state61(1,'Maharashtra','Mumbai',400078), 5 contact_detail61(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',


2 staffTableType(staff61(4,'Anita Salunke', 3 address61('E-2','B.S.road','Vikas Kendra', 4 state61(2,'Goa','Panji',419832), 5 contact_detail61(26831112,26897058,'anitasakunde@gmail. com', 26897059,9820636448)), 3,7000,'','09-sep-1984')));

SQL> insert into dpt_refernce values(3,'Mumbai','Purchase',


2 3 4 5 staffTableType(staff61(5,'Pratik Dhuri ', address61('E-2','Nehru road','HDFC Bank', state61(1,'Maharashtra','ChunaBhatti',400056), contact_detail61(26149172,26157058,'pathik_ doshi@ yahoo.co.in',

26897059,9820739488)), 3,9000,'','29-sep-1985')));

1 row created. Q u eri es: 1. 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 ------------------- ---------Sales 1 Sales 2 Accounts 3 Purchase 4 Purchase 5 STAFF_ID

2. How many workers are in particular department. SQL> select p.dept_id,p.dept_name,count(q.staff_id) as number_of_employees
2 from dpt_refernce p,table(p.emp) q where p.dept_name='Purchase' 3 group by dept_id,dept_name; DEPT_ID DEPT_NAME NUMBER_OF_EMPLOYEES ---------- -------------------- ------------------3 Purchase 2

3. Find department name for particular staff name. SQL> select p.dept_id,p.dept_name from dpt_refernce p,
2 table(p.emp) q where q.staff_name='Ekta Bhatt' ;

no rows selected 4. Display department-wise report. SQL> select p.dept_id,p.dept_name,count(q.staff_id) as number_of_employees


2 3 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

5. Display age and birth date of particular employee. SQL> select q.dob,q.getAge() as Age from dpt_refernce p,
2 table(p.emp) q where q.staff_name='Ankita Bhatt' ; DOB AGE --------- ---------17-AUG-84 24

4.

A] Tempora l Databases
Table Structure: SQL> create table empant
2 3 4 5 6 7 ( acc_no number(10), aname varchar2(20), join_date date, retire_date date );

Table created. Inserting records in the ta bles SQL> insert into empant values(1, 'Batista', '22-mar-2001', '21-mar-2004'); 1 row created. SQL> insert into empant values(2, 'John Cena', '04-nov-2008', '04-nov-2058'); 1 row created. SQL> insert into empant values(4, 'Triple H', '22-mar-2001', '21-mar-2025'); 1 row created. SQL> insert into empant values(5, 'Booker T', '22-mar-2001', '21-mar-2054'); 1 row created. SQL> insert into empant values(6, 'Shawn Micheals', '22-mar-2001', '21-mar-2004'); 1 row created. SQL> insert into empant values(7, 'JBL', '22-mar-2000', '21-mar-2010'); 1 row created. SQL> insert into empant values(8, 'Ray Mysterio', '22-mar-2001', '21-mar-2004'); 1 row created. SQL> insert into empant values(9, 'Brook Lesner', '22-nov-2001', '21-mar-2024'); 1 row created. SQL> insert into empant values(10, 'CM Punk', '2-mar-2001', '21-mar-2020'); 1 row created. SQL> insert into empant values(3, 'Undertaker', '22-mar-2001', '14-May-2006'); 1 row created. Q u eri es: 1.Find the employees who have joining date 22-Mar-2001 SQL> select * from empant where join_date = '22Mar-2001' ; ACC_NO ANAME

JOIN_DATE RETIRE_DA

---------- -------------------- --------- --------1 Batista 22-MAR-01 21-MAR-04 4 Triple H 22-MAR-01 21-MAR-25 5 Booker T 22-MAR-01 21-MAR-54 6 Shawn Micheals 22-MAR-01 21-MAR-04 8 Ray Mysterio 22-MAR-01 21-MAR-04 9 Brook Lesner 22-MAR-01 21-MAR-24 3 Undertaker 22-MAR-01 14-MAY-06 7 rows selected. 2.Find the employees who have retirement date 14-May-2006 SQL> select * from empant where retire_date = '14-May-2006' ; ACC_NO ANAME JOIN_DATE RETIRE_DA ---------- -------------------- --------- --------3 Undertaker 22-MAR-01 14-MAY-06

B] Temporal Da tabases
Table Structure: SQL> Create table tbl_shares15 2 ( 3 cname varchar2(20), 4 nofshares number(5), 5 pricepshare number(5), 6 transtime timestamp(6) 7 ) 8 / Table created. Inserting records in the ta bles SQL> insert into tbl_shares15 values('Sail',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('Reliance,250,25,systimestamp); 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('Hero ',100,250,'21-aug-04 05.30.00.000000 pm'); 1 row created. SQL> insert into tbl_shares15 values('Google',310,30, '08-may-01 07.25.00.000000 am'); 1 row created.

SQL> select * from tbl_shares15; CNAME NOFSHARES PRICEPSHARE TRANSTIME ---------- ----------------- -------------------- ---------------------

Sail 250 Tata 205 Wipro 250 Relianc 250 e Patni 115 TCS 140 Hero 100 Google 310 8 rows selected.

2 5 2 0 2 5 2 5 1 5 1 2 250 3 0

07-MAR-09 03.12.30.390000 PM 05-JUN-04 11.45.00.000000 AM 10-MAR-03 06.15.00.000000 PM 07-MAR-09 03.17.37.500000 PM 08-MAY-01 07.25.00.000000 AM 14-APR-05 05.30.00.000000 PM 21-AUG-04 05.30.00.000000 PM 08-MAY-01 07.25.00.000000 AM

Q u eri es: 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' 4 / 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) from tbl_shares15 3 where to_char(transtime,'HH12:MI:AM')='05:30:PM') 4 / CNAME -----------------Hero

C] Tempora l Database
Table Structu re: C rea t i n g trigg er SQL> create or replace trigger trigemp_appnt 2 before insert or update 3 on empant 4 referencing new as new 5 for each row 6 7 /*declaring variables*/ 8 9 declare 10 nssn_old number(5); 11 vname_old varchar2(10); 12 drec_old date; 13 dret_old date; 14 nssn_new number(5); 15 vname_new varchar2(10); 16 drec_new date; 17 dret_new date; 18 drec_min date; 19 dret_min date; 20 drec_max date; 21 dret_max date; 22 23 /*procedure begins*/ 24 25 begin 26 27 /*assigning old values in the record*/ 28 29 nssn_old:=:old.acc_no; 30 vname_old:=:old.aname; 31 drec_old:=:old.join_date; 32 dret_old:=:old.retire_date; 33 34 /*assigning values passed by the user*/ 35 36 nssn_new:=:new.acc_no; 37 vname_new:=:new.aname; 38 drec_new:=:new.join_date; 39 dret_new:=:new.retire_date; 40 41 /*retirement date must not be less then recruitment date*/ 42

43 if drec_new>dret_new then 44 raise_application_error(-20001,'record cannot be added'); 45 end if; 46

47 /*on inserting values*/ 48 49 if inserting then 50 select max(retire_date) into dret_max from empant 51 where acc_no=nssn_new; 52 53 /* the new recruitment date must be greater to last retirement date of the employee */ 54 55 if drec_new<dret_max then 56 raise_application_error(-20001,'record cannot be added'); 57 end if; 58 59 update empant 60 set retire_date= drec_new-1 61 where join_date not in (select join_date from empant where 62 retire_date>join_date) and acc_no=nssn_new; 63 end if; 64 65 if updating then 66 if nssn_old!=nssn_new then 67 raise_application_error(-20002,'record cannot be updated'); 68 elsif vname_old!=vname_new then 69 raise_application_error(-20002,'record cannot be updated'); 70 elsif drec_old!=drec_new then 71 raise_application_error(-20002,'record cannot be updated'); 72 end if; 73 end if; 74 end; 75 / Trigger created.

Inserting records in the ta bles SQL> insert into empant values(109,'ekta','12-jan-2000', ' '); 1 row created. SQL> select * from empant; acc_no aname ---------------- ---------------101 sam 102 anne 103 smith 104 harry 105 jyo 107 nick 108 abc 109 ekta join_date ------------------- 22-mar-01 04-apr-99 01-dec-98 15-may-00 10-aug-95 22-mar-01 12-sep-00 12-jan-00 retire_date ------------------------21-mar-04 10-may-05 10-jun-02 14-may-06 09-aug-05 20-mar-05 14-may-06

8 rows selected.

D] Tempora l Database
Table Structure: SQL> create table emp_15 2 ( 3 emp_no varchar2(5), 4 ename varchar2(25), 5 addr varchar2(50), 6 email varchar2(50), 7 sal number 8 ); Table created. SQL> create table update_15 2 ( 3 emp_no varchar2(5), 4 old_sal number, 5 up_sal number(10), 6 e_date date, 7 primary key(emp_no,e_date) 8 ); Table created. Crea ting trigg ers SQL> create or replace trigger ins_sal15 2 after insert on emp_15 3 for each row 4* insert into update_15 values(:new.emp_no,null,:new.sal,sysdate) SQL> / Trigger created. SQL> create or replace trigger upd_sal15 2 after update on emp_15 3 for each row 4* insert into update_15 values(:new.emp_no,:old.sal,:new.sal,sysdate); Trigger created. Inserting records in the ta bles SQL> insert into emp_15 values 2* ('a01','ekta','borivali','e ks@red iffmail.co m',15000); 1 row created. SQL> insert into emp_15 values 2* ('a02','anita','andheri','anita [email protected]',8900); 1 row created.

SQL> insert into emp_15 values 2* ('a03','sharmila','borivali','[email protected] m',15000); 1 row created. SQL> insert into emp_15 values 2* ('a04','sweta','goregaon','sweta [email protected]',16000); 1 row created. SQL> insert into emp_15 values 2* ('a05','romil','parle','romil2u@ yaho o.com',12000); 1 row created. SQL> 1 insert into emp_15 values 2* ('a06','aarti','bandra','aartishah@ ya hoo.co.in',11500); 1 row created. SQL> insert into emp_15 values 2* ('a07','bikram','dahisar','[email protected]',10900); 1 row created. SQL> insert into emp_15 values 2* ('a08','vaibhav','matunga','vaibha v_7ir@yaho o.com',19000); 1 row created. SQL> insert into emp_15 values 2* ('a09','dharmesh','poisar','dhirajb hatt@ yaho o.co.in',7000); 1 row created. SQL> insert into emp_15 values 2* ('a10','madhu','santacruz','sweetmadhu @rediffmail.com',16500); 1 row created. SQL> select * from emp_15; Output: Emp_No Ename Addr Email ------------ --------------- --------------- ----------------------------------------------------- a01 ekta borivali [email protected] 15000 a02 ani andheri anitasaku [email protected] ta a03 sharmila borivali [email protected] m a04 sweta goregao swetakm@ yaho o.com n a05 ro parle romil2u @yahoo.com mil a06 aa bandra aartisha h@ya hoo.co.in rti a07 bikram dahisar [email protected] a08 vaibhav matung vaibha v_7ir@ yaho o.com a a09 dharmes poisar dhira jbhatt@yahoo .co.in h Sal

8900 1500 0 1600 0 120 00 115 00 109 00 190 00 700 0

a10

madhu

santacr uz

165 00

10 rows selected.

SQL> select * from update_15; O u t p u t : Emp_No Old_Sal Up_Sal E_Date ------------ ---------------------------------------a01 15000 28-JAN-06 a02 8900 28-JAN-06 a03 15000 28-JAN-06 a04 16000 28-JAN-06 a05 12000 28-JAN-06 a06 11500 28-JAN-06 a07 10900 28-JAN-06 a08 19000 28-JAN-06 a09 7000 28-JAN-06 a10 16500 28-JAN-06 10 rows selecte d. Updat ing table: SQL> update emp_15 2 set sal=20000 3* where emp_no='a01'; 1 row upd ated . SQL> update emp_15 2 set sal=22000 3* where emp_no='a01';

1 row upd ated . SQL> update emp_15 2 set sal=17500 3* where emp_no='a03'; 1 row upd ated . SQL> update emp_15 2 set sal=18000 3 * where emp_no='a03'; 1 row upd ated . SQL> update emp_15 2 set sal=19000 3* where emp_no='a03'; 1 row upd ated . SQL> update emp_15 2 set sal=9700 3* where emp_no='a02'; 1 row upd ated .

SQL> update emp_15 2 set sal=18100 3* where emp_no='a04'; 1 row updated. SQL> update emp_15 2 set sal=13000 3* where emp_no='a05'; 1 row updated. SQL> select * from update_15; Output: Emp_No Old_Sal ------------ -------------a01 a02 a03 a04 a05 a06 a07 a08 a09 a10 a01 15000 a01 20000 a03 15000 a03 17500 a03 18000 a02 8900 a04 16000 a05 12000 18 rows selected. Q u eri es 1.Display the latest salary of all the employees. SQL> select emp_no,up_sal from update_15 2 where up_sal in(select max(up_sal)"Latest Salary" 3* from update_15 group by emp_no); Emp_No Up_Sal ----------------------------- a09 7000 a02 9700 a07 10900 a06 11500 a05 13000 Up_Sal ----------- 1500 0 8900 1500 0 1600 0 1200 0 11500 1090 0 1900 0 7000 1650 0 20000 22000 17500 18000 19000 9700 18100 13000 E_Date --------------- 28-JAN06 28-JAN06 28-JAN06 28-JAN06 28-JAN06 28-JAN06 28-JAN06 28-JAN06 28-JAN06 28-JAN06 28-JAN06 28-JAN06 28-JAN06 28-JAN06 28-JAN06 28-JAN06 28-JAN06 28-JAN06

a10 a04 a08 a03 a01 10 rows selected.

165 00 181 00 190 00 190 00 220 00

2.Display employee name that has got more than 2 user events. SQL> select a.ename,b.emp_no,count(b.emp_no)"User Event" 2 from emp_15 a,update_15 b 3 where a.emp_no=b.emp_no group by b.emp_no,a.ename having 4* count(b.emp_no)>2; Ename Emp_N User Event ------------------- ---------------------------- ekta a01 3 sharmila a03 4 3.Display employee name that has got an increment of 5000 in one increment. SQL> select a.emp_no,b.ename from update_15 a,emp_15 b 2* where a.up_sal - a.old_sal>=5000 and a.emp_no=b.emp_no; Emp_N Ename ---------------------------- a01 ekta 3.Display employee name and salary of all the employees at second increment. SQL> select e1.ename"Ename", 2 e1.sal"salary", 3 e2.up_sal"Increment" 4 from emp_15 e1,(select emp_no, count(*) up_sal from update_15 5 group by emp_no) e2 where e1.emp_no = e2.emp_no 6* and up_sal=2; Ename salary Increment ---------------- ------------- --------------anita 9700 2 sweta 18100 2 romil 13000 2 4.Display employee name, total salary and total increment. SQL> select a.ename,b.emp_no,min(b.up_sal)"Initial Salary", 2 max(b.up_sal)"Latest Salary",max(b.up_sal)-min(b.up_sal)"total incr", 3 count(b.up_sal)-1 "Increments"

4 from emp_15 a,update_15 b where a.emp_no=b.emp_no 5* group by b.emp_no,a.ename;

ENAME EMP_N ------------- -----------ekta a01 anita a02 sharmila a03 sweta a04 romil a05 aarti a06 bikram a07 vaibhav a08 dharmes a09 h madhu a10 10 rows selected.

Initial Salary Latest Salary Total Incr Increments ------------------ ---------------- ---------------- ---------------15000 22000 7000 2 8900 9700 800 1 15000 19000 4000 3 16000 18100 2100 1 12000 13000 1000 1 11500 11500 0 0 10900 10900 0 0 19000 19000 0 0 7000 7000 0 0 16500 16500 0 0

8. A] A ctive Databases
Table Structure: SQL> create table Empl(
2 3 4 5 6 7 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),pname varchar2(50),thrs number(8),super_no nu mb er( 8)); Tab le cre ate d. Inserting records into tables SQL> insert into Empl values(1,'Prachi',7,10,101); 1 row created. SQL> insert into Empl values(2,'Amita',10,20,102); row created. SQL> insert into Empl values(3,'Sheetal',12,30,103); 1 row created. SQL> insert into Empl values(4,'Sulaxana',15,40,104); 1 row created. SQL> insert into Empl values(5,'Anagha',13,50,105); 1 row created. SQL> insert into Empl values(6,'Pooja',12,60,106); 1 row created. SQL> select * from Empl;
ENO ENAME HRS PNO SUPER_NO ---------- -------------------- ---------- ---------- ---------1 Prachi 7 10 10 1

2 Amita 3 Sheetal 4 Sulaxana 5 Anagha 6 Pooja

1 0 1 2 1 5 13 1 2

2 0 3 0 4 0 5 06 0

1 0 1 0 1 0 1 0 1 0

SQL> insert into Project values(10,'Sun',7,101); 1 row created.

SQL> insert into Project values


2 (20,'Moon',8,102);

1 row created. SQL> insert into Project values


2 (30,'Earth',9,103);

1 row created. SQL> insert into Project values


2 (40,'Jupiter',13,104);

1 row created. SQL> insert into Project values


2 (50,'Pluto',16,105);

1 row created. SQL> create or replace Trigger thrs


2 after insert on Empl 3 for each row 4 when(New.pno IS NOT NULL) 5 begin 6 update project 7 set thrs=thrs + :New.hrs 8 where pno=:New.pno; 9 end; 10 /

Trigger created. SQL> select * from Project;


PNO PNAME THRS SUPER_NO ---------- -------------------------------------------------- ---------10 Sun 7 1 0 20 Moon 8 10 21 30 Earth 9 0 40 Jupiter 1 10 3 4 50 Pluto 1 1 6 0

Creating Trigger SQL> create Trigger thrs1


2 after update of hrs on Empl 3 for each row 4 when(New.pno IS NOT NULL) 5 begin 6 update project 7 set thrs=thrs+:New.hrs-:Old.hrs 8 where pno=:New.pno; 9 end; 10 /

Trigger created. SQL> update Empl


2 set hrs =1 0 3 wher e eno= 5;

1 row updated. SQL> select * from Project;


PNO PNAME THRS SUPER_NO ---------- -------------------------------------------------- ---------10 Sun 7 1 0 20 Moon 8 1 0 30 Earth 9 1 0 40 Jupiter 1 10 41 50 Pluto 13 3 0 SQL> create Trigger thrs2 2 after update of pno on empl 3 for each row 4 when(New.pno IS NOT NULL) 5 begin 6 update project 7 set thrs=thrs+:New.hrs-:Old.hrs 8 where pno=:New.pno; 9 end; 10 /

Trigger created. SQL> update Empl


2 3 set pno=10 where eno=2;

1 row updated. SQL> update Empl


2 3 set pno=20 where eno=4;

1 row updated. SQL> select * from Project;


PNO PNAME THRS SUPER_NO ---------- -------------------------------------------------- ---------10 Sun 0 1 0 20 Moon 8 1 0 30 Earth 9 1 0

40 50

Jupiter Pluto

1 13 3

10 41 0

SQL> select * from Empl;


ENO ENAME HRS PNO SUPER_NO ---------- -------------------- ---------- ---------- ---------1 Prachi 7 10 101 2 Amita 10 1 10 0 2 3 Sheetal 12 3 10 02 31 4 Sulaxana 1 5 0 0 5 Anagha 1 5 1 0 0 0 6 12 60 10 Pooja 6

6 rows SQL> create trigger thrs4


2 3 4 5 6 7 8 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;

9 10 / Trigger created.

SQL> delete from empl where eno=1; 1 row deleted. SQL> select * from Empl;
ENO ENAME HRS PNO SUPER_NO ---------- -------------------- ---------- ---------- ---------2 Amita 10 10 10 3 Sheetal 12 30 10 2 31 4 Sulaxana 1 2 5 0 0 5 Anagha 1 5 1 0 0 0 6 Pooja 12 60 10 6

B ] Active Database:
Table Structure: SQL> create table stud1 (roll_no number(5),sname varchar2(50)); Table created. SQL> create table stud2(roll_no number(5),sname varchar2(50)); Table created. Crea te Triggers: SQL> create or replace trigger stud
2 3 4 5 6 7 / after insert on stud1 for each row begin insert into stud2 values(:new.roll_no,:new.sname); end;

Trigger created. Inserting records into tables SQL> insert into stud1 values(1,'Vikas'); 1 row created. SQL> insert into stud1 values(2,'Neeta'); 1 row created. SQL> insert into stud1 values(3,'Harshda'); 1 row created. SQL> select *
from stud1; ROLL_NO SNAME ---------- -------------------------------------------------1 Vikas 2 Neeta 3 Ha rsh da

SQL> select * from stud2;


ROLL_N O SNAME ---------- -------------------------------------------------1 Vi kas 2 Neet

a 3 Harshda

C] A ctive Database:
Table Structure: SQL> create table empd
2 3 4 5 6 7 ( dept_no varchar2(5), eno varchar2(5), ename varchar2(20), salary number(10) );

Table created. SQL> create table deptd


2 3 4 5 ( dept_no varchar2(5), total_sal number(10) );

Table created. Crea ting Trigg er SQL> create or replace trigger ins
2 3 4 5 6 7 8 / after insert on empd for each row when(new.dept_no is not null) update deptd set total_sal=total_sal+:new.salary where dept_no=:new.dept_no

Trigger created. SQL> create or replace trigger del


2 3 4 5 6 7 8 / after delete on empd for each row when(old.eno is not null) update deptd set total_sal=total_sal-:old.salary where dept_no=:old.dept_no

Trigger created. Inserting records into tables: SQL> insert into deptd values('a1',0); 1 row created. SQL> insert into deptd values('b1',0); 1 row created. SQL> insert into deptd values('c1',0); 1 row created. SQL> insert into empd values('a1','a01','Sujata',10000); 1 row created.

SQL> insert into empd values('a1','a02','shamal',8000); 1 row created. SQL> insert into empd values('b1','b01','shweta',5000); 1 row created. SQL> insert into empd values('b1','b02','anita',5000); 1 row created. SQL> select * from empd;
DEPT_ ENO ENAME SALARY ----- ----- -------------------- ---------a1 a Sujata 100 0 00 a1 a shamal 80 0 00 b b shweta 50 1 0 00 b b anita 500 1 0 0 SQL> select * from deptd; DEPT_ TOTAL_SAL ----- ---------a1 0 b1 0 c1 0

SQL> delete from empd where eno='b02'; 1 row deleted. SQL> select * from empd;
DEPT_ ENO ENAME SALARY ----- ----- -------------------- ---------a1 a Sujata 100 0 00 a1 a shamal 80 0 00 b b shweta 50 1 0 00

SQL> select * from deptd;


DEPT_ TOTAL_SAL ----- ---------a1 0 b1 50 000 c1

A] XML Da tabase:

Crea te different Types a nd Tables


SQL> create table emp_xml15( 2 dept_id number(4), 3* employee_spec XMLtype) SQL> / Table created. Insertion of different values in the tables 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.sha [email protected]</mgr_email> 7 <doj>12/12/2003</doj> 8* </e mp >')) SQ L> / 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.sha [email protected]</mgr_email> 7 <doj>2/6/2003</doj> 8* </e mp >')) SQ L> / 1 row created. SQL> insert into emp_xml15 values(1,XMLtype( 2 '<emp id="3"> 3 <name> ekta </name> 4 <email> ektabhatt@ yaho o.com</email> 5 <acc_no>2343456</acc_no>

6 <mgr_email>ekta.b hatt@ ho tmail.com</mgr_email> 7 <doj>24/5/2001</doj> 8* </e mp >')) SQ L> / 1 row created. SQL> insert into emp_xml15 values(1,XMLtype( 2 '<emp id="4"> 3 <name> nancy </name> 4 <email> nanc yshah@ yahoo .com</email> 5 <acc_no>2343678</acc_no>

6 <mgr_email>[email protected]</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@ ya hoo.com</email> 5 <acc_no>2343345</acc_no> 6 <mgr_email>falguni.sha h@ hotmail.co m</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> swetame hta@ ya hoo.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@ya hoo.com</email> 5 <acc_no>23433898</acc_no> 6 <mgr_email>[email protected] m</mgr_email> 7 <doj>4/9/2002</doj> 8* </em p>')) SQL >/ 1 row created. SQL> insert into emp_xml15 values(2,XMLtype( 2 '<emp id="8"> 3 <name> sandy </name> 4 <email>sa [email protected]</email> 5 <acc_no>23567898</acc_no> 6 <mgr_email>[email protected]</mgr_email> 7 <doj>4/4/2004</doj> 8* </em p>')) SQL >/

1 row created.

1.Retrieve the names of employee: SQL> select e.employee_spec.extract('//name/text()').getStringVal() "EMP_NAME" from emp_xml15 e; EMP_NAME ------------------------------sharmila a n i t a e k t a n a n c y f a l g u n i s w e t a a a r t i s a n d y 8 rows selected.

2. Retrieve the acc_no of employees: SQL>select e.employee_spec.extract('//acc_no/text()').getStringVal() 2* "Acc_No" from emp_xml15 e; Acc_No --------------------------------23456 234346 2343456 2343678 2343345 2343890 23433898 23567898 8 rows selected. 3. 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/ NAME ACC_NO EMAIL -------------------------------------------------------------------------------sharmila 23456 d a ve @ ya ho o . c o m anita 234346 ani@yaho o.co m ekta 2343456 ektab hatt@yahoo .com nancy 2343678 m falguni 2343345 om sweta 2343890 om aarti 23433898 m sandy 23567898 sagu pta@yaho o.com 8 rows selected.

4 Update the 3rd 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@ yaho o.com</email> 4 <acc_no>2343456</acc_no> 5 <mgr_email>ekta.b hatt@ ho tmail.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() 1 1 * = ' e k t a ' S Q L > / 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 ' SQL> / NAME ----------------------------------------------------------- EMP_SPECIFICATION ---------------------------------------------------------- ekta <emp id="3"> <name> ekta </name> <email>ektabhatt@ yahoo.com</email> <acc_no>2343456</acc_no> <mgr_email>[email protected]</mgr_email> <doj>24/5/2001</doj> <update>This is the updated record</update> </emp>

5. Delete the 4th record from the table: SQL> delete from emp_xml15 e 2 where e.employee_spec.extract('//name/text()').getStringVal() 3 * = 'n a n c y ' S Q L > / 1 row deleted. SQL> select e.employee_spec.extract('//name/text()').getStringVal() "NAME" from 2* emp_xml151 e S Q L > / N A M E -------------------------------------- sharmila a n i t a e k t a

f a l g u n i s w e t a a a r t i 6 rows selected.

9..

B ] XML Databa se
Tables Structure: SQL>Create table candidate( 2 cand_id varchar(3) primary key, 3* biodata xmltype) SQL> / Table created. Inserting record into tables SQL> insert into candidate 2 values('C01', 3 xmltype(' <cand> 4 <name>Ekta</name> 5 <address>Vile Parle</address> 6 <skill> 7 <compskill> 8 <lang>Java</lang> 9 <networkskill>Windows 2000 server</networkskill> 10 </compskill> 11 </skill> 12 <expr> 13 <programmer>5</programmer> 14 <projectmgr>5</projectmgr> 15 </expr> 16 <Objectives> To be an Enterpreneur</Objectives> 17 </cand>' 18* )); 1 row created. SQL> insert into candidate 2 values('C02', 3 xmltype(' <cand> 4 <name>Sweta</name> 5 <address>Dahisar</address> 6 <skill> 7 <compskill> 8 <lang>C</lang> 9 <networkskill>Windows NT</networkskill> 10 </compskill> 11 </skill> 12 <expr> 13 <programmer>10</programmer> 14 <projectmgr>3</projectmgr> 15 </expr>

16

<Objectives> To be a Project Manager</Objectives>

17 </cand>' 18* )); 1 row created. SQL> insert into candidate 2 values('C03', 3 xmltype(' <cand> 4 <name>Sharmila</name> 5 <address>Borivali</address> 6 <skill> 7 <compskill> 8 <lang>C</lang> 9 <networkskill>Windows XP</networkskill> 10 </compskill> 11 </skill> 12 <expr> 13 <programmer>10</programmer> 14 <projectmgr>5</projectmgr> 15 </expr> 16 <Objectives> To have my own software firm</Objectives> 17 </cand>' 18* )); 1 row created. SQL> insert into candidate 2 values('C04', 3 xmltype(' <cand> 4 <name>Anita</name> 5 <address>Andheri</address> 6 <skill> 7 <compskill> 8 <lang>C++</lang> 9 <networkskill>Windows 98</networkskill> 10 </compskill> 11 </skill> 12 <expr> 13 <programmer>7</programmer> 14 <projectmgr>2</projectmgr> 15 </expr> 16 <Objectives> To make my knowledge to better use</Objectives> 17 </cand>' 18* )); 1 row created. SQL> insert into candidate 2 values('C05', 3 xmltype(' <cand> 4 <name>Nancy</name> 5 <address>Bandra</address> 6 <skill> 7 <compskill>

8 <lang>VB</lang> 9 <networkskill>Windows XP</networkskill> 10 </compskill> 11 </skill> 12 <expr> 13 <programmer>5</programmer> 14 <projectmgr>1</projectmgr> 15 </expr> 16 <Objectives>To gain more knowledge</Objectives> 17 </cand>' 18* )); 1 row created. SQL> insert into candidate 2 values('C06', 3 xmltype(' <cand> 4 <name>Falguni</name> 5 <address>Vile Parle</address> 6 <skill> 7 <compskill> 8 <lang>Java</lang> 9 <networkskill>Windows 2000 Professional</networkskill> 10 </compskill> 11 </skill> 12 <expr> 13 <programmer>15</programmer> 14 <projectmgr>10</projectmgr> 15 </expr> 16 <Objectives>To design my own language</Objectives> 17 </cand>' 18* )); 1 row created. SQL> insert into candidate 2 values('C07', 3 xmltype(' <cand> 4 <name>Sandeep</name> 5 <address>Goregaon</address> 6 <skill> 7 <compskill> 8 <lang>SQl</lang> 9 <networkskill>Windows XP</networkskill> 10 </compskill> 11 </skill> 12 <expr> 13 <programmer>18</programmer> 14 <projectmgr>12</projectmgr> 15 </expr> 16 <Objectives>To earn upto my needs</Objectives> 17 </cand>'

18* )); 1 row created. SQL> insert into candidate 2 values('C08', 3 xmltype(' <cand> 4 <name>Gautam</name> 5 <address>Malad</address> 6 <skill> 7 <compskill> 8 <lang>Web designing</lang> 9 <networkskill>Windows NT</networkskill> 10 </compskill> 11 </skill> 12 <expr> 13 <programmer>20</programmer> 14 <projectmgr>15</projectmgr> 15 </expr> 16 <Objectives>Creative is my motive</Objectives> 17 </cand>' 18* )); 1 row created. SQL> insert into candidate 2 values('C09', 3 xmltype(' <cand> 4 <name>Akshay</name> 5 <address>Kandivali</address> 6 <skill> 7 <compskill> 8 <lang>VB</lang> 9 <networkskill>Windows 95</networkskill> 10 </compskill> 11 </skill> 12 <expr> 13 <programmer>12</programmer> 14 <projectmgr>8</projectmgr> 15 </expr> 16 <Objectives>To learn and master VB</Objectives> 17 </cand>' 18* )); 1 row created. SQL> insert into candidate 2 values('C10', 3 xmltype(' <cand> 4 <name>Rayo</name> 5 <address>Mumbai Central</address> 6 <skill> 7 <compskill>

8 <lang>Java</lang> 9 <networkskill>Windows XP</networkskill> 10 </compskill> 11 </skill> 12 <expr> 13 <programmer>21</programmer> 14 <projectmgr>15</projectmgr> 15 </expr> 16 <Objectives>To build a compiler some day in java</Objectives> 17 </cand>' 18* )); 1 row created. Q u eri es: 1. Select names of candidates who have programming experience greater than 5 and language skills in java: SQL> select cand_id "ID",c.biodata.extract('cand/name/text()') "Name" 2 from candidate c where 3 c.biodata.extract('//expr/programmer/text()').getStringVal() >5 and 4* c.biodata.extract('//skill/compskill/lang/text()').getStringVal() ='Java' SQL> / ID Name -----------------------------C06 Falguni C10 Rayo 2. Display Candidate having project manager level experience: SQL> select c.extract(biodata,'/cand/name/text()') 2 " Candidate Name" from candidate c 3* where c.biodata.existsNode('/cand/expr[projectmgr>1]')=1 SQL> / Candi date Name ------------------------------------------------------------------------------- Ekta S w e t a S h a

r m i l a A n i t a F a l g u n i S a n d e e p G a u t a m A k s h a y R a y o 9 rows selected. 3. Display Candidate Name and skills of all candidates: SQL> select c.extract(biodata,'cand/name').getStringVal() "Name",

2 c.extract(biodata,'/cand/skill/compskill/lang').getStringVal() "Lang Known",

3 c.extract(biodata,'/cand/skill/compskill/networkskill').getStringVal() "NetworkSkill" 4* from candidate c; Name ------------------------------------------------------------------------------- Lang Known ------------------------------------------------------------------------------- NetworkSkill -------------------------------------------------------------------------------<name>Ekta</name> <lang>Java</lang> <networkskill>Windows 2000 server</networkskill> <name>Sweta</name> <lang>C</lang> <networkskill>Windows NT</networkskill> Name ------------------------------------------------------------------------------- Lang Known ------------------------------------------------------------------------------- NetworkSkill -------------------------------------------------------------------------------<name>Sharmila</name> <lang>C</lang> <networkskill>Windows XP</networkskill> <name>Anita</name> <lang>C++</lang> Name ------------------------------------------------------------------------------- Lang Known ------------------------------------------------------------------------------- NetworkSkill -------------------------------------------------------------------------------<networkskill>Windows 98</networkskill> <name>Nancy</name> <lang>VB</lang> <networkskill>Windows XP</networkskill> <name>Falgu ni</name> Name ------------------------------------------------------------------------------- Lang Known ------------------------------------------------------------------------------- NetworkSkill

-------------------------------------------------------------------------------<lang>Java</lang> <networkskill>Windows 2000 Professional</networkskill>

<name>Sandeep</name> <lang>SQl</lang> <networkskill>Windows XP</networkskill> Name ------------------------------------------------------------------------------- Lang Known ------------------------------------------------------------------------------- NetworkSkill -------------------------------------------------------------------------------<name>Gautam</name> <lang>Web designing</lang> <networkskill>Windows NT</networkskill> <name>Akshay</name> <lang>VB</lang> <networkskill>Windows 95</networkskill> Name ------------------------------------------------------------------------------- Lang Known ------------------------------------------------------------------------------- NetworkSkill -------------------------------------------------------------------------------<name>Rayo</name> <lang>Java</lang> <networkskill>Windows XP</networkskill> 10 rows selected. 4. Delete record for address is Borivali: SQL> delete from candidate c 2* where c.biodata.extract('//address/text()').getStringVal()='Borivali'; 1 row deleted. SQL> select c.extract(biodata,'cand/name').getStringVal() "Name" 2* from candidate c S Q L > / O u t p u t

: Name -------------------------------------------------------------------------------<name>Ekta</name> <name>Sweta</name> <name>Anita</name> <name>Romil</name> <name>Mayank</name> <name>Sandeep</name> <name>Gautam</name>

<name>Akshay</name> <name>Rayo</name> 9 rows selected. e) Update Experience of Candidates where candidate-Id is C10: SQL> update candidate 2 set biodata=xmltype('<cand><expr><projectmgr> 100</projectmgr> </expr></cand>') 3* where cand_id='C0 1' SQL>/ 1 row updated. SQL> select e.biodata.getClobVal() "EMP_SPECIFICATION" 3 from candidate e where 4* e.cand_id='C0 1' SQL> / EMP_SPECIF ICATION <cand><expr><projectmgr>100</projectmgr></expr></cand>

8. A] Spatial Database
Table Structure: 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 );

1 row created. SQL> insert into university_15 values(


2 2, 3 'pqr', 4 mdsys.sdo_geometry( 5 2003, -- 2-dimensional polygon 6 null, 7 null, 8 mdsys.sdo_elem_info_array(1,1003,1), 9 -- one polygon (exterior polygon ring) 10 mdsys.sdo_ordinate_array(5,1, 8,1, 8,6, 5,7, 5,1) 11 );

1 row created. SQL> insert into university_15 values(


2 3, 3 'mno', 4 mdsys.sdo_geometry( 5 2003, -- 2-dimensional polygon 6 null, 7 null, 8 mdsys.sdo_elem_info_array(1,1003,1), 9 -- one polygon (exterior polygon ring) 10 mdsys.sdo_ordinate_array(3,3, 6,3, 6,5, 4,5, 3,3) 11 );

1 row created. SQL> insert into university_15 values(

2 3 4 5 6 7 8

4, 'xyz', mdsys.sdo_geometry( 2003, -- 2-dimensional polygon null,null,mdsys.sdo_elem_info_array(1,1003,4), -- one circle mdsys.sdo_ordinate_array(8,7, 10,9, 8,11) );

1 row created. SQL> insert into user_sdo_geom_metadata values (


2 3 4 5 6 7 8 9 'university_15', 'shape', mdsys.sdo_dim_array( mdsys.sdo_dim_element('x', 0, 20, 0.005), mdsys.sdo_dim_element('y', 0, 20, 0.005) ), null -- srid );

1 row created. Crea ting index : create index university_spatial_idx on university_15(shape) indextype is mdsys.spatial_index; Index created. Q u eri es: 1 .Find the topological intersection of two geometries. SQL> select sdo_geom.sdo_intersection (c_a.shape, c_c.shape, 0.005)
2 from university_15 c_a, university_15 c_c 3 where c_a.name = 'abc' and c_c.name = 'mno'; 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)
2 from university_15 c_b, university_15 c_d 3 where c_b.name = 'pqr' and c_d.name = 'xyz'; 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)


2 from university_15 c_b, university_15 c_a 3 where c_b.name = 'pqr' and c_a.name = 'abc';

SDO_GEOM.RELATE(C_B.SHAPE,'ANYINTERACT',C_A.SH APE,0.005)

------------------------------------------------------------------------------- TRUE

3.Find the areas of all direction locations. SQL> select name, sdo_geom.sdo_area(shape, 0.005) from university_15;
NAME SDO_GEOM.SDO_AREA(SHAPE,0.005) -------------------------------- -----------------------------abc 24 pqr 16.5 mno 5 xyz 12.56637 06

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
2 where c.name = 'abc'; 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) 2 from university_15 c_b, university_15 c_d 3 where c_b.name = 'pqr' and c_d.name = 'xyz'; SDO_GEOM.SDO_DISTANCE(C_B.SHAPE,C_D.SHAPE,0.005) -----------------------------------------------.846049894

B ] Spatial Database
Table Structure: SQL> create table university_11 (
2 3 4 mkt_id number primary key, name varchar2(32), shape_11 mdsys.sdo_geometry);

Table created. Inserting records ino table: SQL> insert into university_11 values(
2 1, 3 'main_gate', 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,5,2,8) 10 -- only 2 points needed to 11 -- define rectangle (lower left and upper right) with 12 -- cartesian-coordinate data 13 );

1 row created. SQL> insert into university_11 values(


2 2, 3 'canteen', 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(9,1, 11,3) 10 -- only 2 points needed to 11 -- define rectangle (lower left and upper right) with 12 -- cartesian-coordinate data 13 );

1 row created. SQL> insert into university_11 values(


2 3, 3 'arts_building', 4 mdsys.sdo_geometry( 5 2003, -- 2-dimensional polygon 6 null, 7 null, 8 mdsys.sdo_elem_info_array(1,1003,1), 9 --one polygon (exterior polygon ring) 10 mdsys.sdo_ordinate_array(4,2, 7,2, 8,3, 7,4, 4,4, 4,2) 11 );

1 row created.

SQL> insert into university_11 values(


2 4, 3 'lab', 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(8,6, 10,9) 10 -- only 2 points needed to 11 -- define rectangle (lower left and upper right) with 12 -- cartesian-coordinate data 13 );

1 row created. SQL> insert into university_11 values(


2 5, 3 'print_facility', 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(8,4, 10,7) 10 -- only 2 points needed to 11 -- define rectangle (lower left and upper right) with 12 -- cartesian-coordinate data 13 );

1 row created. SQL> insert into university_11 values(


2 6, 3 'science_building', 4 mdsys.sdo_geometry( 5 2003, -- 2-dimensional polygon 6 null, 7 null, 8 mdsys.sdo_elem_info_array(1,1003,1), 9 -- one polygon (exterior polygon ring) 10 mdsys.sdo_ordinate_array(8,10, 11,10, 11,13, 7,13, 8,11, 8,10) 11 );

1 row created. SQL> insert into university_11 values(


2 3 4 5 6 7 8 7, 'playground', mdsys.sdo_geometry( 2003, -- 2-dimensional polygon null,null,mdsys.sdo_elem_info_array(1,1003,4), -- one circle mdsys.sdo_ordinate_array(5,5, 7,7, 5,9) );

1 row created. SQL> insert into user_sdo_geom_metadata values (

2 3 4 5 6 7 8 9

'university_11', 'shape_11', mdsys.sdo_dim_array( mdsys.sdo_dim_element('x', 0, 20, 0.005), mdsys.sdo_dim_element('y', 0, 20, 0.005) ), null -- srid );

1 row created. Crea ting Index: SQL> create index university_spatial_idx11


2 on university_11(shape_11) 3 indextype is mdsys.spatial_index;

Index created. Q u eri es: 1.Find the distance between two geometries: 1.1 Distance between main gate and playground: SQL> select sdo_geom.sdo_distance(c_b.shape_11, c_d.shape_11, 0.005)
2 from university_11 c_b, university_11 c_d 3 where c_b.name = 'main_gate' and c_d.name = 'playground'; SDO_GEOM.SDO_DISTANCE(C_B.SHAPE_11,C_D.SHAPE_11,0.005) -----------------------------------------------------1

1.2. Distance between main gate and canteen: SQL> select sdo_geom.sdo_distance(c_b.shape_11, c_d.shape_11, 0.005)
2 from university_11 c_b, university_11 c_d 3 where c_b.name = 'main_gate' and c_d.name = 'canteen'; SDO_GEOM.SDO_DISTANCE(C_B.SHAPE_11,C_D.SHAPE_11,0.005) -----------------------------------------------------7 . 2 8 0 1 0 9 8 9

1.3. Distance between main gate and arts building:


SQL> select sdo_geom.sdo_distance(c_b.shape_11, c_d.shape_11, 0.005) 2 from university_11 c_b, university_11 c_d 3 where c_b.name = 'main_gate' and c_d.name = 'arts_building'; SDO_GEOM.SDO_DISTANCE(C_B.SHAPE_11,C_D.SHAP E_11,0.005) ------------------------------------------------------

2 . 2 3 6 0 6 7 9 8

1.4.Distance between main gate and science building:


SQL> select sdo_geom.sdo_distance(c_b.shape_11, c_d.shape_11, 0.005) 2 from university_11 c_b, university_11 c_d 3 where c_b.name = 'main_gate' and c_d.name = 'science_building'; SDO_GEOM.SDO_DISTANCE(C_B.SHAPE_11,C_D.SHAP E_11,0.005) -----------------------------------------------------6 . 3 2 4 5 5 5 3 2

1.5. Distance between main gate and lab:

SQL> select sdo_geom.sdo_distance(c_b.shape_11, c_d.shape_11, 0.005)


2 from university_11 c_b, university_11 c_d 3 where c_b.name = 'main_gate' and c_d.name = 'lab'; SDO_GEOM.SDO_DISTANCE(C_B.SHAPE_11,C_D.SHAPE_11,0.005) -----------------------------------------------------6

1.6. Distance between main gate and print facility: SQL> select sdo_geom.sdo_distance(c_b.shape_11, c_d.shape_11, 0.005)
2 from university_11 c_b, university_11 c_d 3 where c_b.name = 'main_gate' and c_d.name = 'print_facility'; SDO_GEOM.SDO_DISTANCE(C_B.SHAPE_11,C_D.SHAPE_11,0.005) -----------------------------------------------------6

1.7. Distance between college buildings and canteen. SQL> select sdo_geom.sdo_distance(c_b.shape_11, c_d.shape_11, 0.005)
2 from university_11 c_b, university_11 c_d 3 where c_b.name = 'arts_building' and c_d.name = 'canteen'; SDO_GEOM.SDO_DISTANCE(C_B.SHAPE_11,C_D.SHAPE_11,0.005) -----------------------------------------------------1

SQL> select sdo_geom.sdo_distance(c_b.shape_11, c_d.shape_11, 0.005)


2 from university_11 c_b, university_11 c_d 3 where c_b.name = 'science_building' and c_d.name = 'canteen'; SDO_GEOM.SDO_DISTANCE(C_B.SHAPE_11,C_D.SHAPE_11,0.005) -----------------------------------------------------7

1.8. Distance between science and arts building SQL> select sdo_geom.sdo_distance(c_b.shape_11, c_d.shape_11, 0.005)
2 from university_11 c_b, university_11 c_d 3 where c_b.name = 'arts_building' and c_d.name = 'science_building'; SDO_GEOM.SDO_DISTANCE(C_B.SHAPE_11,C_D.SHAPE_11,0.005) -----------------------------------------------------6 . 0 8 2 7 6 2 5 3

2.Find out the distance of the main gate from all other objects SQL> select sdo_geom.sdo_intersection(c_a.shape_11, c_c.shape_11, 0.005)
2 from university_11 c_a, university_11 c_c 3 where c_a.name = 'lab' and c_c.name = 'print_facility';

SDO_GEOM.SDO_INTERSECTION(C_A.SHAPE_11,C_C.SHAPE_11,0.005) (SDO_GTYPE,

SDO_SRID, -------------------------------------------------------------------------------SDO_GEOMETRY(2003, NULL, NULL, SDO_ELEM_INFO_ARRAY(1, 1003, 1), SDO_ORDINATE_ARR AY(8, 7, 8, 6, 10, 6, 10, 7, 8, 7))

3.Find the intersection area of lab and print facility 3.1.Spatial relationship between lab and print facility: SQL> select sdo_geom.relate(c_b.shape_11, 'anyinteract', c_d.shape_11, 0.005)
2 from university_11 c_b, university_11 c_d 3 where c_b.name = 'lab' and c_d.name = 'print_facility'; SDO_GEOM.RELATE(C_B.SHAPE_11,'ANYINTERACT',C_D.SHAPE_11,0.005) ------------------------------------------------------------------------------- TRUE

3.2.Spatial relationship between canteen and print_facility SQL> select sdo_geom.relate(c_b.shape_11, 'anyinteract', c_d.shape_11, 0.005)
2 from university_11 c_b, university_11 c_d 3 where c_b.name = 'canteen' and c_d.name = 'print_facility'; SDO_GEOM.RELATE(C_B.SHAPE_11,'ANYINTERACT',C_D.SHAPE_11,0.005) ------------------------------------------------------------------------------- FALSE

4.To find if any spatial relationship exists between playground and print_facility: SQL> select sdo_geom.relate(c_b.shape_11, 'anyinteract', c_d.shape_11, 0.005)
2 from university_11 c_b, university_11 c_d 3 where c_b.name = 'playground' and c_d.name = 'print_facility'; SDO_GEOM.RELATE(C_B.SHAPE_11,'ANYINTERACT',C_D.SHAPE_11,0.005) ------------------------------------------------------------------------------- FALSE

5) Return the areas of all different locations. SQL> select name, sdo_geom.sdo_area(shape_11, 0.005) from university_11;
NAME SDO_GEOM.SDO_AREA(SHAPE_11,0.005) ---------------------------------------------------------------- main_gate 3 canteen 4 arts_building 7 lab 6 print_facility 6 science_building 10 playground 12.5663706

7 rows selected.

6) Return the area of science_building:

SQL> select c.name, sdo_geom.sdo_area(c.shape_11, 0.005) from university_11 c


2 where c.name = 'science_building'; NAME SDO_GEOM.SDO_AREA(C.SHAPE_11,0.005) -----------------------------------------------------------------science_building 10

You might also like