Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Prolog and Datalog


1. Prolog and Datalog
Datalog is a subset of Prolog that can be used to model relational database operations. And, since rules can be expressed in Datalog, Datalog can model what is called a deductive database.

2. Intensional database
An intensional database consists of tables, here expressed as Datalog facts.
names("Mary","22602"). names("Cory" ,"40513"). names("Amy" ,"17022"). names("Emily" ,"17022"). zips("17022","Elizabethtown","PA"). zips("40513","Lexington","KY"). zips("22602","Winchester","VA").


3. Extensional database
An extensional database consists of rules, here expressed as Datalog rules. The Datalog rule for the extensional database that determines if two names are from the same state is as follows.
samestate(N1,N2) if    name(N1,Z1) and zip(Z1,C1,S1) and    name(N2,Z2) and zip(Z2,C2,S2) and    S1 == S2.


4. English definition
The English definition of this rule is that

5. SQL queries
SQL queries can be converted to Datalog queries. The Datalog query to output the linked tables is as follows.
?-    nl, write("Name City State Zip"),    nl, write("-------- ------------- ----- -----"),    names(Name,Zip) and zips(Zip,City,State),    nl, write(Name," ",City," ",State," ",Zip),    fail.


6. SQL
What is the E-R model?

7. Data models
How does the E-R model compare with data normalization?

What are business rules?

8. Business rules
Business rules are rules that cannot be enforced by the data model (easily or at all).

9. Output
The output would appear similar to the following.
Name City State Zip Mary Winchester VA 22602 Cory Lexington KY 40513 Amy Elizabethtown PA 17022 Emily Elizabethtown PA 17022


10. SQL query
The above facts can be represented by the following database tables.
   Names(Name, Zip)    Zips(Zip, City, ST)

The SQL (Structured Query Language) query for the above Datalog query is as follows.
   SELECT Names.Name, Zips.City, Zips.ST, Zips.Zip    FROM       Names INNER JOIN Zips ON Names.Zip = Zips.Zip


11. Updated code
Here is the Prolog code.

Here is the output of the Prolog code.


12. End of page

13. Acronyms and/or initialisms for this page