0% found this document useful (0 votes)
24 views52 pages

Practical No:1 Horizontal Fragmentation: Institute of Distance & Open Learning

The document describes creating replicas of a database table across two Oracle databases to implement database replication. It involves: 1. Creating a table called EMP_01 with employee data on Oracle1. 2. Creating database links between Oracle1 and Oracle2 to allow querying tables remotely. 3. Creating replicas of the EMP_01 table on both Oracle1 and Oracle2 by using the CREATE TABLE...AS SELECT query. 4. Firing sample queries on the replicas to retrieve employee salary and email information from both databases.

Uploaded by

ranajana pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views52 pages

Practical No:1 Horizontal Fragmentation: Institute of Distance & Open Learning

The document describes creating replicas of a database table across two Oracle databases to implement database replication. It involves: 1. Creating a table called EMP_01 with employee data on Oracle1. 2. Creating database links between Oracle1 and Oracle2 to allow querying tables remotely. 3. Creating replicas of the EMP_01 table on both Oracle1 and Oracle2 by using the CREATE TABLE...AS SELECT query. 4. Firing sample queries on the replicas to retrieve employee salary and email information from both databases.

Uploaded by

ranajana pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Institute of Distance & Open Learning

PRACTICAL NO:1
HORIZONTAL FRAGMENTATION

1. Create (Global conceptual schema) a table Emp_01.


create table Emp_01
(Eno number,
Enamevarchar(9),
Eaddrvarchar(15),
Eemailvarchar(15),
Esalary number);

2. Insert Records.
INSERT INTO EMP_01 VALUES(&ENO, '&ENAME', '&EADDR', '&EMAIL',
&ESALARY);
Enter value for eno: 1
Enter value for ename: NIKHIL
Enter value for eaddr: UDIT
Enter value for email: NIKHIL@MU.AC.IN
Enter value for esalary: 15000
old 1: INSERT INTO EMP_01 VALUES(&ENO, '&ENAME', '&EADDR', '&EMAIL',
&ESALARY)
new 1: INSERT INTO EMP_01 VALUES(1, 'NIKHIL', 'UDIT', 'NIKHIL@MU.AC.IN',
15000)

3. Check Records

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

4. Create link from Oracle1 to Oracle2


CREATE PUBLIC DATABASE LINK "LINK2" CONNECT TO "SYSTEM"
IDENTIFIED BY "system"
USING 'ORACLE2'
Database link created.

5. Connect to Oracle2.
connect system/tiger@oracle2
connected.

6. Create link from Oracle2 to Oracle1

CREATE PUBLIC DATABASE LINK "LINK2" CONNECT TO "SYSTEM"


IDENTIFIED BY "system"
USING 'ORACLE1'
Database link created.

7. Create Horizontal Fragment EMP1 on Oracle1.


CREATE TABLE EMPA
AS
SELECT * FROM EMP_01
WHERE
ESALARY=10000

8. Check Records on Fragment EMPA on Oracle1

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

9. Create Horizontal Fragment EMPB on Oracle2.


CREATE TABLE EMPB
AS
SELECT * FROM EMP_01@LINK2
WHERE
ESALARY>10000

10. Check Records on Fragment EMPB on Oracle2

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

11. Fire the following queries:


A. FIND SALARY OF ALL EMPLOYEES

ON ORACLE1

SELECT ENAME, ESALARY FROM EMPA


UNION
SELECT ENAME, ESALARY FROM EMPB@LINK1

OUTPUT:

ON ORACLE 2

SELECT ENAME, ESALARY FROM EMPB


UNION
SELECT ENAME, ESALARY FROM EMPA@LINK2

OUTPUT:

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

B. FIND THE EMAIL OF EMPLOYEE WHERE SALARY =17000

ON ORACLE 1

SELECT EEMAIL FROM EMPA


WHERE
ESALARY = 17000
UNION
SELECT EMPB.EEMAIL@LINK1 FROM EMPB@LINK1
WHERE
EMPB.ESALARY@LINK1 = 17000

OUTPUT:

ON ORACLE 2

SELECT EEMAIL FROM EMPB


WHERE
ESALARY = 17000
UNION
SELECT EMPA.EEMAIL@LINK2 FROM EMPA@LINK2
WHERE
EMPA.ESALARY@LINK2 = 17000

OUTPUT:

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

C. FIND THE EMPLOYEE NAME AND EMAIL WHERE EMPLOYEE


NUMBER IS KNOWN.

ON ORACLE 1

SELECT ENAME, EEMAIL FROM EMPA


WHERE
ENO = &ENO
UNION
SELECT EMPB.ENAME@LINK1, EMPB.EEMAIL@LINK1 FROM EMPB@LINK1
WHERE
EMPB.ENO@LINK1 = &ENO

OUTPUT:

ON ORACLE 2

SELECT EMPA.ENAME@LINK2, EMPA.EEMAIL@LINK2 FROM EMPA@LINK2


WHERE
EMPA.ENO@LINK2 = &ENO
UNION
SELECT ENAME, EEMAIL FROM EMPB
WHERE
ENO = &ENO

OUTPUT:

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

PRACTICAL NO: 2
VERTICAL FRAGMENTATION

1. Create (Global conceptual schema) a table Emp_01.


create table Emp_01
(Eno number,
Enamevarchar(9),
Eaddrvarchar(15),
Eemailvarchar(15),
Esalary number);

2. Insert Records.
INSERT INTO EMP_01 VALUES(&ENO, '&ENAME', '&EADDR', '&EMAIL',
&ESALARY);
Enter value for eno: 1
Enter value for ename: NIKHIL
Enter value for eaddr: UDIT
Enter value for email: NIKHIL@MU.AC.IN
Enter value for esalary: 15000
old 1: INSERT INTO EMP_01 VALUES(&ENO, '&ENAME', '&EADDR', '&EMAIL',
&ESALARY)
new 1: INSERT INTO EMP_01 VALUES(1, 'NIKHIL', 'UDIT', 'NIKHIL@MU.AC.IN',
15000)

3. Check Records

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

4. Create link from Oracle1 to Oracle2


CREATE PUBLIC DATABASE LINK "LINK2" CONNECT TO "SYSTEM"
IDENTIFIED BY "system"
USING 'ORACLE2'
Database link created.

5. Connect to Oracle2.
connect system/tiger@oracle2
connected.

6. Create link from Oracle2 to Oracle1

CREATE PUBLIC DATABASE LINK "LINK2" CONNECT TO "SYSTEM"


IDENTIFIED BY "system"
USING 'ORACLE1'
Database link created.

7. Create Vertical Fragment EMPA on Oracle1.

CREATE TABLE EMPA


AS
SELECT ENO, ENAME, EADDR
FROM EMP_01

8. Check Records on Fragment EMPA on Oracle1

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

9. Create Vertical Fragment EMPB on Oracle2.

CREATE TABLE EMPB


AS
SELECT ENO, EEMAIL, ESALARY
FROM EMP_01@LINK2

10. Check Records on Fragment EMPB on Oracle2

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

11. FIRE THE FOLLOWING QUERIES:

A. FIND SALARY OF AN EMPLOYEE WHERE EMPLOYEE NUMBER IS KNOWN

ON ORACLE 1

SELECT ENO, ESALARY FROM EMPB@LINK1


WHERE ENO IN(
SELECT ENO FROM EMPB@LINK1 WHERE ENO = &ENO)

OUTPUT:

ON ORACLE 2

SELECT ENO, ESALARY FROM EMPB


WHERE
ENO =&ENO

OUTPUT:

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

B. FIND EMAIL WHERE EMPLOYEE NAME IS KNOWN

ON ORACLE 1

SELECT ENAME, EMPB.EEMAIL@LINK1 FROM EMPA, EMPB@LINK1


WHERE ENAME = '&ENAME'
AND
EMPA.ENO = EMPB.ENO@LINK1

OUTPUT:

ON ORACLE 2

SELECT EMPA.ENAME@LINK2, EEMAIL FROM EMPA@LINK2, EMPB


WHERE ENAME = '&ENAME'
AND
EMPA.ENO@LINK2 = EMPB.ENO

OUTPUT:

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

C. FIND EMPLOYEE NAME AND EMAIL WHERE EMPLOYEE NUMBER IS KNOWN

ON ORACLE 1
SELECT ENAME, EMPB.EEMAIL@LINK1 FROM EMPA, EMPB@LINK1
WHERE
EMPA.ENO = EMPB.ENO@LINK1
AND
EMPA.ENO = &ENO

OUTPUT:

ON ORACLE 2

SELECT EMPA.ENAME@LINK2, EEMAIL FROM EMPA@LINK2, EMPB


WHERE
EMPA.ENO@LINK2 = EMPB.ENO
AND
EMPB.ENO = &ENO

OUTPUT:

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

D. FIND EMPLOYEE NAME WHOSE SALARY >17000

ON ORACLE 1

SELECT ENAME FROM EMPA


WHERE
ENO IN (SELECT EMPB.ENO@LINK1
FROM EMPB@LINK1
WHERE
EMPB.ESALARY@LINK1>17000)

OUTPUT:

ON ORACLE 2

SELECT EMPA.ENAME@LINK2 FROM EMPA@LINK2


WHERE
ENO IN (SELECT EMPB.ENO
FROM EMPB
WHERE
EMPB.ESALARY>17000)

OUTPUT:

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

PRACTICAL NO: 3
CREATING REPLICA OF DATABASE

1. Create (Global conceptual schema) a table Emp_01.


create table Emp_01
(Eno number,
Enamevarchar(9),
Eaddrvarchar(15),
Eemailvarchar(15),
Esalary number);

2. Insert Records.
INSERT INTO EMP_01 VALUES(&ENO, '&ENAME', '&EADDR', '&EMAIL',
&ESALARY);
Enter value for eno: 1
Enter value for ename: NIKHIL
Enter value for eaddr: UDIT
Enter value for email: NIKHIL@MU.AC.IN
Enter value for esalary: 15000
old 1: INSERT INTO EMP_01 VALUES(&ENO, '&ENAME', '&EADDR', '&EMAIL',
&ESALARY)
new 1: INSERT INTO EMP_01 VALUES(1, 'NIKHIL', 'UDIT', 'NIKHIL@MU.AC.IN',
15000)

3. Check Records

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

4. Create link from Oracle1 to Oracle2


CREATE PUBLIC DATABASE LINK "LINK2" CONNECT TO "SYSTEM"
IDENTIFIED BY "system"
USING 'ORACLE2'
Database link created.

5. Connect to Oracle2.
connect system/tiger@oracle2
connected.

6. Create link from Oracle2 to Oracle1

CREATE PUBLIC DATABASE LINK "LINK2" CONNECT TO "SYSTEM"


IDENTIFIED BY "system"
USING 'ORACLE1'
Database link created.

7. CREATE REPLICA OF THE TABLE EMP_01 IN ORACLE1


CREATE TABLE EMPA
AS
SELECT * FROM EMP_01

8. CHECK THE RECORDS AT REPLICA1 AT ORACLE1

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

9. CREATE REPLICA OF THE TABLE EMP_01 IN ORACLE2


CREATE TABLE EMPB
AS
SELECT * FROM EMP_01@LINK2

10. CHECK THE RECORDS AT REPLICA2 AT ORACLE2

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

11. FIRE THE FOLLOWING QUERIES:

A. FIND THE SALARY OF ALL EMPLOYEES

B. FIND THE EMAIL OF ALL EMPLOYEES WHERE SALARY =10000

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

C. FIND THE EMPLOYEE NAME AND ADDRESS WHERE EMPLOYEE NUMBER IS


KNOWN

D. FIND THE EMPLOYEE NAME AND EMAIL WHERE EMPLOYEE NUMBER IS KNOWN

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

PRACTICAL NO: 4A
TEMPORAL DATABASE

1. CREATE TABLE EMP

2. INSERT RECORDS

3. CHECK INSERTED RECORDS

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

4. FIRE THE FOLLOWING QUERIES


A. FIND ALL EMPLOYEES WHO JOINED ON 1/4/2011

OR

B. FIND ALL EMPLOYEES WHO WILL RETIRE ON 6/7/2019

OR

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

PRACTICAL NO: 4B
TEMPORAL DATABASE

1. CREATE TABLE SHARES

CREATE TABLE SHARES


(
COMPANY_NAME VARCHAR(10),
NO_SHARES NUMBER(5),
PRICE NUMBER(5),
TRANSACTION_TIME TIMESTAMP
);

2. INSERT RECORDS INTO SHARES

INSERT INTO SHARES VALUES('INFOSYS',300,10,'10-FEB-2010 9.20.45.000000 AM')

INSERT INTO SHARES VALUES('WIPRO',200,20,’8-JUL-2011 2.30.12.000000 PM’);

INSERT INTO SHARES VALUES('HIMALAYA',100,15,’18-NOV-2009 12.40.42.000000 AM’);

INSERT INTO SHARES VALUES('MBT',100,20, ’28-APR-2011 1.00.42.000000 PM’);

INSERT INTO SHARES VALUES('PATNI',500,10, ’28-APR-2011 10.07.24.000000 PM’);

3. CHECK RECORDS IN SHARES TABLE

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

4. FIRE THE FOLLOWING QUERIES:

A.FIND THE NAME OF THE COMPANY WHERE SHARE PRICE IS MORE


THAN RS.10 AT 10.07 PM.

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

PRACTICAL NO 5
OBJECT ORIENTED DATABASE

1. Creating Types & Tables:

Types: AddrType, BranchType, AuthorType, AuthorListType, PublisherType


Tables: BranchTableType, authors

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

2. Insert values:

Table Name1: authors_01


Format : authors(name, addr:<pincode,street,city,state,no>)

insert into Authors values


('Rabiner', AddrType(5002,'sstreet','pune','mha',04));

insert into Authors values


('Schiller', AddrType(7002,'lbs marg','mumbai','mha',01));

insert into authors values


('Jerry', AddrType(7003,'dstreet','mumbai','mha',1003));

insert into authors values


('Paulraj', AddrType(7008,'sstreet','mumbai','mha',1007));

insert into authors values


('Elmasri', AddrType(7006,'nstreet','mumbai','mha',1006));

insert into authors values


('Ramakrishnan', AddrType(8002,'dstreet','pune','mha',1003));

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

Table name2: Publishers


Format : Publishers(name, addr:<pincode,street,city,state,no>, branches:set
of<address:<pincode,street,city,state,no>,phone1,phone2>)
/*where addr is an object-valued attribute. Branches is a complex-valued attribute, in this case a

nested table where each element in the table has 3 parts: an address and two phones*/

insert into Publishers values


('Pearson', AddrType(4002,'rstreet','mumbai','mha',03),
BranchTableType(BranchType
(AddrType(5002,'fstreet','mumbai','mha',03),23406,69896)));

insert into Publishers values


('ekta', AddrType(7007,'sstreet','mumbai','mha',1007),
BranchTableType(BranchType(
AddrType(7007,'sstreet','mumbai','mha',1007),4543545,8676775)));

insert into Publishers values


('joshi', AddrType(7008,'sstreet','mumbai','mha',1007),
BranchTableType(BranchType(
AddrType(1002,'sstreet','nasik','mha',1007),456767,7675757)));

insert into Publishers values('wiley',


AddrType(6002,'sstreet','nasik','mha',1007),
BranchTableType(BranchType(
AddrType(6002,'sstreet','nasik','mha',1007),4543545,8676775)));

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

Table Name3: books


Format : books(title,year,published_by: ref<Publishers>,
authors:list of ref Author)

/*Where published_by is a reference to elements of the table Publishers; authors is a complex-valued


attribute, in this case a list of references to objects of the type author(managed by table ‘Author’.*/

insert into books insert into books


select 'DSP','28-may-1983',ref(pub), select 'compiler','09-jan-1890',ref(pub),
AuthorListType(ref(aut)) AuthorListType(ref(aut))
from from
Publishers pub,Authors aut Publishers pub,Authors aut
where where
pub.name='joshi' and pub.name='wiley'and
aut.name='Elmasri'; aut.name='Jerry';

insert into books insert into books


select 'Speech Recognition','25-may- select 'DBMS','28-may-1983',ref(pub),
1983',ref(pub), AuthorListType(ref(aut))
AuthorListType(ref(aut)) from
from Publishers pub,Authors aut
Publishers pub,Authors aut where
where pub.name='joshi' and
pub.name='Pearson'and aut.name='Elmasri';
aut.name='Rabiner';

insert into books insert into books


select 'DBMS','28-may-1983',ref(pub), select 'DSP','28-may-1983',ref(pub),
AuthorListType(ref(aut)) AuthorListType(ref(aut))
from from
Publishers pub,Authors aut publishers pub,Authors aut
where where
pub.name='Pearson' and pub.name='joshi' and
aut.name='Elmasri'; aut.name='Jerry';

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

3.Firing queries on the created tables

i. List all of the authors that have the same address as their publisher:

select aut.name
from Authors aut ,Publishers pub
where aut.addr=pub.addr;

Output:

NAME
----------------------------
Paulraj

ii. List all of the authors that have the same pin code as their publisher:

select aut.name
from Authors aut ,Publishers pub
where aut.addr.pincode=pub.addr.pincode;

Output:

NAME
----------------------------
Paulraj

iii. List all books that have 2 or more authors:

select * from books b where 1 < (


select count(*)
from table(b.authors));

Output:

no rows selected

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

iv. List the title of the book that has the most authors:

Select title
from books b, table(b.authors)
group by title
having count(*) =
(select max(count(*)) from books b, table(b.authors)
group by title);

Output:

TITLE
----------------------
DBMS
DSP

v. List the name of the publisher that has the most branches:

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
-----------------------
Pearson
ekta
joshi
wiley

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

vi. Name of authors who have not published a book:

select a.name
from authors a
where not exists( select b.title
from books b, table(b.authors)
where a.name = name);

Output:
no rows selected.

vii. Move all the branches that belong to the publisher 'tata' to the publisher ‘joshi'.

insert into table(


select branches
from publishers
where name = 'wiley')
select b.* from publishers p, table(p.branches) b
where name = 'ekta';

Output:
1 row created.

viii. List all authors who have published more than one book:

select a.name from authors a, books b,table(b.authors) v


where
v.column_value=ref(a) group by a.name;

Output:

NAME
---------------------
Elmasri
Jerry
Rabiner

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

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

select title
from authors a, books b, table(b.authors) v
where v.column_value = ref(a)
group by title having count(*) > 1

Output:

TITLE
----------------------
DBMS
DSP

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

PRACTICAL NO 6
ACTIVE DATABASES

1. Create Table EMP and insert few records.

2. Insert 10 rows in EMP


insert into Emp values(7,'minal',10,13,67);

3. Check the table.

4. Create Table proj and insert few records.

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

5. Create Triggers.

1. Creating a trigger to insert new employee tuple and display the new total hours
from project table.

Check the trigger

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

2. Creating a trigger to change the hours of existing employee and display the new total
hours from project table.

Check the trigger

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

3. Create a trigger to change the project of existing employee & display the new total
hours from project.

Check the trigger

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

4. Create a trigger to delete the project of an employee.

Check the trigger

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

PRACTICAL NO 7
XML DATABASES

Step 1: Creating table with XMLType

Create a table called “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 .

We create employee_spec like any other user-defined type.

CREATE TABLE employee(


dept_id NUMBER(3),
employee_spec XMLTYPE);

Step 2: Inserting values into “employee table”

INSERT INTO employee VALUES


(100, XMLType(
'<employees>
<emp id="1">
<name>nikhil p</name>
<email>nikhilp@mu.ac.in</email>
<acc_no>111</acc_no>
<mgr_email>hod@udit.com</mgr_email>
<dateOfJoining>2009-11-16</dateOfJoining>
</emp>
</employees>'))

1 row created.

INSERT INTO employee VALUES


(200, XMLType(
'<employees>
<emp id="2">
<name>pooja</name>
<email>pooja@mu.ac.in</email>
<acc_no>222</acc_no>
<mgr_email>hod@udcs.com</mgr_email>
<dateOfJoining>2009-7-16</dateOfJoining>
</emp>
</employees>'))

1 row created.

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

INSERT INTO employee VALUES


(300, XMLType(
'<employees>
<emp id="3">
<name>subodh</name>
<email>subodh@mu.ac.in</email>
<acc_no>333</acc_no>
<mgr_email>hod@udit.com</mgr_email>
<dateOfJoining>2010-4-1</dateOfJoining>
</emp>
</employees>'))

1 row created.

INSERT INTO employee VALUES


(400, XMLType(
'<employees>
<emp id="4">
<name>pooja</name>
<email>niketa@mu.ac.in</email>
<acc_no>444</acc_no>
<mgr_email>hod@udcs.com</mgr_email>
<dateOfJoining>2011-6-20</dateOfJoining>
</emp>
</employees>'))

1 row created.

INSERT INTO employee VALUES


(500, XMLType(
'<employees>
<emp id="5">
<name>amit</name>
<email>amit@somecollege.com</email>
<acc_no>555</acc_no>
<mgr_email>hod.dept@somecollege.com</mgr_email>
<dateOfJoining>2011-6-20</dateOfJoining>
</emp>
</employees>'))

1 row created.

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

Step 3: Firing Queries on XML database.

1. Retrieve the names of employee from employee table

Name of an employee is a child of emp and emp is a child of employees in employee


specification.

2. Retrieve the acc_no of employee from employee table

Acc_no of an employee is a child of emp and emp is a child of employees in employee


specification.

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

3. Retrieve the names, acc_no, email of employees from employee table

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

Step 4: Updating XMLType

To update an XML document, you can execute a standard SQL UPDATE statement. You need to
bind an XMLType instance, as follows:

Step 5: Deleting an XMLType Row

You can use extract() to identify rows and to delete as well

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

PRACTICAL NO 8
DEDUCTIVE DATABASES

A. Towers of Hanoi puzzle

This object of this famous puzzle is to move N disks from the left peg to the right peg using the
center peg as an auxiliary holding peg. At no time can a larger disk be placed upon a smaller disk.
The following diagram depicts the starting setup for N=3 disks.

Here is a recursive Prolog program that solves the puzzle. It consists of two clauses.

move(1,X,Y,_) :-
write('Move top disk from '),
write(X),
write(' to '),
write(Y),
nl.

move(N,X,Y,Z) :-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).

The variables filled in by '_' (or any variables beginning with underscore) are 'don't-care' variables. Prolog
allows these variables to freely match any structure, but no variable binding results from this gratuitous
matching.

Here is what happens when Prolog solves the case N=3.

?- move(3,left,right,center).
Move top disk from left to right
Move top disk from left to center
Move top disk from right to center

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

Move top disk from left to right


Move top disk from center to left
Move top disk from center to right
Move top disk from left to right

yes

The first clause in the program describes the move of a single disk. The second clause declares how a
solution could be obtained, recursively. For example, a declarative reading of the second clause for N=3,
X=left, Y=right, and Z=center amounts to the following:

move(3,left,right,center) if

move(2,left,center,right) and ] *
move(1,left,right,center) and
move(2,center,right,left). ] **

This declarative reading of the clause is obviously correct. The procedural reading is closely related to the
declarative interpretation of the recursive clause. The procedural interpretation would go something like
this:

In order to satisfy the goal ?- move(3,left,right,center) do this :

satisfy the goal ?-move(2,left,center,right), and then


satisfy the goal ?-move(1,left,right,center), and then
satisfy the goal ?-move(2,center,right,left).

Also, we could write the declarative readings for N=2:

move(2,left,center,right) if ] *

move(1,left,right,center) and
move(1,left,center,right) and
move(1,right,center,left).

move(2,center,right,left) if ] **

move(1,center,left,right) and
move(1,center,right,left) and
move(1,left,right,center).

Now substitute the bodies of these last two implications for the heads and one can "see" the solution that
the prolog goal generates.

move(3,left,right,center) if

move(1,left,right,center) and
move(1,left,center,right) and *

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

move(1,right,center,left) and
---------------------------
move(1,left,right,center) and
---------------------------
move(1,center,left,right) and
move(1,center,right,left) and **
move(1,left,right,center).

A procedural reading for this last big implication should be obvious. This example illustrates well three
major operations of Prolog:

1) Goals are matched against the head of a rule, and

2) the body of the rule (with variables appropriately bound) becomes a new sequence of goals,
repeatedly,

until

3) some base goal or condition is satisfied, or some simple action is taken (like printing something).

The variable matching process is called unification.

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

Source Code:

move(1,X,Y,_):-
write('Move top disk from '),
write(X),
write(' to '),
write(Y),
nl.
move(N,X,Y,Z):-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).

Output:

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

B. Two factorial definitions

Two predicate definitions that calculate the factorial function are in file 2_2.pl, which the reader
can view by clicking on the 'Code' link at the bottom of this page. The first of these definitions is:

factorial(0,1).

factorial(N,F) :-
N>0,
N1 is N-1,
factorial(N1,F1),
F is N * F1.

This program consists of two clauses. The first clause is a unit clause, having no body. The
second is a rule, because it does have a body. The body of the second clause is on the right side of
the ':-' which can be read as "if". The body consists of literals separated by commas ',' each of
which can be read as "and". The head of a clause is the whole clause if the clause is a unit clause,
otherwise the head of a clause is the part appearing to the left of the colon in ':-'. A declarative
reading of the first (unit) clause says that "the factorial of 0 is 1" and the second clause declares
that "the factorial of N is F if N>0 and N1 is N-1 and the factorial of N1 is F1 and F is N*F1".

The Prolog goal to calculate the factorial of the number 3 responds with a value for W, the goal
variable:

?- factorial(3,W).
W=6

Consider the following clause tree constructed for the literal 'factorial(3,W)'. As explained in the
previous section, the clause tree does not contain any free variables, but instead has instances
(values) of variables. Each branching under a node is determined by a clause in the original
program, using relevant instances of the variables; the node is determined by some instance of the
head of a clause and the body literals of the clause determine the children of the node in the clause
tree.

Fig. 2.2
Fig. 2.2

All of the arithmetic leaves are true by evaluation (under the intended interpretation), and the
lowest link in the tree corresponds to the very first clause of the program for factorial. That first
clause could be written

factorial(0,1) :- true.

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

and, in fact, ?- true is a Prolog goal that always succeeds (true is built-in). For the sake of brevity,
we have not drawn 'true' leaves under the true arithmetic literals.

The program clause tree provides a meaning of the program for the goal at the root of the tree.
That is, 'factorial(3,6)' is a consequence of the Prolog program, because there is a clause tree
rooted at 'factorial(3,6)' all of whose leaves are true. The literal 'factorial(5,2)' is, on the other
hand, not a consequence of the program because there is no clause tree rooted at 'factorial(5,2)'
having all true leaves. Thus the meaning of the program for the literal 'factorial(5,2)' is that it is
false. In fact,

?- factorial(3,6).
yes
?- factorial(5,2).
no

as expected. Clause trees are so-called AND-trees, since, in order for the root to be a consequence
of the program, each of its subtrees must also be rooted at literals which are themselves
consequences of the program. We will have more to say about clause trees later. We have
indicated that clause trees provide a meaning or semantics for programs. We will see another
approach to program semantics in Chapter 6. Clause trees do provide an intuitive, as well as a
correct, approach to program semantics.

Factorial
Source Code:

factorial(0,1).
factorial(N,F):-
N>0,
N1 is N-1,
factorial(N1,F1),
F is N*F1.

Output:

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

C. Map colorings

A famous problem in mathematics concerns coloring adjacent planar regions. Like cartographic
maps, it is required that, whatever colors are actually used, no two adjacent regions may not have
the same color. Two regions are considered adjacent provided they share some boundary line
segment. Consider the following map.

We have given numerical names to the regions. To represent which regions are adjacent, consider
also the following graph.

Here we have erased the original boundaries and have instead drawn an arc between the names of
two regions, provided they were adjacent in the original drawing. In fact, the adjacency graph will
convey all of the original adjacency information. A Prolog representation for the adjacency
information could be represented by the following unit clauses, or facts.

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

adjacent(1,2). adjacent(2,1).
adjacent(1,3). adjacent(3,1).
adjacent(1,4). adjacent(4,1).
adjacent(1,5). adjacent(5,1).
adjacent(2,3). adjacent(3,2).
adjacent(2,4). adjacent(4,2).
adjacent(3,4). adjacent(4,3).
adjacent(4,5). adjacent(5,4).

If these clauses were loaded into Prolog, we could observe the following behavior for some goals.

?- adjacent(2,3).
yes
?- adjacent(5,3).
no
?- adjacent(3,R).
R=1;
R=2;
R=4;
no

One could declare colorings for the regions in Prolog also using unit clauses.

color(1,red,a). color(1,red,b).
color(2,blue,a). color(2,blue,b).
color(3,green,a). color(3,green,b).
color(4,yellow,a). color(4,blue,b).
color(5,blue,a). color(5,green,b).

Here we have encoded 'a' and 'b' colorings. We want to write a Prolog definition of a conflictive
coloring, meaning that two adjacent regions have the same color. For example, here is a Prolog
clause, or rule to that effect.

conflict(Coloring) :-
adjacent(X,Y),
color(X,Color,Coloring),
color(Y,Color,Coloring).

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

For example,

?- conflict(a).
no
?- conflict(b).
yes
?- conflict(Which).
Which = b

Here is another version of 'conflict' that has more logical parameters.

conflict(R1,R2,Coloring) :-
adjacent(R1,R2),
color(R1,Color,Coloring),
color(R2,Color,Coloring).

Prolog allows and distinguishes the two definitions of 'conflict'; one has one logical parameter
('conflict/1') and the other has three ('conflict/3'). Now we have

?- conflict(R1,R2,b).
R1 = 2 R2 = 4
?- conflict(R1,R2,b),color(R1,C,b).
R1 = 2 R2 = 4 C = blue

The last goal means that regions 2 and 4 are adjacent and both are blue. Grounded instances like
'conflict(2,4,b)' are said to be consequences of the Prolog program. One way to demonstrate such
a consequence is to draw a program clause tree having the consequence as the root of the tree, use
clauses of the program to branch the tree, and eventually produce a finite tree having all true
leaves

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

Map coloring
Source Code:

/*-------Facts------*/
adjacent(1,2).
adjacent(1,3).
adjacent(1,4).
adjacent(1,5).
adjacent(2,1).
adjacent(2,3).
adjacent(2,4).
adjacent(3,1).
adjacent(3,2).
adjacent(3,4).
adjacent(4,1).
adjacent(4,2).
adjacent(4,3).
adjacent(4,5).
adjacent(5,1).
adjacent(5,4).
color(1,red,a).
color(2,green,a).
color(3,red,a).
color(4,yellow,a).
color(5,green,a).
color(1,blue,b).
color(2,red,b).
color(3,pink,b).
color(4,blue,b).
color(5,green,b).
/*-----Rules-----*/
conflict(Coloring):-
adjacent(X,Y),
color(X,Color,Coloring),
color(Y,Color,Coloring).
conflict(R1,R2,Coloring):-
adjacent(R1,R2),
color(R1,Color,Coloring),
color(R2,Color,Coloring).

Advance Database Systems MscIT Part 1


Institute of Distance & Open Learning

Output:

Advance Database Systems MscIT Part 1

You might also like