Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Data Models and Query Languages CSE 590DB, Winter 1999 Theory of Databases Zack Ives January 10, 1999."— Presentation transcript:

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

2 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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.


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

Similar presentations


Ads by Google