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.
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.
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
names N1 and N2 are from the same state
if name N1 has zip Z1, and zip Z1 is for city C1 and state S1, and
if name N1 has zip Z2, and zip Z2 is for city C2 and state S2, and
if state S1 is the same as state S2.
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
pure SQL is an expression language that is (mostly) declarative.
extensions, such as T-SQL (for SQL Server) or PL/SQL (for Oracle) are needed for real applications.
What is the E-R model?
7. Data models
The Entity-Relationship (ER) data model expresses data and relationships between data.
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