conceptual query languages - york university

42
1.1 Conceptual Query Languages Relational Algebra Basic Operations

Upload: others

Post on 29-Dec-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Conceptual Query Languages - York University

1 . 1

Conceptual Query LanguagesRelational AlgebraBasic Operations

Page 2: Conceptual Query Languages - York University

1 . 2

Table of ContentsConceptual Query Languages 0

Table of Contents 0/1Parke Godfrey 0/2Acknowledgments 0/3

What is an algebra? 1

What is the relational algebra? 1/1The SQL Language 1/2

The Core Relational Algebra 2Selection 3

Page 3: Conceptual Query Languages - York University

1 . 3

Parke Godfrey2016-10-19 initial [v1] 2016-10-24 [v2]

Page 4: Conceptual Query Languages - York University

1 . 4

AcknowledgmentsThanks

to Jeffrey D. Ullman for initial slidedeck

to Jarek Szlichta for the slidedeck with significant refinements on whichthis is derived

Page 5: Conceptual Query Languages - York University

2 . 1

What is an algebra?A mathematical system consisting of

operands, variables or values from which new valuescan be constructed; and

operators, symbols denoting procedures thatconstruct new values from given values.

Page 6: Conceptual Query Languages - York University

2 . 2

What is the relational algebra?An algebra whose operands are relations.

Its operators are designed to do the most basic thingsover relations that we need in order to “query” over adatabase.

The result is an algebra that can be used as a querylanguage on relations.

Page 7: Conceptual Query Languages - York University

2 . 3

The SQL Language

a b5 2010 3020 40

a b5 2010 30

select b from R where a <= 10;

Page 8: Conceptual Query Languages - York University

3

The Core Relational Algebra1. Union (“ ”), Intersection (“ ”), & Difference (“ ”):

the usual set operations;but both operands must have a matching schema.

2. Selection (“ ”):choosing (selecting) certain rows.

3. Projection (“ ”):choosing (projecting) certain columns.

4. Product (“ ”) & Join (“ ”):compositions of relations.

5. Rename (“ ”):renaming of relations and attributes.

Page 9: Conceptual Query Languages - York University

4 . 1

Selection

is a condition — as in “if” statements — written overthe attributes of that evaluates to true or false pertuple.

is the set of all the tuples of that satisfy ; thatis, those tuples from for which evaluates true.

Page 10: Conceptual Query Languages - York University

4 . 2

Example of selection

pub beer priceJoe's Bud 2.50Joe's Molsen 2.75Fox Bud 2.50Fox Molsen 3.50

pub beer priceJoe's Bud 2.50Joe's Molsen 2.75

Page 11: Conceptual Query Languages - York University

5 . 1

Projection

is a list of attr's from the schema of .

is constructed by

taking each tuple from ,extracting the attr's from the tuple in list , andcreating from those components a tuple for .

Eliminate duplicate tuples in , if any.

Page 12: Conceptual Query Languages - York University

5 . 2

Example of prjection

pub beer priceJoe's Bud 2.50Joe's Molsen 2.75Fox Bud 2.50Fox Molsen 3.50

beer priceBud 2.50

Molsen 2.75Molsen 3.50

Page 13: Conceptual Query Languages - York University

5 . 3

Extended projectionWe extend what is allowed in list for .

We allow it to contain arbitrary expressions involving theattr's.

1. Arithmetic operations over the attr's; e.g., .2. String manipulation operators over string-domain

attr's, concatenate, etc.3. Duplicate occurrences of the same attr.! E.g.,

.Note that an “identity” operator would let us “copy” an attr. while giving it a new name; i.e.,

. In fact, this would be preferable, as we want to insist that the columns (attr's) of arelation are uniquely named.

Page 14: Conceptual Query Languages - York University

5 . 4

Example: extended projection

1 23 4

3 1 17 3 3

Page 15: Conceptual Query Languages - York University

Product

Pair each tuple with each tuple .

The concatenation is a tuple of .

The schema of is the union of the attr's of and .

Note. If there is an attr. named in both and ,we get both copies; by convention, we rename them

and , respectively.

Page 16: Conceptual Query Languages - York University

6 . 16 . 2

Example:

1 2 5 61 2 7 81 2 9 103 4 5 63 4 7 83 4 9 10

1 23 4

5 67 89 10

Page 17: Conceptual Query Languages - York University

7 . 1

Theta-Join

Take the product .Then apply to the results.

Thus, .

As with , can be any boolean-valued condition.Older versions of this allowed only where “ ” waslimited to “ ”, “ ”, etc. Hence, the name.

Page 18: Conceptual Query Languages - York University

7 . 2

Natural JoinA (very!) useful variant is called natural join.

This assumes for “ ” equalities between each pair ofattr's from the two tables with the same name.

And then only one copy of each such pair of equatedattr's is kept; that is, one copy of each such pair isprojected out.

Since the “ ” is understood, this is denoted as

Page 19: Conceptual Query Languages - York University

7 . 3

Example: natural join

pub beer priceJoe's Bud 2.50Joe's Molsen 2.75

Fox Bud 2.50Fox Molsen 3.50

pub addrJoe's Maple St

Fox River Rd

pub beer price addrJoe's Bud 2.50 Maple StJoe's Molsen 2.75 Maple St

Fox Bud 2.50 River RdFox Molsen 3.50 River Rd

Page 20: Conceptual Query Languages - York University

8 . 1

RenamingThe operator gives a new schema to a relation. It is away to “rename” a relation.

makes as a relation with attr's , …, , and the same tuples as .

Simplified notation. .

Page 21: Conceptual Query Languages - York University

8 . 2

Example: Renaming

pub addrJoe's Maple St

Fox River Rd

bar addrJoe's Maple St

Fox River Rd

Page 22: Conceptual Query Languages - York University

9 . 1

Building complex expressionsCombine operations with parentheses and precedencerules.

Three notations, as in arithmetic.

1. Sequences of assignment statements.

2. Expressions with several operators.

3. Expression trees.

Page 23: Conceptual Query Languages - York University

9 . 2

Sequences of assignmentsCreate temporary names.

Renaming can be done by giving relations lists of attr's.

Example: can be written as

Page 24: Conceptual Query Languages - York University

9 . 3

Expressions in a single assignmentExample. The theta-join can be writtenas .

Precedence of relational operators.

1. (highest)

2.

3.

4.

Page 25: Conceptual Query Languages - York University

9 . 4

Expression treesLeaves are operands; standing for relations.

Interior nodes are operators, applied to their child orchildren.

Page 26: Conceptual Query Languages - York University

9 . 5

Example: tree for a queryUsing the rel'ns and , find the names of all the pubs that are either on MapleSt or that sell Bud for less than $3.

Pub Sells

Page 27: Conceptual Query Languages - York University

Example: self-joinUsing the rel'n , find the pubs thatsell two different beers at the same price.

Strategy. By renaming, define a copy of — call it . The natural join of and

consists of the tuples of schema , , , such that the pub sells both beers at the same

price.

And select so that and are not the samebeer.

Page 28: Conceptual Query Languages - York University

9 . 6

The treeBeers the same price

Page 29: Conceptual Query Languages - York University

9 . 7

Sells

Sells

Page 30: Conceptual Query Languages - York University

10 . 1

Examples: Colour SchemaCustomer

cust# PKcname

fav_colourphone#

Itemitem# PKprod#cust#

colourdate_sold

FK to ProductFK to Customer

Productprod# PK

pname

makercost

FK to Company

Avail_Colourprod# PKcolour PK, FK to Company

Page 31: Conceptual Query Languages - York University

10 . 2

. Products in customer's favorite colour

Show, for each customer (reporting the customer'sname), the products by name that come in thecustomer's favourite colour.

Page 32: Conceptual Query Languages - York University

10 . 3

. Products not in customer's favorite colour

Show, for each customer (reporting the customer'sname), the products by name that do not come in thecustomer's favourite colour.

Page 33: Conceptual Query Languages - York University

10 . 4

. Two or more in commonList pairs of customers — with columns , , , —such that the two customers own at least two products in common.

Write your query so that a pair is not listed twice. For instance, if is listed,then should not be.

Page 34: Conceptual Query Languages - York University

10 . 5

. Customers with all coloursList customers who own items in all the available colours. That is, for every available colour,the customer owns some item in that colour.

Page 35: Conceptual Query Languages - York University

10 . 6

. Most expensive itemsList each customer by name, paired with the product(s) by name that he or she has bought thatwas the most expensive ( ) of all the products he or she has bought.

Note that there actually may be ties. For instance, and wouldboth qualify if both were 80,000, and for everything else he has bought, each item was lessexpensive than 80,000.

Page 36: Conceptual Query Languages - York University

11 . 1

Bag vs set semanticsRelational algebra (RA) is usually considered with setsemantics; that is, each operator returns a set of tuples.

Thus, there are no duplicate tuples in the return.

But we can interpret RA with a bag (multi-set) semantics

instead, if we wanted. Then duplicate tuples can be

returned in the answer bag.

How would this change our different operators?

Page 37: Conceptual Query Languages - York University

11 . 2

RA operators w/ bag semanticsselect (“ ”). Selects from the input, as before. Can only return duplicates if the input table hasduplicates.project (“ ”). Now returns exactly the same number oftuples as the input table.product (“ ”). Each tuple of rel'n #1 is concatenatedwith each tuple of rel'n #2, as before. Can only return duplicates if the input tables haveduplicates.join (“ ”). Defined via and as before.

Page 38: Conceptual Query Languages - York University

11 . 3

RA “set” operators w/ bag semantics

intersection (“ ”). Given tuple appears in rel'n #1 times and in rel'n #2 times, then appears in theresult times.

union (“ ”). Given tuple appears in rel'n #1 timesand in rel'n #2 times, then appears in the result

times.

minus (“ ”). Say we have . Given tuple appears in times and in times, if , then appears in the result times; else, does notappear in the result.

Page 39: Conceptual Query Languages - York University

12 . 1

DeclarativeBut…RA is not “declarative”. We are having to specifythe order of our operations, to say how the query is to beevaluated.

In a declarative query language, we (ideally) just specifywhat we want, and not how to obtain it.

Would a declarative query language be possible? Yes.

relational calculusdatalogSQL

Page 40: Conceptual Query Languages - York University

12 . 2

Relational calculusA query is stated in predicate calculus in set definitionform.

I.e.,

Rel'ns are just predicates.

The logical variables range either over

attributes' domains (domain relational calculus) or

relations' tuples (tuple relational calculus).

Page 41: Conceptual Query Languages - York University

12 . 3

DatalogQuite similar to the domain relational calculus, but anicer syntax.Parallels the programming language Prolog.Used widely in academic research in databases and inAI.E.g. “Which students were enroled in EECS-3421 andearned an ‘A’?”

Page 42: Conceptual Query Languages - York University

Relational Algebra | EECS-3421-A: Introduction to Database Systems | Fall 2016 12 . 4

SQL: “Intergalatic Data Speak”Declaractive!RA principle of “tables in, tables out.”“Logical variables” that range over tuples. (So, an implementation of tuple relational calculus.)Built-in choice of set or bag semantics.Handles null “values”.Meant to look so much like English that anyone canwrite queries.Plus lots, lots more! (Is a standard.)

Parke's thoughts. One of the ugliest languages ever!