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

Adbms PDF

The document describes creating horizontal and vertical fragments of a database table across two Oracle databases. It details steps to create fragments on each database based on salary attributes, then provides example queries to retrieve data from the fragmented tables using links between the databases.

Uploaded by

Mandar Bodane
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)
28 views52 pages

Adbms PDF

The document describes creating horizontal and vertical fragments of a database table across two Oracle databases. It details steps to create fragments on each database based on salary attributes, then provides example queries to retrieve data from the fragmented tables using links between the databases.

Uploaded by

Mandar Bodane
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

Instituteof 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. InsertRecords.
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: [email protected]
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', '[email protected]',
15000)

3.Check Records

Advance Database Systems MscIT Part 1


Instituteof 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.CreateHorizontal 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


Instituteof 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


Instituteof 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 FROMEMPB


UNION
SELECT ENAME, ESALARY FROM EMPA@LINK2

OUTPUT:

Advance Database Systems MscIT Part 1


Instituteof 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@LINK2FROM EMPA@LINK2
WHERE
EMPA.ESALARY@LINK2= 17000
OUTPUT:

Advance Database Systems MscIT Part 1


Instituteof Distance & Open Learning

C.FIND THE EMPLOYEE NAME AND EMAIL WHERE EMPLOYEE


NUMBER IS KNOWN.
ON ORACLE 1

SELECTENAME, 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


Instituteof 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),
Esalarynumber);

2. InsertRecords.
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: [email protected]
Enter value for esalary: 15000
old1: INSERT INTO EMP_01 VALUES(&ENO, '&ENAME', '&EADDR', '&EMAIL',
&ESALARY)
new 1: INSERT INTO EMP_01 VALUES(1, 'NIKHIL', 'UDIT', '[email protected]',
15000)

3.Check Records

Advance Database Systems MscIT Part 1


Instituteof 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 EMPAon 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


Instituteof Distance & Open Learning

9.Create VerticalFragment EMPB on Oracle2.

CREATE TABLE EMPB


AS
SELECT ENO, EEMAIL,
ESALARY FROM
EMP_01@LINK2
10. Check Records on Fragment EMPBon Oracle2

Advance Database Systems MscIT Part 1


Instituteof 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


Instituteof 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


Instituteof Distance & Open Learning

C.FIND EMPLOYEENAME 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


Instituteof 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

SELECTEMPA.ENAME@LINK2 FROM EMPA@LINK2


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

OUTPUT:

Advance Database Systems MscIT Part 1


Instituteof 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. InsertRecords.
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: [email protected]
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', '[email protected]',
15000)

3.Check Records

Advance Database Systems MscIT Part 1


Instituteof Distance & Open Learning

4.Create link fromOracle1 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

CREATEPUBLIC 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


Instituteof 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


Instituteof Distance & Open Learning

11.FIRE THE FOLLOWING QUERIES:

A.FIND THE SALARY OF ALL EMPLOYEES

B.FIND THE EMAIL OF ALL EMPLOYEESWHERE SALARY =10000

Advance Database Systems MscIT Part 1


Instituteof 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


Instituteof Distance & Open Learning

PRACTICAL NO: 4A
TEMPORAL DATABASE

1. CREATE TABLE EMP

2. INSERT RECORDS

3. CHECKINSERTED RECORDS

Advance Database Systems MscIT Part 1


Instituteof 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


Instituteof Distance & Open Learning

PRACTICAL NO: 4B
TEMPORAL DATABASE

1.

2. INSERT RECORDS INTO SHARES

3. CHECKRECORDS IN SHARES TABLE

Advance Database Systems MscIT Part 1


Instituteof 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


Instituteof Distance & Open Learning

PRACTICAL NO5
OBJECT ORIENTED DATABASE

1. Creating Types& Tables:

Types:AddrType,BranchType,AuthorType,AuthorListType,PublisherType
Tables:BranchTableType,authors

Advance Database Systems MscIT Part 1


Instituteof Distance & Open Learning

2. Insert values:

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

Advance Database Systems MscIT Part 1


Instituteof 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 nestedtable where each element in the table has 3 parts: an address and two phones*/

Advance Database Systems MscIT Part 1


Instituteof Distance & Open Learning

Table Name3:
books Format title,year,published_by:
:
books( ref<Publishers>, authors:list of ref
Author)
/*Where published_by is a reference to elements of the table Publishers; authorsis a complex-valued
attribute, in this case a list of references to objects of the type author(managed by table ‘Author’.*/

Advance Database Systems MscIT Part 1


Instituteof 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 Authorsaut ,Publishers pub
where aut.addr=pub.addr;

Output:

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:

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

Advance Database Systems MscIT Part 1


Instituteof Distance & Open Learning

iv. List the title of the book thathas the most authors:

v. List the name of the publisherthat has the most branches:

Advance Database Systems MscIT Part 1


Instituteof Distance & Open Learning

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

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

viii. List all authors who have published more than onebook:

Advance Database Systems MscIT Part 1


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

Advance Database Systems MscIT Part 1


Instituteof Distance & Open Learning

PRACTICAL NO 6
ACTIVE DATABASES

1.Create Table EMPand insert few records.

2. Insert 10 rows in EMP

3.Check the table.

4.Create Tableprojand insert few records.

Advance Database Systems MscIT Part 1


Instituteof Distance & Open Learning

Advance Database Systems MscIT Part 1


Instituteof Distance & Open Learning

5.Create Triggers.

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

Check the trigger

Advance Database Systems MscIT Part 1


Instituteof Distance & Open Learning

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

Check the trigger

Advance Database Systems MscIT Part 1


Instituteof 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


Instituteof Distance & Open Learning

4. Create a trigger to delete theproject of an employee.

Check the trigger

Advance Database Systems MscIT Part 1


Instituteof 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_specis a schema with attributes emp id, name, email, acc_no, managerEmail,
dateOf Joning .
We create employee_spec like any other user-defined type.

Step 2: Inserting values into “employeetable”

Advance Database Systems MscIT Part 1


Instituteof Distance & Open Learning

Advance Database Systems MscIT Part 1


Instituteof 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 empis 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


Instituteof Distance & Open Learning

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

Advance Database Systems MscIT Part 1


Instituteof Distance & Open Learning

Step 4: Updating XMLType

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

Step 5:Deleting an XMLType Row

You can use extract() to identify rowsandto delete as well

Advance Database Systems MscIT Part 1


Instituteof Distance & Open Learning

PRACTICAL NO 8
DEDUCTIVEDATABASES

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 cana 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


Instituteof 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 thesecond 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


Instituteof 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 bodyof 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


Instituteof Distance & Open Learning

Source Code:

Output:

Advance Database Systems MscIT Part 1


Instituteof Distance & Open Learning

B.Two factorial definitions

Two predicate definitionsthat 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 ':-' whichcan 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 thecolon 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
headof 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 correspondsto 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


Instituteof 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 subtreesmust 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:

Output:

Advance Database Systems MscIT Part 1


Instituteof 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 insteaddrawn 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


Instituteof 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


Instituteof 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


Instituteof Distance & Open Learning
Map
coloring
SourceCode:

Advance Database Systems MscIT Part 1


Instituteof Distance & Open Learning

Output:

Advance Database Systems MscIT Part 1

You might also like