7 - 1/21/10
 Query Languages for Relational DBs SQL


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

View Full Document Right Arrow Icon
1/21/10 1 Query Languages for Relational DBs ECS‐165A WQ'10 1 SQL Prac4cal defni4on oF rela4onal database Operates on tables (with duplicates ‐‐ bags) Rela)onal Algebra Mathema4cal defni4on oF rela4onal database Operates on rela4ons (i.e., sets ) Various keywords, statements SELECT, ±ROM, WHERE, … Set‐based opera4ons Intersec4on, Union, … “cross‐Fer4liza4on” 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
1/21/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
Background image of page 2
1/21/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
Background image of page 3

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

View Full DocumentRight Arrow Icon
1/21/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
Background image of page 4
Image of page 5
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 - 1/21/10
 Query Languages for Relational DBs SQL


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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online