{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# 7-preview -   Query Languages for Relational DBs SQL ...

This preview shows pages 1–5. Sign up to view the full content.

1/16/10 1 Query Languages for Relational DBs ECS‐165A WQ'10 1 SQL Prac3cal defini3on of rela3onal database Operates on tables (with duplicates ‐‐ bags) Rela)onal Algebra Mathema3cal defini3on of rela3onal database Operates on rela3ons (i.e., sets ) Various keywords, statements SELECT, FROM, WHERE, … Set‐based opera3ons Intersec3on, Union, … “cross‐fer3liza3on” Commercial DBMSs – Sets or Bags? The default is to produce a bag (or multiset) of rows as a query answer If you want a set, use DISTINCT Why do you think they do this? Note that even though relational algebra was originally defined as set-based, SQL queries are represented internally using relational algebra (w/ extra operators) There are also versions of relational algebra defined using bag semantics ECS‐165A WQ'10 2

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
1/16/10 2 The Plan … Present mathematical definition of a relational database (relational algebra) Intermix relational algebra and SQL We’ll use relational algebra again when we talk about evaluating and optimizing queries ECS‐165A WQ'10 3 Mathematically Describing a Relational DB A relation is a set of tuples See the original definition of the model … optional reading (Codd 1970) Define query operators as set-theoretic functions Together these form the relational algebra ECS‐165A WQ'10 4
1/16/10 3 Cross Products Let A = {a, b, c} and B = {1, 2} In set theory, the cross product is defined as A X B = {( a , 1 ), ( b , 1 ), ( c , 1 ), ( a , 2 ), ( b , 2 ), ( c , 2 )} A X B is a set consisting of ordered pairs (2-tuples) where each pair consists of an element from A and an element from B ECS‐165A WQ'10 5 Practice Question Suppose A = {a, b, c} and B = {1, 2} What is B X B? B X B = {( 1 , 1 ), ( 2 , 1 ), ( 1 , 2 ), ( 2 , 2 )} ECS‐165A WQ'10 6

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
1/16/10 4 Defining Relations Suppose we have the relation Person(name, salary, num, status) with domains NameValues = {all possible strings of 30 characters} SalValues = {real numbers between 0 and 100,000} StatusValues = {“f”, “p”} NumValues = {integers between 0 and 9999} Any instance of the relation is always a subset ( ) of NameValues X SalValues X NumValues X StatusValues * Note that a “domain” is a set of simple, atomic values ECS‐165A WQ'10 7 Defining Relations Each relation instance is a subset of the cross product of its domains (see the Codd paper) One element of a relation is called a tuple If n domains, then n-tuples A relation is always a set by definition
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}