In contrast to Prolog, it
Datalog was popular in academic database research but never succeeded in becoming part of a commercial database system, despite its advantages (compared to other database languages such as SQL) such as recursive queries and clean semantics. Even so, some widely used database systems include ideas and algorithms developed for Datalog. For example, the standard includes recursive queries, and the Magic Sets algorithm (initially developed for the faster evaluation of Datalog queries) is implemented in IBM's DB2.
Two extensions that have been made to Datalog include an extension to allow object-oriented programming and an extension to allow disjunctions as heads of clauses. Both extensions have major impacts on the definition of Datalog's semantics and on the implementation of a corresponding Datalog interpreter.
These two lines define two facts, i.e. things that always hold. They can be intuitively understood as: the parent of bill is mary and the parent of mary is john.
ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :- ancestor(X,Z),ancestor(Z,Y).
These two lines describe the rules that define the ancestor relationship. A rule consists of two main parts separated by the :- symbol. The part to the left of this symbol is the head, the part to the right the body of the rule. A rule is read (and can be intuitively understood) as if it is known that . Uppercase letters stand for variables. Hence in the example the first rule can be read as X is the ancestor of Y if it is known that X is the parent of Y. And the second rule as X is the ancestor of Y if it is known that X is the ancestor of some Z and Z is the ancestor of Y. The ordering of the clauses is irrelevant in Datalog in contrast to Prolog which depends on the ordering of clauses for computing the result of the query call.
Datalog distinguishes between extensional and intensional predicate symbols. While extensional predicate symbols are only defined by facts, intensional predicate symbols are defined only by rules. In the example above
ancestor is an intensional predicate symbol, and
parent is extensional. Predicates may also be defined by facts and rules and therefore neither be purely extensional nor intensional, but any datalog program can be rewritten into an equivalent program without such predicate symbols with duplicate roles.
The query above asks for all ancestors of bill and would return mary and john when posed against a Datalog system containing the facts and rules described above.