+ All Categories
Home > Education > Adbm ssem1

Adbm ssem1

Date post: 23-Feb-2017
Category:
Upload: maheshraje23
View: 174 times
Download: 0 times
Share this document with a friend
51
M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System Practical 1: Horizontal Fragmentation of Database Steps for Creating a Database: Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar
Transcript
Page 1: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Practical 1: Horizontal Fragmentation of Database

Steps for Creating a Database:

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 2: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 3: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 4: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 5: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 6: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

AT CLIENT

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 7: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 8: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 9: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Query:SQL> conn as sysdba;Enter user-name: scottEnter password: *****Connected.

SQL> create public database link cs1 connect to scott identified by tiger using 'CS1';

SQL> create public database link cs2 connect to scott identified by tiger using 'CS2';Database link created.

In ‘CS1’ database create following two tables :create table emp(Eno number primary key,Ename varchar2(30),Address varchar2(50),Email varchar2(50),salary float);Table created.

Insert values into emp :insert into emp values(1,'Arun','Borivali','Arun.com',20000);insert into emp values(2,'Shreyas','Dahisar','Shreyas.com',15000);insert into emp values(3,'Vivek','Kandivali','vivek.com',10000);insert into emp values(4,'Sukanya','Goregaon','sukanya.com',12000);insert into emp values(5,'Ishan','Andheri','ishan.com',14000);insert into emp values(6,'Narmada','Goregaon','narmada.com',18000);insert into emp values(7,'Sneha','Dahisar','sneha.com',9000);insert into emp values(8,'Prasad','Borivali','prasad.com',8000);insert into emp values(9,'Kaushik','Borivali','kaushik.com',7500);insert into emp values(10,'Ravi','Goregaon','ravi.com',5000);

SQL> create table emp1 as select * from emp@orclink where Salary <=10000;Table createdIn ‘CS2’ database create following tableSQL> create table emp2 as select * from emp@orclink where Salary >10000 and salary<=20000;

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 10: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Table created.

Querries:-1) Find the salary of all employees.SQL> select salary from emp1 union select salary from emp2; SALARY---------- 5000 7500 8000 9000 10000 12000 14000 15000 18000 2000010 rows selected.

2) Find the email of all employees salary is>15000.SQL> select Email from emp2 where salary>15000;EMAIL--------------------------------------------------Arun.comnarmada.com

3) Find the employee name and email where employee number is known. SQL> select Ename,Email from emp1 where Eno=5 union all select Ename,Email from emp2 where Eno=5;ENAME------------------------------EMAIL--------------------------------------------------Ishanishan.com4 Find the employee name and address where employee number is known.SQL> select Ename ,Address from emp1 where Eno=5 union all select Ename,Address from emp2 where Eno=5;ENAME------------------------------ADDRESS--------------------------------------------------IshanAndheri

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 11: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Practical 2: Vertical Fragmentation of DatabaseQuery:SQL> conn as sysdba;Enter user-name: scottEnter password: *****Connected.

SQL> create public database link cs1 connect to scott identified by tiger using 'CS1';

SQL> create public database link cs2 connect to scott identified by tiger using 'CS2';

In ‘CS1’ database create following two tables:create table emp (Eno number primary key, Ename varchar2(30), Address varchar2(50), Email varchar2(50), salary float);

Insert values into emp:insert into emp values(1,'Arun','Borivali','Arun.com',20000);insert into emp values(2,'Shreyas','Dahisar','Shreyas.com',15000);insert into emp values(3,'Vivek','Kandivali','vivek.com',10000);insert into emp values(4,'Sukanya','Goregaon','sukanya.com',12000);insert into emp values(5,'Ishan','Andheri','ishan.com',14000);insert into emp values(6,'Narmada','Goregaon','narmada.com',18000);insert into emp values(7,'Sneha','Dahisar','sneha.com',9000);insert into emp values(8,'Prasad','Borivali','prasad.com',8000);insert into emp values(9,'Kaushik','Borivali','kaushik.com',7500);insert into emp values(10,'Ravi','Goregaon','ravi.com',5000);

IN CS1 database create table emp3:SQL> create table emp3 as select Eno,Ename,Address from emp@orlink;Table created.

IN CS2 create table emp4:SQL> create table emp4 as select Eno,Email,salary from emp@orclink;Table created.

Run Querries in CS1: 1) Find the salary of an employee where employee number is known.SQL> select salary from emp4 where Eno=4; SALARY---------- 120002) Find the email where the employee name is known.SQL> select Email from emp3,emp4 where emp3.Eno=emp4.Eno and Ename='Arun';EMAIL--------------------------------------------------Arun.com3) Find the employee name and email where employee number is known.

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 12: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

SQL> select Ename,Email from emp3,emp4 where emp3.Eno=emp4.Eno and emp3.Eno=7;ENAME------------------------------EMAIL--------------------------------------------------Sukanyasukanya.com

4) Find the employee name whose salary is>2000.SQL> select Ename from emp3,emp4 where emp3.Eno=emp4.Eno and salary >2000;ENAME------------------------------ArunShreyasVivekSukanyaIshanNarmadaSnehaPrasadKaushikRavi

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 13: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Practical 3: Creating Replica of DatabaseQuery:Creating Tables :-create table Emp( Eno number(3),Ename varchar2(20),Address varchar2(30),Email varchar(20),Salary number);

create table Emp( Eno number(3),Ename varchar2(20),Address varchar2(30),Email varchar(20),Salary number);

Creating Link :-connect scott/tiger@db1 as sysdba;Connected.create public database link replica connect to scott identified by tiger using 'db2'Database link created.

Creating Triggers :-create or replace Trigger insert_data after insert on Emp for each rowbegininsert into Emp@replicavalues(:new.Eno,:new.Ename,:new.Address,:new.Email,:new.Salary);end;Trigger created.

create or replace Trigger del_databefore delete on Empfor each rowbegindelete from Emp@replicawhere Eno=:old.Eno;end;Trigger created.

create or replace Trigger update_dataafter update on Empfor each rowbeginupdate Emp@replicaset Eno =:new.Eno,Ename =:new.Ename,Address = :new.Address,Email=:new.Email,Salary=:new.Salarywhere Eno=:old.Eno;end;

Trigger created.

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 14: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Inserting Values:-insert into Emp values(111,'Shweta','Goregaon','[email protected]',100000);insert into Emp values(112,'Sweha','Goregaon','[email protected]',10400);insert into Emp values(113,'Lata','Virar',’[email protected]',100000);insert into Emp values(114,'Sheejal','Goregaon','[email protected]',15000);insert into Emp values(115,'Shalmali','Jogeshwari','[email protected]',5000);insert into Emp values(116,'Meghna','Borivali','[email protected]',100000);insert into Emp values(117,'Swati','Andheri','[email protected]',15000);insert into Emp values(118,'Reena','Kandivali','[email protected]',300000);insert into Emp values(119,'Niyati','Malad','[email protected]',15000);insert into Emp values(120,'Ekta','Kandivali','[email protected]',20000);

1) Find the salary of all employees.SQL> select ename, salary from employ;ENAME SALARY---------- --------------------Shweta 100000Sweha 10400Lata 100000Sheejal 15000Shalmali' 5000Meghna 100000Swati 150000Reena 300000Niyati 15000Ekta 20000

2) Find the email of all employees where salary = 15000.SQL> select email from employ where salary=15000;[email protected]'[email protected]'[email protected]

3) Find the employee name and email where employee number is known.SQL> select ename,email from employ where eno=111;ENAME EMAIL---------- --------------------Shweta '[email protected]

4) Find the employee name and address where employee number is known.SQL> select ename,address from employ where eno=111ENAME ADDRESS---------- ---------------------Shweta Goregaon

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 15: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Practical 4: Create a Temporal DatabaseQuery:Create table:create table Emp_Appnt(Acc_No number(10),Name varchar2(10),RECDate date,RETDate date);

Inserting rows :insert into Emp_Appnt values(2025,'Prachi','12-feb-2005','12-oct-2011') ;insert into Emp_Appnt values(2211,'Prajakta','16-march-2008','16-sep-2010') ;insert into Emp_Appnt values(2221,'Neeta','18-june-2004','18-july-2006') ;insert into Emp_Appnt values(2221,'Neeta','18-june-2004','21-july-2008') ;insert into emp_appnt values(2000,'Meeta','16-oct-2003','16-sep-2010');

Queries:1) select * from emp_appnt where RECDate='18-june-2004';

output: ACC_NO NAME RECDATE RETDATE-------- ---------- --------- --------------------------- 2221 Neeta 18-JUN-04 18-JUL-06 2221 Neeta 18-JUN-04 21-JUL-08

2) select * from emp_appnt where RETDate='16-sep-2010';output : ACC_NO NAME RECDATE RETDATE-------- ---------- --------- --------- 2211 Prajakta 16-MAR-08 16-SEP-10 2000 Meeta 16-OCT-03 16-SEP-10 b)create table tbl_shares(C_Name varchar2(10),No_Share Number(10),Price number(10),TransTime varchar2(10)Default To_char(sysdate,'HH:MI'));

Inserting row:insert into tbl_shares values('Neeta',123,500,Default);insert into tbl_shares values('Meeta',121,810,Default); insert into tbl_shares values('Nivedita',233,600,Default);insert into tbl_shares values('Nivedi',203,650,Default);insert into tbl_shares values('Prasad',212,880,Default);

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 16: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

1) select * from tbl_shares where price>100 and TransTime='12:20';Output :C_NAME NO_SHARE PRICE TRANSTIME---------- ---------- ---------- ----------Neeta 123 500 12:20

2) select * from tbl_shares where price=(select max(price) from tbl_shares where TransTime='12:23')Output :C_NAME NO_SHARE PRICE TRANSTIME---------- ---------- ---------- ----------Prasad 212 880 12:23

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 17: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Practical 5: Implement Active Database Using PL/SQLQuery:Create a table emp (eno, ename, hrs, pno, super_no) and project (pname, pno, thrs, head_no) where thrs is the total hours and is the derived attribute. Its value is the sum of hrs of all employees working on that project. eno and pno are primary keys, head_no is foreign key to emp relation. Insert 10 tuples and write triggers to do the following:Create table:-create table Project_trigger(pname varchar2(10),pno number(5) primary key,thrs number(5),head_no number(5));table created.

create table Employee_trigger(eno number(5) primary key,ename varchar2(10),hrs number(5),super_no number(5),pno number(5));table created.

alter table Employee_triggeradd constraint et_1foreign key(pno)references Project_trigger(pno);

Queries Inserting rows:-a) Inserting into Project_trigger:-

insert into Project_trigger values('prj1',001,5,1);insert into Project_trigger values('prj2',002,10,2);insert into Project_trigger values('prj3',003,10,3);insert into Project_trigger values('prj4',004,8,4);insert into Project_trigger values('prj5',005,5,5);insert into Project_trigger values('prj5',006,7,6);insert into Project_trigger values('prj5',007,8,7);insert into Project_trigger values('prj5',008,10,8);insert into Project_trigger values('prj5',009,12,9);insert into Project_trigger values('prj5',010,15,10);

1) Creating a trigger to insert a new employee tuple and display the new total hours from project table.create or replace trigger empinsert after insert on Employee_trigger for each row when (new.pno is not NULL)update Project_triggerset thrs=thrs+:new.hrswhere pno=:new.pno

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 18: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

/b) Inserting into Employee_trigger:-

insert into Employee_trigger values(0001,'Anthony',5,2,001);insert into Employee_trigger values(0002,'Nishit',4,3,002);insert into Employee_trigger values(0003,'Amrit',6,4,004);insert into Employee_trigger values(0004,'Aakash',6,2,002) ;insert into Employee_trigger values(0005,'Swapnil',5,3,005);insert into Employee_trigger values(0006,'Vishal',10,5,003);insert into Employee_trigger values(0007,'Rahul',2,1,004);insert into Employee_trigger values(0008,'Nilesh',5,3,004);insert into Employee_trigger values(0009,'Priyanka',6,4,004);insert into Employee_trigger values(0010,'Shreya',4,3,004);

2) Creating a trigger to change the hrs of existing employee and display the new total hours from project table.create or replace trigger emphrsafter update of hrs on Employee_triggerfor each rowwhen(new.pno is not NULL)update Project_triggerset thrs=thrs+:new.hrs-:old.hrswhere pno=:new.pno/

Output:-Before Trigger :-SQL> select * from Employee_trigger; ENO ENAME HRS SUPER_NO PNO---------- ---------- ---------- --------------- ---------- 1 Anthony 5 2 1 2 Nishit 4 3 2 3 Amrit 6 4 4 4 Aakash 6 2 2 5 Swapnil 5 3 5 6 Vishal 10 5 3 7 Rahul 2 1 4 8 Nilesh 5 3 4 9 Priyanka 6 4 4 10 Shreya 4 3 4 11 Aakanksha 5 2 411 rows selected.

SQL> select * from Project_trigger;PNAME PNO THRS HEAD_NO---------- ---------- ---------- ----------prj1 1 5 1prj2 2 10 2prj3 3 10 3

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 19: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

prj4 4 13 4prj5 5 5 5prj5 6 7 6prj5 7 8 7prj5 8 10 8prj5 9 12 9prj5 10 15 1010 rows selected.

After trigger :-SQL> update Employee_trigger 2 set hrs=2 where eno=2;1 row updated.

SQL> select * from Employee_trigger; ENO ENAME HRS SUPER_NO PNO---------- ---------- ---------- ---------------- ---------- 1 Anthiny 5 2 1 2 Nishit 2 3 2 3 Amrit 6 4 4 4 Aakash 6 2 2 5 Swapnil 5 3 5 6 Vishal 10 5 3 7 Rahul 2 1 4 8 Nilesh 5 3 4 9 Priyanka 6 4 4 10 Shreya 4 3 4 11 Aakanksha 5 2 411 rows selected.

SQL> select * from Project_trigger;PNAME PNO THRS HEAD_NO---------- -------- ---------- ----------prj1 1 5 1prj2 2 8 2prj3 3 10 3prj4 4 13 4prj5 5 5 5prj5 6 7 6prj5 7 8 7prj5 8 10 8prj5 9 12 9prj5 10 15 1010 rows selected.

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 20: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

3) Creating a trigger to change the project of an employee and display the new total hours from project table.create or replace trigger empprojafter update on Employee_triggerfor each rowupdate Project_triggerset thrs= thrs - :old.hrswhere pno=:old.pno ;update Project_triggerset thrs=thrs + :new.hrswhere pno=:new.pno/

Output:-Before Trigger :-SQL> select * from Employee_trigger; ENO ENAME HRS SUPER_NO PNO---------- ---------- ---------- ---------------- ---------- 1 Anthiny 5 2 1 2 Nishit 2 3 2 3 Amrit 6 4 4 4 Aakash 6 2 2 5 Swapnil 5 3 5 6 Vishal 10 5 3 7 Rahul 2 1 4 8 Nilesh 5 3 4 9 Priyanka 6 4 4 10 Shreya 4 3 4 11 Aakanksha 5 2 411 rows selected.

SQL> select * from Project_trigger;PNAME PNO THRS HEAD_NO---------- ---------- ---------- ----------prj1 1 5 1prj2 2 8 2prj3 3 10 3prj4 4 13 4prj5 5 5 5prj5 6 7 6prj5 7 8 7prj5 8 10 8prj5 9 12 9prj5 10 15 1010 rows selected.

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 21: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

After Trigger:-SQL> Update Employee_trigger Set pno=2 where eno=7;1 row updated.

SQL> select * from Employee_trigger; ENO ENAME HRS SUPER_NO PNO---------- ------------ --------- --------------- ---------- 1 Anthony 5 2 1 2 Nishit 2 3 2 3 Amrit 6 4 4 4 Aakash 6 2 2 5 Swapnil 5 3 5 6 Vishal 10 5 3 7 Rahul 2 1 2 8 Nilesh 5 3 4 9 Priyanka 6 4 4 10 Shreya 4 3 4 11 Aakanksha 5 2 411 rows selected.

SQL> select * from Project_trigger;PNAME PNO THRS HEAD_NO---------- ------- ---------- ----------prj1 1 5 1prj2 2 10 2prj3 3 10 3prj4 4 11 4prj5 5 5 5prj5 6 7 6prj5 7 8 7prj5 8 10 8prj5 9 12 9prj5 10 15 1010 rows selected.

4) Creating a trigger to deleting the project of an employee.create or replace trigger delempafter update of pno on Employee_triggerfor each rowupdate Project_triggerset thrs=thrs-:old.hrswhere pno=:old.pno/

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 22: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Output:-Before Trigger :-SQL> select * from Employee_trigger; ENO ENAME HRS SUPER_NO PNO---------- ---------- -------- --------------- ---------------- 1 Anthiny 5 2 1 2 Nishit 2 3 2 3 Amrit 6 4 4 4 Aakash 6 2 2 5 Swapnil 5 3 5 6 Vishal 10 5 3 7 Rahul 2 1 2 8 Nilesh 5 3 4 9 Priyanka 6 4 4 10 Shreya 4 3 4 11 Aakanksha 5 2 411 rows selected.

SQL> select * from Project_trigger;PNAME PNO THRS HEAD_NO---------- ---------- ---------- ---------------prj1 1 5 1prj2 2 10 2prj3 3 10 3prj4 4 11 4prj5 5 5 5prj5 6 7 6prj5 7 8 7prj5 8 10 8prj5 9 12 9prj5 10 15 1010 rows selected.

After Trigger :-SQL> update Employee_trigger set pno=NULL where eno=2;1 row updated.SQL> select * from Employee_trigger;

ENO ENAME HRS SUPER_NO PNO---------- ---------- ---------- ---------------- ---------- 1 Anthiny 5 2 1 2 Nishit 2 3 3 Amrit 6 4 4 4 Aakash 6 2 2 5 Swapnil 5 3 5 6 Vishal 10 5 3 7 Rahul 2 1 2

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 23: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

8 Nilesh 5 3 4 9 Priyanka 6 4 4 10 Shreya 4 3 4 11 Aakanksha 5 2 411 rows selected.

SQL> select * from Project_trigger;PNAME PNO THRS HEAD_NO---------- ---------- ---------- ----------prj1 1 5 1prj2 2 6 2prj3 3 10 3prj4 4 11 4prj5 5 5 5prj5 6 7 6prj5 7 8 7prj5 8 10 8prj5 9 12 9prj5 10 15 1010 rows selected.

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 24: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Practical 6: Implement Object Oriented Database Using PL/SQLQuery:Using Object Oriented databases create the following types:a) AddrType1 (PinQuery: number, Street :char, City : char, state :char)b) (ii)BranchType (address: AddrType1, phone1: integer,phone2: integer )c) AuthorType (name:char,,addr AddrType1)d) PublisherType (name: char, addr: AddrType1, branches: BranchTableTypee) AuthorListType as varray, which is a reference to AuthorTypeNext create the following tables:f) BranchTableType of BranchTypeg) authors of AuthorTypeh) books(title: varchar, year : date,published_by ref PublisherType,authors AuthorListType)i) Publishers of PublisherTypeInsert 10 records into the above tables and fire the following queries:a) List all of the authors that have the same pin Query as their publisher:b) List all books that have 2 or more authors:c) List the name of the publisher that has the most branchesd) Name of authors who have not published a booke) List all authors who have published more than one book:f) Name of authors who have published books with at least two different publishersg) 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).

Creating Types and TablesCREATING TYPE AddrType1Create or replace type AddrType1 as object (PinQuery number (5), Street char (20), City varchar2(50), state varchar2(40), no number(4) );Type created.

CREATING TYPE BranchTyoecreate or replace type BranchType as object (address AddrType1, phone1 integer,phone2 integer );Type created.

CREATING TYPE BracnhTableTypecreate or replace type BranchTableType as table of BranchType;Type created.

CREATING TYPE AuthorTypecreate or replace type AuthorType as object (name varchar2 (50), addr AddrType1);Type created.

CREATING TABLE Authorscreate table Authors of AuthorType;Table created.

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 25: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

CREATING TABLE AuthorListTypecreate or replace type AuthorListType as varray(10) of ref AuthorTypeType created.

CREATING TYPE PublisherTpyecreate or replace type PublisherType as object(name varchar2(50), addr AddrType1, branches BranchTableType)Type created.

CREATING TABLE Publisherscreate table Publishers of PublisherType NESTED TABLE branches STORE as branchtable;Table created.

CREATING TABLE bookscreate table books(title varchar2(50), year date, published_by ref PublisherType,authors AuthorListType);Table created.

Inserting records into the table authors:insert into Authors values('Vigil', AddrType1(7000,'AT street', 'mumbai','maharashtra',1007));insert into Authors values('Rohan', AddrType1(7007,'VT street', 'mumbai','maharashtra',1006));insert into Authors values('Ninad',AddrType1(7008,'TT street', 'Nasik',2 'maharashtra',1008));insert into Authors values('Ameya',AddrType1(7003,'PL street', 'mumbai','maharashtra',1003));insert into Authors values('Vigil',AddrType1(7008,'AT street', 'mumbai', 'maharashtra',1007));insert into Authors values ('DonBox', AddrType1 (7006,'Nehrut','mumbai','maharashtra',1005));insert into Authors values ('Haseeb', AddrType1(8002,'TH street','pune','maharashtra',13));insert into Authors values('Richard',AddrType1(7002,'FL street','pune',2 'maharashtra',03));

Inserting records into the table publishers:insert into Publishers values ('Vipul', AddrType1 (4002,'PK street', 'mumbai','maharashtra',03), BranchTableType(BranchType (AddrType1(5002,'PL street', 'mumbai', 'maharashtra', 03), 23406,69896)));1 row created.

insert into Publishers values('McGraw',AddrType1(7007,'LJstreet','mumbai' ,'maharashtra',07), BranchTableType (BranchType ( AddrType1 (7007,'K street','mumbai', 'maharashtra',1007), 4543545,8676775)));1 row created.

insert into Publishers values ('Tata',AddrType1(7008,'JW street','mumbai', 'maharashtra',27), BranchTableType (BranchType (AddrType1(1002,'DM street','nasik', 'maharashtra',1007), 456767,7675757)));1 row created.

insert into Publishers values ('Nurali', AddrType1(7002,'ST street','pune','maharashtra',1007), BranchTableType (BranchType (AddrType1(1002,'SG street','pune', 'maharashtra',1007), 4543545,8676775)));

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 26: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

1 row created.insert into Publishers values('Tata', AddrType1(6002,'Gold street','nasik', 'maharashtra',1007), BranchTableType(BranchType(AddrType1(6002,'South street', 'nasik','mha',1007), 4543545,8676775)));

Inserting records into the table booksinsert into books select 'IP','28-may-1983', ref (pub), AuthorListType(ref(aut)) from Publishers pub,Authors aut where pub.name='Tata' and aut.name='Richard';2 rows created.

insert into books select 'ADBMS','09-jan-1890',ref(pub), AuthorListType(ref(aut)) from Publishers pub,Authors aut where pub.name='McGraw' and aut.name='Amol';2 rows created.

insert into books select 'c prog','25-may-1983', ref (pub),AuthorListType(ref(aut)) from Publishers pub,Authors aut where pub.name='Vipul' and aut.name='Haseeb';1 row created.

Firing Queries on the tables.1) List all of the authors that have the same pin Query as their publisher:

Query:select a.name from Authors a, Publishers pwhere a.addr.pinQuery = p.addr.pinQuery;Output:NAME--------------------------------------------------RichardRohanNinadVigil

2) List all books that have 2 or more authorsQuery:Select title from books b where 2 <= (select count(*) from table(b.authors));Output:TITLE------------c prog

3) List the name of the publisher that has the most branchesQuery: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);Output:NAME--------------------------------------------------Tata

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 27: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

4) Name of authors who have not published a bookQuery:select a.name from authors a where not exists(select b.title from books b,table(select authorsfrom books b1 where b.title=b1.title)where a.name=name);Output:NAME--------------------------------------------------Rohan

5) List all authors who have published more than one bookQuery:select a.name from authors a, books b, table (b.authors) vwhere v.column_value = ref(a) group by a.name having count(*) > 1;OutputNAME--------------------------------------------------VigilHaseebRichard

6) Name of authors who have published books with at least two different publishersQuery:select a.name from authors a, books b, table (b.authors) vwhere v.column_value = ref(a) group by a;Output:NAME--------------------------------------------------VigilHaseebRichard

7) 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).Query:select title from authors a, books b, table (b.authors) vwhere v.column_value = ref(a) group by title having count(*) > 1;Output:TITLE--------------------------------------------------ADBMSIPc prog

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 28: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Practical 7: Implement & Retrieve Record from Spatial DatabaseQuery:Create a spatial database table that stores the number, name and location, which consists of four different areas say abc, pqr, mno and xyz. Fire the following queries:a)Find the topological intersection of two geometries.b)Find whether two geometric figures are equivalent to each other.c)Find the areas of all different locations.d)Find the area of only one location.e) Find the distance between two geometries.

Query for Creating Tablecreate table cola_mrp(mkt_id number primary key,name varchar(20),shape MDSYS.SDO_Geometry);

Queries for inserting rows :1)insert into cola_mrp values(1,'cola_a',MDSYS.SDO_GEOMETRY(2003,NULL,NULL,MDSYS.SDO_ELEM_INFO_ARRAY(1,1003,3),MDSYS.SDO_ORDINATE_ARRAY(1,1,5,7)))/

2)insert into cola_mrp values(2,'cola_b',MDSYS.SDO_GEOMETRY(2003,NULL,NULL,MDSYS.SDO_ELEM_INFO_ARRAY(1,1003,1),MDSYS.SDO_ORDINATE_ARRAY(5,1,8,1,8,6,5,7,5,1)))/

3)insert into cola_mrp values(3,'cola_c',MDSYS.SDO_GEOMETRY(2003,NULL,NULL,MDSYS.SDO_ELEM_INFO_ARRAY(1,1003,1),MDSYS.SDO_ORDINATE_ARRAY(3,3,6,3,6,5,4,5,3,3)))/

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 29: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

4)insert into cola_mrp values(4,'cola_d',MDSYS.SDO_GEOMETRY(2003,NULL,NULL,MDSYS.SDO_ELEM_INFO_ARRAY(1,1003,4),MDSYS.SDO_ORDINATE_ARRAY(7,9,10,9,8,11)))/

Creating Metadata information:insert into user_SDO_GEOM_METADATA values('cola_mrp','shape',MDSYS.SDO_DIM_ARRAY(MDSYS.SDO_DIM_ELEMENT('X',0,20,0.005),MDSYS.SDO_DIM_ELEMENT('Y',0,20,0.005)),NULL);

Query for creating index :create index cola_spatial_idxon cola_market(location)Indextype Is mdsys.spatial_index;

Queries :1) Find the topological intersection of two geometries.select SDO_GEOM.SDO_INTERSECTION (c_a.shape,c_c.shape,0.005)from cola_mrp c_a,cola_mrp c_cwhere c_a.name='cola_a' AND c_c.name='cola_c';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_ARRAY(4, 5, 3, 3, 5, 3, 5, 5, 4, 5))

2) Find whether two geometric figures are equivalent to each other.SELECT SDO_GEOM.RELATE(c_c.shape, 'EQUAL', c_a.shape,0.005)FROM cola_mrp c_c, cola_mrp c_aWHERE c_c.name='cola_c' AND c_a.name = 'cola_a';Output :-SDO_GEOM.RELATE(C_C.SHAPE,'EQUAL',C_A.SHAPE,0.005)--------------------------------------------------------------------------------FALSE

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 30: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

3) Find the areas of all different locationsselect name,SDO_GEOM.SDO_AREA(shape,0.005) from cola_mrp;Output :-NAME SDO_GEOM.SDO_AREA(SHAPE,0.005)-------------------- ------------------------------cola_a 24cola_b 16.5cola_c 5cola_d 7.85398163

4) Find the area of only one location.select c.name,SDO_GEOM.SDO_AREA(c.shape,0.005) from cola_mrp cwhere c.name='cola_a';Output :NAME SDO_GEOM.SDO_AREA(C.SHAPE,0.005)-------------------- --------------------------------cola_a 24

5) Find the distance between two geometries.select SDO_GEOM.SDO_DISTANCE(c_b.shape,c_d.shape,0.005)from cola_mrp c_b,cola_mrp c_dwhere c_b.name= 'cola_b' AND c_d.name ='cola_d';Output :-SDO_GEOM.SDO_DISTANCE(C_B.SHAPE,C_D.SHAPE,0.005)------------------------------------------------1.8973666

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 31: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

Practical 8: Create XML Application Using DatabaseQuery:Create a table employee having dept_id as number datatype and employee_spec as XML datatype (XMLType).The employee_spec is a schema with attributes emp id, name, email, acc_no, managerEmail, dateOf Joning. Insert 10 tuples into employee table. Fire the following queries on XML database.a) Retrieve the names of employee.b) Retrieve the acc_no of employees.c) Retrieve the names,acc_no, email of employees.d)Update the 3rd record from the table and display the name of an employee.e) Delete 4 th record from the table.

Creating Employee table:SQL> CREATE TABLE employee (Dept_id number(5),emp_specification XMLTYPE);Table created.

Inserting data for XML:insert into employee values(1,XMLTYPE('<emp><e_id>1</e_id><ename>Priya</ename><email>[email protected]</email><acc_no>101</acc_no><mngr_email>[email protected]</mngr_email><doj>22 jan 2011</doj></emp>'));

insert into employee values(2,XMLTYPE('<emp><e_id>2</e_id><ename>mohini</ename><email>[email protected]</email><acc_no>102</acc_no><mngr_email>[email protected]</mngr_email><doj>22 feb 2011</doj></emp>'));

insert into employee values(3,XMLTYPE('<emp><e_id>3</e_id><ename>Pornima</ename><email>[email protected]</email><acc_no>103</acc_no><mngr_email>[email protected]</mngr_email><doj>22 mar 2011</doj></emp>'));

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 32: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

insert into employee values(4,XMLTYPE('<emp><e_id>4</e_id><ename>shreya</ename><email>[email protected]</email><acc_no>104</acc_no><mngr_email>[email protected]</mngr_email><doj>22 april 2011</doj></emp>'));

insert into employee values(5,XMLTYPE('<emp><e_id>5</e_id><ename>ketal</ename><email>[email protected]</email><acc_no>105</acc_no><mngr_email>[email protected]</mngr_email><doj>22 may 2011</doj></emp>'));

insert into employee values(6,XMLTYPE('<emp><e_id>6</e_id><ename>aakanksha</ename><email>[email protected]</email><acc_no>106</acc_no><mngr_email>[email protected]</mngr_email><doj>22 june 2011</doj></emp>'));

insert into employee values(7,XMLTYPE('<emp><e_id>7</e_id><ename>aakash</ename><email>[email protected]</email><acc_no>107</acc_no><mngr_email>[email protected]</mngr_email><doj>22 july 2011</doj></emp>'));

insert into employee values(8,XMLTYPE('<emp><e_id>8</e_id><ename>nishit</ename><email>[email protected]</email><acc_no>108</acc_no><mngr_email>[email protected]</mngr_email>

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 33: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

<doj>22 aug 2011</doj></emp>'));insert into employee values(9,XMLTYPE('<emp><e_id>9</e_id><ename>swapnil</ename><email>[email protected]</email><acc_no>109</acc_no><mngr_email>[email protected]</mngr_email><doj>22 sept 2011</doj></emp>'));

insert into employee values(10,XMLTYPE('<emp><e_id>10</e_id><ename>anthony</ename><acc_no>110</acc_no><email>[email protected]</email><mngr_email>[email protected]</mngr_email><doj>22 oct 2011</doj></emp>'));

QUERIES:1) Retrieve the names of employee:Select e.emp_specification.EXTRACT('/emp/ename/text()').getStringVal() from employee e;Output:-E.EMP_SPECIFICATION.EXTRACT('/EMP/ENAME/TEXT()').GETSTRINGVAL()--------------------------------------------------------------------------------PriyaPornimashreyaketalaakankshaaakashnishitswapnilanthonymohini10 rows selected.

2) Retrieve the acc_no of employees:Select e.emp_specification.EXTRACT('/emp/acc_no/text()').getStringVal() from employee e;Output:-E.EMP_SPECIFICATION.EXTRACT('/EMP/ACC_NO/TEXT()').GETSTRINGVAL()--------------------------------------------------------------------------------101103104105

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 34: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

10610710810911010210 rows selected.

3) Retrieve the names, acc_no, and email of employees:Select e.emp_specification.EXTRACT('/emp/ename/text()').getStringVal() "Name", e.emp_specification.EXTRACT('/emp/acc_no/text()').getStringVal() "Account_no", e.emp_specification.EXTRACT('/emp/email/text()').getStringVal() "Email" from employee e;

Output:-Name Account_no EmailPriya 101 [email protected] 103 [email protected] 104 [email protected] 105 [email protected] 106 [email protected] 107 [email protected] 108 [email protected] 109 [email protected] 110 [email protected] 102 [email protected] rows selected.

4) Update the 3rd record from the table and display the name of an employee.Update employee e sete.emp_specification=XMLTYPE('<emp><e_id>4</e_id><ename>shree</ename><email>[email protected]</email><acc_no>104</acc_no><mngr_email>[email protected]</mngr_email><doj>22 april 2011</doj></emp>')where e.emp_specification.EXTRACT('/emp/ename/text()').getStringVal()='shreya';

Output:-

Before updation :-

Select e.emp_specification.EXTRACT('/emp/ename/text()').getStringVal() from employee e where e.emp_specification.EXTRACT('/emp/ename/text()').getStringVal()='shreya';Name--------------------------------------------------------------------------------shreya

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 35: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

After updation :-Select e.emp_specification.EXTRACT('/emp/ename/text()').getStringVal() “Name” from employee e where e.emp_specification.EXTRACT('/emp/ename/text()').getStringVal()='shree';Name--------------------------------------------------------------------------------Shree

5) Delete 4th record from the table:SQL>delete from employee e where e.emp_specification.EXTRACT('/emp/ename/text()').getStringVal()='anthony';Output:-1 row deleted.

SQL> select * from employee;DEPT_ID----------EMP_SPECIFICATION-------------------------------------------------------------------------------- 1<emp> <e_id>1</e_id> <ename>Priya</ename> <email>[email protected]</email> 3<emp>

DEPT_ID----------EMP_SPECIFICATION-------------------------------------------------------------------------------- <e_id>3</e_id> <ename>Pornima</ename> <email>[email protected]</emai 4<emp> <e_id>4</e_id> <ename>shree</ename> <email>[email protected]</email>

DEPT_ID----------EMP_SPECIFICATION-------------------------------------------------------------------------------- 5<emp> <e_id>5</e_id> <ename>ketal</ename>

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 36: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

<email>[email protected]</email> 6 DEPT_ID----------EMP_SPECIFICATION--------------------------------------------------------------------------------<emp> <e_id>6</e_id> <ename>aakanksha</ename> <email>[email protected]</

7<emp> <e_id>7</e_id> <ename>aakash</ename> DEPT_ID----------EMP_SPECIFICATION-------------------------------------------------------------------------------- <email>[email protected]</email>

8<emp> <e_id>8</e_id> <ename>nishit</ename> <email>[email protected]</email>

9 DEPT_ID----------EMP_SPECIFICATION--------------------------------------------------------------------------------<emp> <e_id>9</e_id> <ename>swapnil</ename> <email>[email protected]</emai

2<emp> <e_id>2</e_id> <ename>mohini</ename>

DEPT_ID----------EMP_SPECIFICATION-------------------------------------------------------------------------------- <email>[email protected]</email>

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar

Page 37: Adbm ssem1

M.Sc. (Computer Science ) Part I –Sem I Date: / / 2015 Advanced Database Management System

9 rows selected.

Chikitsak Samuha’s Patkar – Varde College Krupa Bhavsar


Recommended