# 7-preview - 1/16/10  Query Languages for Relational DBs...

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 defni3on oF rela3onal database Operates on tables (with duplicates ‐‐ bags) Rela)onal Algebra Mathema3cal defni3on oF rela3onal database Operates on rela3ons (i.e., sets ) Various keywords, statements SELECT, ±ROM, 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 deFned as set-based, SQL queries are represented internally using relational algebra (w/ extra operators) There are also versions of relational algebra deFned 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 defnition 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 defnition oF the model … optional reading (Codd 1970) Defne 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 defned 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 deFnition Recall: If you add the element 2 to the set {1, 2, 3, 4}, then
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/06/2010 for the course CSE 302 taught by Professor Joel during the Summer '05 term at Punjab Engineering College.

### Page1 / 15

7-preview - 1/16/10  Query Languages for Relational DBs...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online