# 8 -   Query Languages for Relational DBs SQL ...

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

1/25/10 1 Query Languages for Relational DBs ECS‐165A WQ'10 1 SQL Prac4cal defini4on of rela4onal database Operates on tables (with duplicates ‐‐ bags) Rela)onal Algebra Mathema4cal defini4on of rela4onal database Operates on rela4ons (i.e., sets ) Various keywords, statements SELECT, FROM, 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 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/25/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/25/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/25/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 ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern