1 Data Models and Query Languages CSE 590DB, Winter 1999 Theory of Databases Zack Ives January 10, 1999.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Relational Calculus and Datalog
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Relational Algebra Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
Chapter 3 : Relational Model
1 Conjunctions of Queries. 2 Conjunctive Queries A conjunctive query is a single Datalog rule with only non-negated atoms in the body. (Note: No negated.
D ATABASE S YSTEMS I R ELATIONAL A LGEBRA. 22 R ELATIONAL Q UERY L ANGUAGES Query languages (QL): Allow manipulation and retrieval of data from a database.
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 basisdataFASILKOM Chapter 2: Relational Model Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
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.
2005conjunctive1 Query languages, equivalence & containment  conjunctive queries – CQ’s  More expressive languages.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
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.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Deductive Databases Chapter 25
One More Normal Form Consider the dependencies: Product Company Company, State Product Is it in BCNF?
Relational Algebra.
Databases Illuminated
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Recursive query plans for Data Integration Oliver Michael By Rajesh Kanisetti.
DBSQL 3-1 Copyright © Genetic Computer School 2009 Chapter 3 Relational Database Model.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Logical Query Languages Motivation: 1.Logical rules extend more naturally to recursive queries than does relational algebra. u Used in SQL recursion. 2.Logical.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
1 Relational Algebra. 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of data from a database. v Relational model supports.
Datalog Inspired by the impedance mismatch in relational databases. Main expressive advantage: recursive queries. More convenient for analysis: papers.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
Relational Algebra.
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,
2.1 Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional.
Chapter 2 Introduction to Relational Model. Example of a Relation attributes (or columns) tuples (or rows) Introduction to Relational Model 2.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 5 – September 4 th,
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
CSC271 Database Systems Lecture # 7. Summary: Previous Lecture  Relational keys  Integrity constraints  Views.
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.
Query Processing – Implementing Set Operations and Joins Chap. 19.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
CS589 Principles of DB Systems Fall 2008 Lecture 4c: Query Language Equivalence Lois Delcambre
CSE202 Database Management Systems
Module 2: Intro to Relational Model
Datalog Rules / Programs / Negation Slides by Jeffrey D. Ullman
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Relational Algebra - Part 1
Chapter 3: Relational Model III
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
The Relational Algebra and Relational Calculus
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Logic Based Query Languages
Chapter 2: Intro to Relational Model
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
Datalog Inspired by the impedance mismatch in relational databases.
Relational Model B.Ramamurthy 5/28/2019 B.Ramamurthy.
Presentation transcript:

1 Data Models and Query Languages CSE 590DB, Winter 1999 Theory of Databases Zack Ives January 10, 1999

2 The Relational Model zDatabase consists of relations, where columns are attributes and rows are tuples zDeclarative queries using an algebraic or logic language (or SQL, which we shall ignore) attribute tuple arity = 3, cardinality = 3NewMovies(Name, Rating, Director) NewMovies NameRatingDirector The Phantom MenacePGGeorge Lucas Patch AdamsPG-13Tom Shadyac StepmomPG-13Chris Columbus

3 Schema zEach attribute in a relation has a type: NewMovies(Name: String, Rating: RatingDomain, Director: String), where RatingDomain = {G, PG, PG-13, R, NC-17} (In many cases, we will omit the type, and assume the type most appropriate to the expression) zRelation schema: relation name, attributes, and types zRelation instance: a set of tuples for a given schema zDatabase schema: set of relation schemas zDatabase instance: relation instance for every relation schema

4 More on Tuples zFormally, a tuple is a mapping from attribute names to (correctly typed) values: yDirector -> “George Lucas” yName -> “Stepmom” zMay specify a tuple using values & schema notation: NewMovies(“The Phantom Menace”, “PG”, “George Lucas”) zCan have constraints on attribute values: yIntegrity constraints yKeys yForeign keys yFunctional dependencies

5 Relational Algebra All operators take one or more sets of tuples as input, and produce a new set as output yBasic set operators ( , , —, but normally no complement) ySelection (  ) yProjection (  ) yCartesian Product (  ) yJoin (    ) yDivision (  )

6 Set Operations zSets must be compatible! ySame number of attributes ySame types/domains zR1  R2 = {all tuples in either R1 or R2} zR1  R2 = {all tuples in both R1 and R2} zComplement??? zNegation: R1 — R2 = {all tuples in R1 not in R2} zQuery without unions is conjunctive

7 Selection Chooses a subset of tuples satisfying a predicate  Rating = “PG-13” (NewMovies) NameRatingDirector Patch AdamsPG-13Tom Shadyac StepmomPG-13Chris Columbus NewMovies NameRatingDirector The Phantom MenacePGGeorge Lucas Patch AdamsPG-13Tom Shadyac StepmomPG-13Chris Columbus

8 Projection Chooses a subset of attributes, removes duplicates NewMovies NameRatingDirector The Phantom MenacePGGeorge Lucas Patch AdamsPG-13Tom Shadyac StepmomPG-13Chris Columbus NameDirector The Phantom MenaceGeorge Lucas Patch AdamsTom Shadyac StepmomChris Columbus  Name, Director (NewMovies)

9 Cartesian Product zCombine two relations - all pairings of tuples NewMovie Name Director Phantom Menace George Lucas Patch Adams Tom Shadyac Stepmom Chris Columbus MetroShows NameTime Patch Adams7:00 Patch Adams9:40 Stepmom6:50 Stepmom9:00 NewMovie  MetroShows Name DirectorNameTime Phantom Menace George LucasPatch Adams7:00 Phantom Menace George LucasPatch Adams9:40 Phantom Menace George LucasStepmom6:50 Phantom Menace George LucasStepmom9:00 Patch Adams Tom ShadyacPatch Adams7:00 … ……...

10 Join zCombine tuples from two relations by predicate. If predicate is equality, remove duplicate attribute. NewMovie Name Director Phantom Menace George Lucas Patch Adams Tom Shadyac Stepmom Chris Columbus MetroShows NameTime Patch Adams7:00 Patch Adams9:40 Stepmom6:50 Stepmom9:00 NewMovie    NewMovie.Name = MetroShows.Name MetroShows Name Director Time Patch Adams Tom Shadyac 7:00 Patch Adams Tom Shadyac 9:40 Stepmom Chris Columbus 6:50 Stepmom Chris Columbus 9:00 Equivalent to:  Name, Director, Time (  NewMovie.Name = MetroShows.Name (NewMovie  MetroShows))

11 Division zOnly returns results from dividend which match attributes of all of tuples in divisor (“for all”) Customers namebranch JohnsonDowntown SmithBrighton LindsayRedwood GreenBrighton GreenDowntown Branches branch Brighton Downtown Customers  Branches name Green

12 Logic-Based Query Languages zTuple-relational calculus yBased on first-order predicate calculus, but imposes restrictions (e.g., no function symbols) yEquivalent in power to relational algebra zDatalog yMore powerful than tuple-relational calculus Supports recursion and thus transitive closure yEquivalent to set of Horn clauses

13 Predicates & Atoms zRelations represented by predicates zTuples represented by atoms: Flight(“Alon”, “British Airways”, 1234, “Seattle”, “Israel”, “1/9/1999”, “10:00”) zArithmetic atoms: X Z * 2 zIn certain cases, negated atoms: not Flight(“Zack”, “British Airways”, 1234, “Seattle”, “Israel”, “1/9/1999”, “10:00”)

14 Datalog Rules & Programs z“Pure” datalog rules: head :- atom1, atom2, atom3 where all atoms are non-negated and relational zDatalog program is set of datalog rules zConjunctive query has single rule: HanksDirector(D) :- ActorIn(“Hanks”, M) & DirectedBy(D, M) zDisjunctive query: HanksDirector(D) :- ActorIn(“Hanks”, M) & DirectedBy(D, M) HanksDirector(D) :- ProducedBy(“Hanks”, M) & DirectedBy(D, M)

15 Intension & Extension zDistinction between EDB & IDB: yExtensional DB xwhat’s stored in the database xcan only be in the body of a Datalog rule yIntensional DB xresult of a datalog rule xcan be in head or body of a Datalog rule

16 Evaluating Rules zEvaluating rules: yStart with EDB, iteratively derive facts for IDB’s yRepeat until cannot derive any new facts: xConsider every possible assignment from database to variables in body xIf each atom in body is made true by assignment, add tuple for the head into the head’s relation zConjunctive queries are inexpensive zDisjunctive are more expensive (even though they’re Horn clauses)

17 Transitive Closure zSuppose we to know all possible destination airports reachable from SeaTac, given an EDB Flight(From, To): SEA SFO LAX JFK LGADEN We need to express the query: Find all airports reachable from SEA

18 Recursion in Datalog zProgram: Dest(X, Y) :- Flight(X, Y) Dest(X, Y) :- Dest(X, Z), Flight(Z, Y) zEvaluate unknown # of times until fixpoint çFlight: {(SEA,SFO), (SEA,LAX), (SEA,JFK), (LAX,LGA), (JFK,LGA),(LGA,DEN)} Dest0: {} çDest1: Dest0  {(SEA,SFO), (SEA,LAX), (SEA,JFK)} çDest2: Dest1  {(SEA,LGA),(LAX,DEN),(JFK,DEN)} çDest3: Dest2  {(SEA,DEN)} çNo more changes, so stop (minimum fixpoint)

19 Built-in Predicates zRules may include atoms with built-in predicates: Expensive(X) :- Product(X, Y, P) & P > 100 zBut need to restrict use of built-in atoms: P(X) :- R(X) & X < Y æWhen is X < Y? zWe require that every variable from a built-in atom appear in a relational atom

20 Negated Subgoals zRestrict forms: P(X, Y) :- Between(X, Y, Z) & NOT Direct(X,Z) zBad: Q(X, Y) :- From(X) & NOT To(Y) zBad but fixable: P(X) :- From(X) & NOT Connects(X, Y) zRewrite as: Connects’(X) :- Connects(X,Y) P(X) :- End(X) & NOT Connects’(X)

21 Stratified/Stratisfiable Rules zPredicate P depends on predicate Q if Q appears negated in a rule defining P zIf cycle in dependency graph, program is not stratifiable: p(X) :- r(X) & NOT q(X) q(X) :- r(X) & NOT p(X) zSuppose r has tuple {1} Intuitively, stratification provides a way of executing program P as sequence of subprograms P 1 … P n defining IDB relations w/o “forward references”. Results independent of stratification.