Datalog Presented by: Michael Lam CS 157A section 1 Dr. Sin Min Lee.

Slides:



Advertisements
Similar presentations
Chapter 5: Other Relational Languages
Advertisements

1 Datalog: Logic Instead of Algebra. 2 Datalog: Logic instead of Algebra Each relational-algebra operator can be mimicked by one or several Database Logic.
Relational Calculus and Datalog
Chapter 5: Other Relational Languages
CSE 636 Data Integration Conjunctive Queries Containment Mappings / Canonical Databases Slides by Jeffrey D. Ullman.
Lecture 11: Datalog Tuesday, February 6, Outline Datalog syntax Examples Semantics: –Minimal model –Least fixpoint –They are equivalent Naive evaluation.
1. An Overview of Prolog.
Dr. Alexandra I. Cristea CS 319: Theory of Databases: C6.
Chapter 3 Tuple and Domain Relational Calculus. Tuple Relational Calculus.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE) Datalog.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query.
Ver 1,12/09/2012Kode :CCs 111,Sistem basis DataFASILKOM Chapter 5: Other Relational Languages Database System Concepts, 5th Ed. ©Silberschatz, Korth and.
Answer Set Programming Overview Dr. Rogelio Dávila Pérez Profesor-Investigador División de Posgrado Universidad Autónoma de Guadalajara
SECTION 21.5 Eilbroun Benjamin CS 257 – Dr. TY Lin INFORMATION INTEGRATION.
RELATIONAL CALCULUS Rohit Khokher. INTRODUCTION Relational calculus is a formal query language where the queries are expressed as variables and formulas.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Domain Relational Calculus and Query-by-Example CS157a John Eagle.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Relational Calculus Ameetinder Singh CS 157A. Tuple Relational Calculus  non-procedural query language as compared to relational algebra that is procedural.
1 9. Evaluation of Queries Query evaluation – Quantifier Elimination and Satisfiability Example: Logical Level: r   y 1,…y n  r’ Constraint.
2005certain1 Views as Incomplete Databases – Certain & Possible Answers  Views – an incomplete representation  Certain and possible answers  Complexity.
Chapter 5 Other Relational Languages By Cui, Can B.
SPRING 2004CENG 3521 E-R Diagram for the Banking Enterprise.
Embedded SQL Direct SQL is rarely used: usually, SQL is embedded in some application code. We need some method to reference SQL statements. But: there.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 5: Other Relational.
Dr. Alexandra I. Cristea CS 319: Theory of Databases: C6.
Deductive Databases Chapter 25
DEDUCTIVE DATABASE.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Chapter 5: Other Relational Languages. 5.2 Chapter 5: Other Relational Languages Tuple Relational Calculus Domain Relational Calculus Query-by-Example.
1 The Relational Model By Elena Ciriani CS157A February 19, 2004 Professor Lee.
Slide 1 Propositional Definite Clause Logic: Syntax, Semantics and Bottom-up Proofs Jim Little UBC CS 322 – CSP October 20, 2014.
Computing & Information Sciences Kansas State University Thursday, 08 Feb 2007CIS 560: Database System Concepts Lecture 11 of 42 Thursday, 08 February.
Datalog Inspired by the impedance mismatch in relational databases. Main expressive advantage: recursive queries. More convenient for analysis: papers.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Query Languages Tuple Relational Calculus Domain Relational Calculus.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
Datalog –Another query language –cleaner – closer to a “logic” notation, prolog – more convenient for analysis – can express queries that are not expressible.
Chapter 5 Notes. P. 189: Sets, Bags, and Lists To understand the distinction between sets, bags, and lists, remember that a set has unordered elements,
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Appendix C: Other Relational.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Database System Concepts, 5th Ed. Bin Mu at Tongji University Chapter 5: Other Relational Languages.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Module A: Formal Relational.
Computing & Information Sciences Kansas State University Wednesday, 17 Sep 2008CIS 560: Database System Concepts Lecture 9 of 42 Wednesday, 18 September.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan Chapter 5: Other Relational Languages.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 21 A First Course in Database Systems.
Copyright © Curt Hill The Relational Calculus Another way to do queries.
Datalog Another formalism for expressing queries: - cleaner - closer to a “logic” notation - more convenient for analysis - equivalent in power to relational.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 5: Other Relational.
CS589 Principles of DB Systems Fall 2008 Lecture 4c: Query Language Equivalence Lois Delcambre
CS589 Principles of DB Systems Fall 2008 Lecture 4b: Domain Independence and Safety Lois Delcambre
Relational Algebra - Part 1
Goal for this lecture Demonstrate how we can prove that one query language is more expressive than (i.e., “contained in” as described in the book) another.
Chapter 6: Formal Relational Query Languages
Chapter 5: Other Relational Languages
Relational Algebra : #I
Knowledge Representation and Inference
Logic Based Query Languages
Chapter 6: Formal Relational Query Languages
Appendix C: Other Relational Languages
Datalog Inspired by the impedance mismatch in relational databases.
Chapter 27: Formal-Relational Query Languages
Representations & Reasoning Systems (RRS) (2.2)
CS589 Principles of DB Systems Fall 2008 Lecture 4a: Introduction to Datalog Lois Delcambre
CS589 Principles of DB Systems Fall 2008 Lecture 4a: Introduction to Datalog Lois Delcambre
CS589 Principles of DB Systems Fall 2008 Lecture 4e: Logic (Model-theoretic view of a DB) Lois Delcambre
CS589 Principles of DB Systems Fall 2008 Lecture 4b: Domain Independence and Safety Lois Delcambre
Presentation transcript:

Datalog Presented by: Michael Lam CS 157A section 1 Dr. Sin Min Lee

Outline Basic Structure/Terminolgoy (5.2.1) Syntax of Datalog Rules (5.2.2) Safety (5.2.4) Recursion in Datalog (5.2.6) Relational Operations in Datalog (5.2.5) Examples

What is Datalog? Datalog is a nonprocedural query language based on the logic- programming language Prolog. A user describes the information desired without giving a specific procedure for obtaining that information. Datalog simplifies writing simple queries and makes query optimization easier.

Terminology Variable A variable is like a variable in a mathematical equation, it represents some constant that has yet to be determined. By convention variables always start with an upper case letter, e.g., City, Firstname,Code, A, B, C, etc Constant A constant is an attribute value. By convention all constants begin with lower-case letters such as: sammy, joe, xxy2003, etc fact A fact is the same things a tuple in a relation. A fact has the following structure. (called Ground facts, because there are no variables) ( ) Example: Facts about student enrollment: The following set of facts shows some students and the subjects in which they are enrolled. enrolledIn(joe, cp2001) enrolledIn(sue, cp2001) enrolledIn(joe, cp2003). I n relational database terms, it would be the same as the following relation. enrolledIn Rules A rule is a way to derive new facts. It is part of a query.

JOE CP2001 SUECP2001 JOECP2003 The major difference in the representation is that in the Datalog facts the attributes are not named. So there is nothing to tell us that the first attribute is a Name and the second is a Code. enrolledIn

Syntax of Datalog Rules A rule has the following form :- Head is a single predicate and the body is a list of predicates. Each predicate has the form positive literal: ( ) negative literal: not ( ) What does :- mean? Assume we have a rule: Q :- P Then :- means, if P is true then Q is true Example: A rule about students The following rule identifies courses students are enrolled in.

student result(Name, Code, Phone) :- student(Name, Phone), enrolledIn(Name, Code). enrolledIn NamePhone joe NameCode joeCP2004 sueCP2001 joeCP2003 Test every combination of student and enrooledIn that unifies student(joe, ), enrolledIn(joe, CP2004) student(joe, ), enrolledIn(sue, CP2001) student(joe, ), enrolledIn(joe, CP2003) Result: result(joe, CP2004, ) result(joe, CP2003, )

Safety It may be possible to write rules that generate an an infinite number of answers. For example: employee(X,Y) :- X>Y Since the relation defining > is infinite, this rule would generate an infinite number of facts for the relation employee. Also the use of negation can cause similar problems. For example: not-in-loan(L, B, A) :- not loan(L, B, A) We what to retrieve all tuples that are not in the loan relation but if account numbers, branch-name and balance is infinite then the relation not-in-loan would also be infinite. To avoid such problems Datalog rules are required to satisfy the following safety conditions: 1. Every variable that appears in the head of the rule also appears in a nonarithmetic positive literal in the body of the rule 2. Every variable appearing in a negative literal in the body of the rule also appears in some positive literal in the body of the rule.

Recursion in Datalog Procedure I = set of facts in the database repeat Old_I = I I = I U infer( R, I) until I = Old_I Suppose now that we want to find out which employees are supervised, directly or indirectly by a given manager, "Jones". (1) People whose manager is "Jones" (2) People whose manager is supervised by "Jones" Ex. Tim manager is Jones Kelly manager is Tim employee-jones(X) :- manager(X, "Jones") employee-jones(X) :- manager(X,Y), employee-jones(Y)

employee-namemanager-name AlonBarinsky Estovar CorbinDuarte Jones EstovarJones Klinger RensalKlinger IterationTuples in employee-jones 0 1(Duarte), (Estovar) 2(Duarte), (Estovar), (Barinsky), (Corbin) 3(Duarte), (Estovar), (Barinsky), (Corbin), (Alon) 4 manager relation employee-jones(X) :- manager(X, "Jones") employee-jones(X) :- manager(X,Y), employee-jones(Y)

Relational Operations in Datalog Relational Algebra: names = Π name (student) Datalog Rule: names(Name) :- student(Name,_) Relational Algebra: joeInfo = Π studentID (σ name="joe" (student)) Datalog Rule: joeInfo(StudentID) :- student(joe,StudentID) Relational Algebra: Π coursName (σ name="joe" (student |><| courses)) Datalog Rule: joeInfo(Name,Cname) :- student(joe, StudentID), courses(Cname, StudentID) student(Name,StudentID) courses(Cname, StudentID)

Examples Relational Algebra: joeInfo = Π studentID (σ name="joe", studentID > 25 (student)) Datalog Rule: joeInfo(StudentID) :- student(joe,StudentID), StudentID > 25 Relational Algebra: Π cName (courses) |><| Π name (student) Datalog Rule: joeInfo(Name,Cname) :- student(Name, StudentID), courses(Cname, StudentID) Relational Algebra: Π studentID (σ name  "joe", studentID > 25 (student)) Datelog Rule: joeInfo(StudentID) :- student(Name,StudentID), StudentID > 25, Name <> joe