Datalog –Another query language –cleaner – closer to a “logic” notation, prolog – more convenient for analysis – can express queries that are not expressible.

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.
CS411 Database Systems Kazuhiro Minami 06: SQL. SQL = Structured Query Language Standard language for querying and manipulating data Has similar capabilities.
1 Lecture 02: SQL. 2 Outline Data in SQL Simple Queries in SQL (6.1) Queries with more than one relation (6.2) Recomeded reading: Chapter 3, Simple Queries.
SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: SQL92, SQL2, SQL3. Vendors support.
Relational Calculus and Datalog
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.
Ver 1,12/09/2012Kode :CCs 111,Sistem basis DataFASILKOM Chapter 5: Other Relational Languages Database System Concepts, 5th Ed. ©Silberschatz, Korth and.
1 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke Deductive Databases Chapter 25.
L8-S1 Datalog Queries 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San.
Relational Algebra (end) SQL April 19 th, Complex Queries Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store,
Relational Algebra Maybe -- SQL. Confused by Normal Forms ? 3NF BCNF 4NF If a database doesn’t violate 4NF (BCNF) then it doesn’t violate BCNF (3NF) !
1 Lecture 12: SQL Friday, October 26, Outline Simple Queries in SQL (5.1) Queries with more than one relation (5.2) Subqueries (5.3) Duplicates.
1 Data Definition in SQL So far we have see the Data Manipulation Language, DML Next: Data Definition Language (DDL) Data types: Defines the types. Data.
Winter 2002Arthur Keller – CS 18014–1 Schedule Today: Feb. 26 (T) u Datalog. u Read Sections Assignment 6 due. Feb. 28 (TH) u Datalog and SQL.
CSE 636 Data Integration Datalog Rules / Programs / Negation Slides by Jeffrey D. Ullman.
Lecture #3 Functional Dependencies Normalization Relational Algebra Thursday, October 12, 2000.
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.
Correlated Queries SELECT title FROM Movie AS Old WHERE year < ANY (SELECT year FROM Movie WHERE title = Old.title); Movie (title, year, director, length)
Credit: Slides are an adaptation of slides from Jeffrey D. Ullman 1.
Deductive Databases Chapter 25
Complex Queries (1) Product ( pname, price, category, maker)
One More Normal Form Consider the dependencies: Product Company Company, State Product Is it in BCNF?
Relation Decomposition A, A, … A 12n Given a relation R with attributes Create two relations R1 and R2 with attributes B, B, … B 12m C, C, … C 12l Such.
Integrity Constraints An important functionality of a DBMS is to enable the specification of integrity constraints and to enforce them. Knowledge of integrity.
Exercises Product ( pname, price, category, maker) Purchase (buyer, seller, store, product) Company (cname, stock price, country) Person( per-name, phone.
SQL (almost end) April 26 th, Agenda HAVING clause Views Modifying views Reusing views.
Logical Query Languages Motivation: 1.Logical rules extend more naturally to recursive queries than does relational algebra. u Used in SQL recursion. 2.Logical.
1. Midterm summary Types of data on the web: unstructured, semi- structured, structured Scale and uncertainty are key features Main goals are to model,
DEDUCTIVE DATABASE.
Chapter 5: Other Relational Languages. 5.2 Chapter 5: Other Relational Languages Tuple Relational Calculus Domain Relational Calculus Query-by-Example.
Recursive query plans for Data Integration Oliver Michael By Rajesh Kanisetti.
Logical Query Languages Motivation: 1.Logical rules extend more naturally to recursive queries than does relational algebra. u Used in SQL recursion. 2.Logical.
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.
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,
Computing & Information Sciences Kansas State University Wednesday, 17 Sep 2008CIS 560: Database System Concepts Lecture 9 of 42 Wednesday, 18 September.
Row Types in SQL-3 Row types define types for tuples, and they can be nested. CREATE ROW TYPE AddressType{ street CHAR(50), city CHAR(25), zipcode CHAR(10)
Lu Chaojun, SJTU 1 Extended Relational Algebra. Bag Semantics A relation (in SQL, at least) is really a bag (or multiset). –It may contain the same tuple.
Datalog Another formalism for expressing queries: - cleaner - closer to a “logic” notation - more convenient for analysis - equivalent in power to relational.
ICS 321 Fall 2011 Algebraic and Logical Query Languages (ii) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at.
1 Lecture 5: Outerjoins, Schema Creation and Views Wednesday, January 15th, 2003.
Aggregation SELECT Sum(price) FROM Product WHERE manufacturer=“Toyota” SQL supports several aggregation operations: SUM, MIN, MAX, AVG, COUNT Except COUNT,
Extensions of Datalog Wednesday, February 13, 2001.
Datalog Rules / Programs / Negation Slides by Jeffrey D. Ullman
Relational Algebra at a Glance
Lecture 8: Relational Algebra
Modifying the Database
SQL Introduction Standard language for querying and manipulating data
Logic for Artificial Intelligence
Cse 344 April 11th – Datalog.
Lecture 10: Relational Algebra (continued), Datalog
Cse 344 January 29th – Datalog.
Building a Database Application
SQL Introduction Standard language for querying and manipulating data
Motivation for Datalog
Lecture 9: Relational Algebra (continued), Datalog
Lecture 12: SQL Friday, October 20, 2000.
Where are we? Until now: Modeling databases (ODL, E/R): all about the schema Now: Manipulating the data: queries, updates, SQL Then: looking inside -
Lecture 4: SQL Thursday, January 11, 2001.
Why use a DBMS in your website?
Integrity Constraints
Lecture 3 Monday, April 8, 2002.
Logic Based Query Languages
Appendix C: Other Relational Languages
Datalog Inspired by the impedance mismatch in relational databases.
Lecture 3: Relational Algebra and SQL
CS252: Systems Programming
Syllabus Introduction Website Management Systems
Presentation transcript:

Datalog –Another query language –cleaner – closer to a “logic” notation, prolog – more convenient for analysis – can express queries that are not expressible in relational algebra or SQL (recursion). – No grouping and aggregation, bags, orderby.

Predicates and Atoms - relations are represented by predicates - tuples are represented by atoms. Purchase( “joe”, “bob”, “Nike Town”, “Nike Air”) - arithmetic comparison atoms: X Z/2 - negated atoms: NOT Product(“Brooklyn Bridge”, $100, “Microsoft”)

Datalog Rules and Queries A datalog rule has the following form: head :- atom1, atom2, …., atom,… Examples: PerformingComp(name) :- Company(name,sp,c), sp > $50 AmericanProduct(prod) :- Product(prod,pr,cat,mak), Company(mak, sp,“USA”) All the variables in the head must appear in the body. A single rule can express exactly select-from-where queries.

The Meaning of Datalog Rules AmericanProduct(prod) :- Product(prod,pr,cat,mak), Company(mak, sp,“USA”) Consider every assignment from the variables in the body to the constants in the database. If each of the atoms in the body is in the database, then the tuple for the head is in the resulting.

More Examples CREATE VIEW Seattle-view AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = “Seattle” AND Person.per-name = Purchase.buyer SeattleView(buyer,seller,product,store) :- Person(buyer, “Seattle”, phone), Purchase(buyer, seller, product, store).

More Examples (negation, union) SeattleView(buyer,seller,product,store) :- Person(buyer, “Seattle”, phone), Purchase(buyer, seller, product, store) not Purchase(buyer, seller, product, “The Bon”) Q5(buyer) :- Purchase(buyer, “Joe”, prod, store) Q5(buyer) :- Purchase(buyer, seller, store, prod), Product(prod, price, cat, maker) Company(maker, sp, country), sp > 50.

Rule Safety Every variable that appears anywhere in the query must appear also in a relational, non-negated atom in the query. Q(X,Y,Z) :- R1(X,Y) & X < Z not safe Q(X,Y,Z) :- R1(X,Y) & NOT R2(X,Y,Z) not safe

Defining Views SeattleView(buyer,seller,product,store) :- Person(buyer, “Seattle”, phone), Purchase(buyer, seller, product, store) not Purchase(buyer, seller, product, “The Bon”) Q6(buyer) :- SeattleView(buyer, “Joe”, prod, store) Q6(buyer) :- SeattleView(buyer, seller, store, prod), Product(prod, price, cat, maker) Company(maker, sp, country), sp > 50.

From Relational Algebra to Datalog We can translate any relational algebra operation to datalog: - projection - selection - union - intersection - join

Exercises Product ( name, price, category, maker) Purchase (buyer, seller, store, product) Company (name, stock price, country) Person( name, phone number, city) Ex #1: Find people who bought telephony products. Ex #2: Find names of people who bought American products Ex #3: Find names of people who bought American products and did not buy French products Ex #4: Find names of people who bought American products and they live in Seattle. Ex #5: Find people who bought stuff from Joe or bought products from a company whose stock prices is more than $50.

Transitive Closure Suppose we are representing a graph by a relation Edge(X,Y): Edge(a,b), Edge (a,c), Edge(b,d), Edge(c,d), Edge(d,e) a b c d e How can I express the query: Find all nodes reachable from a.

Only in Datalog Recursive queries: Path( X, Y ) :- Edge( X, Y ) Path( X, Y ) :- Path( X, Z ), Path( Z, Y ). A query is recursive if there is a cycle in the dependency graph of the predicates.

Evaluating Recursive Queries Path( X, Y ) :- Edge( X, Y ) Path( X, Y ) :- Path( X, Z ), Path( Z, Y ). Semantics: evaluate the rules until a fixed point: Iteration #0: Edge: {(a,b), (a,c), (b,d), (c,d), (d,e)} Path: {} Iteration #1: Path: {(a,b), (a,c), (b,d), (c,d), (d,e)} Iteration #2: Path gets the new tuples: (a,d), (b,e), (c,e) Iteration #3: Path gets the new tuple: (a,e) Iteration #4: Nothing changes -> We stop. Note: number of iterations depends on the data. Cannot be anticipated by only looking at the query!

Another Recursive Query Flight(x,y) : there is a flight from city x to y. Hub(x): x is a hub. Query: find pairs of cities for which there is a flight that goes through some hub. Fl(x,y) :- Flight(x,y) Fl(x,y) :- Flight(x,z), Fl(z,y) Answer(x,y) :- Fl(x,z), Hub(z), Fl(z,y).

Deductive Databases General idea: some relations are stored (extensional), others are defined by datalog queries (intensional). Many research projects (MCC, Stanford, Wisconsin) [Great Ph.D theses!] SQL3 realized that recursion is useful, and added linear recursion. Hard problem: optimizing datalog performance. Ideas from deductive databases made it into the mainstream.