Your SlideShare is downloading. ×
[ABDO] Logic As A Database Language
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

[ABDO] Logic As A Database Language

356
views

Published on

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
356
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Databases: A Logical View Logic as a Database Language Integrity Constraints Query Containment
  • 2. Logic as a Database Language
    • Base facts : represent the tuples stored in the database relations:
          • father (john, tony)
          • mother (mary, bob)
    • Deductive rules : define which derived (view) facts can be inferred from base facts:
          • parent ( X , Y )  father ( X , Y )
          • parent ( X , Y )  mother ( X , Y )
  • 3. Meaning of Deductive Rules “ The head is true if all the subgoals are true.” head body subgoals h ( X ,…)  a ( Y ,…)  b ( Z ,…)  …
  • 4. Meaning of Deductive Rules (cont.)
    • Head and subgoals are atoms .
    • An atom consists of a predicate applied to zero or more arguments (variables or constants).
    • Predicates represent relations.
    • An atom is true for given values of its variables iff the arguments form a tuple of the relation.
    • Whenever an assignment of values to all variables makes all subgoals true, the rule asserts that the resulting head is also true
    • Although is seldom shown, all variables are assumed to universally quantified in the whole formula:
    •  X  Y ( isHappy ( X )  married ( X , Y )  isRich ( Y ) )
  • 5. IDB/EDB
    • A predicate representing a stored relation is called EDB (extensional database).
    • A predicate representing a “view,” i.e., a defined relation that does not exist in the database is called IDB (intesional database).
    • Head is always IDB; subgoals may be IDB or EDB.
  • 6. Applying Rules
    • To evaluate an IDB predicate p :
      • Apply each rule for p to the current relations corresponding to its subgoals.
        • “ Apply” = If an assignment of values to variables makes the body true, insert the tuple that the head becomes into the relation for p (no duplicates).
      • Take the union of the result for each p -rule.
  • 7. Computing IDB Relations: A Naïve Algorithm
    • Notes:
      • As long as there is no negation of IDB subgoals, then each IDB relation “grows,” i.e., on each round it contains at least what it used to contain.
      • Since relations are finite, the loop must eventually terminate.
      • Result is the least fixed point ( minimal model ) of rules.
    make all IDB relations empty; REPEAT FOR (each IDB predicate p) DO evaluate p using current values of all relations; UNTIL (no IDB relation is changed)
  • 8. Computing IDB Relations: Example
    • R#0: IDB 0 = 
    • R#1: IDB 1 = IDB 0  {parent(j,t), parent(p,m), parent(m,b)}
    • R#2: IDB 2 = IDB 1  {grandfather(p,b), ancestor(j,t), ancestor(p,m), ancestor(m,b)}
    • R#3: IDB 3 = IDB 2  {ancestor(p,b)}
    • R#4: IDB 4 = IDB 3
    parent ( X , Y )  father ( X , Y ) parent ( X , Y )  mother ( X , Y ) grandfather ( X , Y )  father ( X , Z )  parent ( Z , Y ) ancestor ( X , Y )  parent ( X , Y ) ancestor ( X , Y )  parent ( X , Z )  ancestor ( Z , Y ) father (john, tony) mother (mary, bob) father (peter, mary) FACTS DEDUCTIVE RULES
  • 9. Comparative Expressive Power
    • Logical (sub)languages
    • Conjunctive queries ( views ): a single deductive rule defining an IDB predicate with no IDB subgoals
    • Nonrecursive Datalog :
      • ≥ 1 rules for each IDB predicate
      • No head predicate appearing in the rule body
    • Datalog : recursion allowed
    • Relational Algebra
    • Expressions with SELECT, PROJECT, JOIN operators
    • Expressions with SELECT, PROJECT, JOIN, UNION
    • Recursion not allowed (SQL: in a limited way)
  • 10. Datalog Extensions
    • Negated subgoals.
      • ‘ ¬’ (NOT) in front of a subgoal means that an assignment of values to variables must make it false in order for the body to be true.
      • Closed world assumption (CWA): the facts that are not known to be true, are false.
      • Example: lucky ( X )  meets ( X , Y )  ¬ isMarried ( Y )
    • Arithmetic subgoals.
      • Mostly, order and (in)equality comparisons: =, ≠, <, ≤
      • Example: composite ( A )  divides ( B , A )  B > 1  B < A
  • 11. Safe Rules
    • When we apply a rule to finite relations, we need to get a finite result.
    • Simple guarantee: safety = all variables appear in some nonnegated, relational (not arithmetic) subgoal of the body
    • Nonsafety examples:
        • isHappy ( X )  isRich ( Y )
        • bachelor ( X )  ¬ isMarried ( X )
        • bachelor ( X )  male ( X )  ¬ married ( X , Y )
        • isCheap ( X )  X < 10
    Recall that every variable is universally quantified
  • 12. Rewriting Unsafe Rules
    • Some unsafe situations can be mended:
        • bachelor ( X )  male ( X )  ¬ married ( X , Y )
        • bachelor ( X )  male ( X )  ¬ isMarried ( X )
        • isMarried ( X )  married ( X , Y )
  • 13. Integrity Constraints
    • Express conditions that every state of the database must satisfy
    • These conditions may be expressed as a general first order formulas:
    •  E  D  S ( emp ( E , D , S )  dept ( D ) )
    • “ If E is an emp of D for S, then D must be a dept”
    • In general , it is assumed that an integrity constraint is a formula in denial form:
    •  emp ( E , D , S )  ¬ dept ( D )
    • “ I cannot happen that E is an of at D for S and D is not a dept”
  • 14. Common Integrity Constraints
    • Key Integrity Constraints ( primary keys ):
      •  married ( X, Y )  married ( X, Z )  Y ≠ Z
    • Referential Integrity Constraints ( foreign keys )
      •  married ( X, Y )  ¬ person ( X )
      •  married ( X, Y )  ¬ person ( Y )
    • “ Column Check” Constraints
      •  emp ( E, D, S )  S < 30000
  • 15. Deductive Databases DDB = { F, DR, IC } Extensional - base facts Intensional - deductive rules - integrity constraints EDB IDB
  • 16. Logic vs. SQL: View Definition
    • Logic:
      • grandfather ( X , Y )  father ( X , Z )  parent ( Z , Y )
    • SQL:
      • CREATE VIEW grandfather AS
        • SELECT father.asc, parent.desc
        • FROM father, parent
        • WHERE father.desc = parent.asc
      • Negative literals can be handled by means of the NOT EXISTS sentence
      • Views defined by means of more than one rule are handled by the UNION operator
      • Recursion: in a restrictive way in SQL Standard (but not supported by most vendors)
  • 17. Logic vs. SQL: Integrity Constraint Definition
    • Logic:
      •  married ( X, Y )  married ( X, Z )  Y ≠ Z
      •  married ( X, Y )  lives (Y, kabul)
    • SQL:
      • CREATE TABLE married(
        • s1 CHAR(40) PRIMARY KEY,
        • s2 CHAR(40))
      • CREATE ASSERTION ic2 CHECK
      • (NOT EXISTS ( SELECT * FROM married, lives
      • WHERE married.s1 = lives.name
      • AND lives.loc = ‘Kabul’))
    Assertions are not broadly supported by DBMS vendors
  • 18. Logic vs. SQL: final remarks
    • Wen using Logic as a Database language, we don’t take care of:
      • NULL values
      • Bag semantics
      • Strong typing
    • So, why use Logic instead of SQL?
      • Deductive rules express things that go on in both FROM and WHERE clauses, and let us state some general principles and properties (e.g., containment of rules) that are almost impossible to state correctly in SQL.
      • We can take advantage of results, approaches, algorithms, … from Logic, Logic Programming and AI
  • 19. Queries and Answers
    • A query Q for a database D = ( EDB , DR , IC ) is a finite set of deductive rules that defines a dedicated n-ary query predicate q .
    • The answer to the query is the set of all ground facts about q obtained as a result of evaluating the deductive rules from both Q and DR on EDB :
    • { q (a 1 ,…, a n ) | q (a 1 ,…, a n )  ( Q  DR )( EDB ) }
  • 20. Query Containment
    • A query Q 1 is contained in another query Q 2 , Q 1 ⊑ Q 2 , when the answer that a query Q 1 obtains is a subset of the answer obtained by another Q 2 , for every database state.
    Q 1 is contained in Q 2 : Q 1 ⊑ Q 2 { q (a 1 ,…, a n ) | q (a 1 ,…, a n )  ( Q 1  DR )( EDB ) }   { q (b 1 ,…, b n ) | q (b 1 ,…, b n )  ( Q 2  DR )( EDB ) } for any EDB
  • 21. Query Containment under Constraints
    • When considering the integrity constraints defined in a database, the containment relationship between two queries does not need to hold for any state (content) of the database but only for the consistent ones, i.e. those that satisfy the integrity constraints
    Q 1 is contained in Q 2 wrt. IC : Q 1 ⊑ IC Q 2 { q (a 1 ,…, a n ) | q (a 1 ,…, a n )  ( Q 1  DR )( EDB ) }   { q (b 1 ,…, b n ) | q (b 1 ,…, b n )  ( Q 2  DR )( EDB ) } for any EDB that does not violate IC
  • 22. Uses of Query Containment
    • QC can be applied in several contexts:
      • query optimisation
      • materialised view and cache reuse
      • query and predicate satisfiability checking
      • integrity constraints redundancy checking
      • queries independent of updates
      • constraints independent of updates
      • concept subsumption in Description Logics
      • Etc.
    • ... without accessing to the database contents in any case.
  • 23. Uses of QC: Example emp (joan, sales) emp (pere, sales) dept (sales) manager (pere, sales) em ( E , M )  emp ( E,D )  manager ( M , D ) Ic 1 :  emp ( E , D )  ¬ dept ( D ) Ic 2 :  manager ( M , D )  ¬ emp ( M , D ) Ic 3 :  manager ( M , D )  ¬ dept ( D ) Q 1 : q ( E , M )  em ( E , M )  emp ( E , D ) Q 2 : q ( M , D )  manager ( M , D )  ¬ emp ( M , D ) U 1 : { Insert dept (accounting)}
    • emp ( E , D ) in Q 1 is redundant
    • em is independent of U 1
    • Ic 3 is redundant according to Ic 1 and Ic 2 :
    • Ic 3 ⊑ Ic 1  Ic 2
    • Q 2 is insatisfiable: Q 2 ⊑ IC Ø
    • Ic 1 , Ic 2 and Ic 3 are independent of U 1
  • 24. Containment of Conjunctive Queries
    • Recall that a CQ is a single deductive rule, with all subgoals assumed to be EDB.
    • Theorem : Q 1 ⊑ Q 2 if and only if t here is a containment mapping from Q 2 to Q 1
    • A containment mapping from Q 2 to Q 1 is a mapping from the atoms of Q 2 to the atoms of Q 1 , such that:
      • Each subgoal of Q 2 is mapped to some subgoal of Q 1 with the same predicate.
      • No variable in Q 2 mapped to two different variables in Q 1 .
      • The head of Q 2 is mapped to the head of Q 1 .
  • 25. Containment of CQs: Examples
    • Q1: p(X,Y)  r(X,Y)  g(Y,Z)
    • Q2: p(A,B)  r(A,B)  r(A,C)
    • Q3: p(U,V)  r(V,U)  r(U,W)
    Q 1 ⊑ Q 2 Q 2 ⋢ Q 3
  • 26. Complexity of Checking QC for CQ’s
    • Testing Containment is NP-complete, but CQ’s tend to be small so here is one case of a NP problem that is “tractable” in general
    • Some special cases where QC is in P:
      • Q1 (the contained query) has no more than one subgoal with the same predicate  straightforward
      • Q1 has no more than two subgoals with the same predicate  a more complex proof, based on a reduction to 2SAT.
      • Etc.