From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.

Slides:



Advertisements
Similar presentations
Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
Advertisements

SQL Group Members: Shijun Shen Xia Tang Sixin Qiang.
1 Introduction to SQL Select-From-Where Statements Multirelation Queries Subqueries.
SQL Queries Principal form: SELECT desired attributes FROM tuple variables –– range over relations WHERE condition about tuple variables; Running example.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
SQL CSET 3300.
Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
1 Lecture 12: Further relational algebra, further SQL
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
IS698: Database Management Min Song IS NJIT. Overview  Query processing  Query Optmization  SQL.
Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.
SQL. 1.SQL is a high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
Relational Model and Relational Algebra Rose-Hulman Institute of Technology Curt Clifton.
Fall 2001Arthur Keller – CS 1806–1 Schedule Today (TH) Bags and SQL Queries. u Read Sections Project Part 2 due. Oct. 16 (T) Duplicates, Aggregation,
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
SQL SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
Creating Tables, Defining Constraints Rose-Hulman Institute of Technology Curt Clifton.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #3.
SQL I. SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language”
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
Chapter 6 Notes. 6.1 Simple Queries in SQL SQL is not usually used as a stand-alone language In practice there are hosting programs in a high-level language.
Relational Algebra Basic Operations Algebra of Bags.
SCUHolliday6–1 Schedule Today: u SQL Queries. u Read Sections Next time u Subqueries, Grouping and Aggregation. u Read Sections And then.
Databases : SQL-Introduction 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
1 IT 244 Database Management System Lecture 11 SQL Select-From-Where Statements Meaning of queries Subqueries Ref : -A First Course in Database System.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Onsdag The concepts in a relation data model SQL DDL DML.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
Computational Biology Dr. Jens Allmer Lecture Slides Week 6.
Databases 1 Second lecture.
NULL VALUES CHAPTER 5 (6/E) CHAPTER 8 (5/E) 1. LECTURE OUTLINE  Dealing with null values Three-valued logic Effects in WHERE clauses IS NULL Effects.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 Introduction to SQL Database Systems. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Introduction to SQL Introduction Select-From-Where Statements Queries over Several Relations Subqueries.
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
1. Chapter 2: The relational Database Modeling Section 2.4: An algebraic Query Language Chapter 5: Algebraic and logical Query Languages Section 5.1:
1 Database Design: DBS CB, 2 nd Edition SQL: Select-From-Where Statements & Multi-relation Queries & Subqueries Ch. 6.
Select-From-Where Statements Multirelation Queries Subqueries
Basic Operations Algebra of Bags
More SQL: Complex Queries,
Schedule Today: Jan. 28 (Mon) Jan. 30 (Wed) Next Week Assignments !!
Slides are reused by the approval of Jeffrey Ullman’s
CPSC-310 Database Systems
Computational Biology
Foreign Keys Local and Global Constraints Triggers
CPSC-310 Database Systems
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Introduction to Database Systems, CS420
CPSC-608 Database Systems
06a: SQL-1 The Basics– Select-From-Where
CS 440 Database Management Systems
CPSC-608 Database Systems
CPSC-310 Database Systems
IST 210: Organization of Data
CPSC-310 Database Systems
IT 244 Database Management System
Basic Operations Algebra of Bags
CPSC-608 Database Systems
CPSC-608 Database Systems
CPSC-608 Database Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton

Review – Relational Integrity  Entity Integrity Constraints: Primary key values cannot be null  Referential Integrity Constraints: Foreign key values must either:  Match the primary key values of some tuple, or  Be null

Review – Relational Algebra  Intersection: R1  R2  Union: R1  R2  Difference: R1 - R2  Selection:  BDATE < (EMPLOYEE)  Projection:  FNAME, BDATE (EMPLOYEE)  Theta-Join:  Natural Join: R1 * R2

Why Relational Algebra?  Foundational knowledge  Used by query optimizers Finer grained than SQL Can be formally reasoned about  Formal basis for semantics of SQL

Homework Problem 6.18  Parts a–d and g  Begin in class, may work in groups of 2–3 Please note your partners on the sheet

Sets versus Bags  Sets Order doesn’t matter No duplicates  Examples {1,2,3} = {2,1,3} {1,2}  {2} = {1,2}  Bags (or multi-sets) Order doesn’t matter Duplicates allowed  Examples {1,2,3} = {2,1,3} {1,2}  {2} = {1,2,2} {1,2,3} ≠ {1,2,2,3}

Why Bags? Efficiency!  Eliminating duplicates can be expensive  By default SQL uses bags

Bag Union  “Just dump all the elements into a single bag”  An element appears in the union of two bags the sum of the number of times it appears in each bag

Bag Intersection  “Whichever bag has the fewest, has the answer”  An element appears in the intersection of two bags the minimum number of times it appears in either.

Bag Difference  “Take elements out of the first if they’re in the second”  An element appears in the difference of two bags as many times as it appears in the first, minus the number of times it appears in the second, but no less than 0 times

Bag Selection AB R  Like set selection  But input and output can be bags

Bag Projection AB R  Unlike set project, can turn a set into a bag

Bag Theta-Join AB R  Pair each tuple of first table with each tuple of second  Check condition  Don’t eliminate duplicates BC S

Introducing SQL  Pronounced: “ess queue ell” Or “sequel”  Benefits: Designed for the relational model Easily optimized by DBMS Standard (well, sort of, a little bit, sometimes)

Running Example – The SodaBase  Soda(name, manf)  Rest(name, addr, contract)  Customer(name, addr, phone)  Likes(customer, soda)  Sells(rest, soda, price)  Frequents(customer, rest)

The Basic SQL Query  SELECT attributes FROM table WHERE condition  Semantics:  attributes (  condition (table))

Example  Find all the names of all sodas made by PepsiCo

Example  Find all the names of all sodas made by PepsiCo  SELECT name FROM Soda WHERE manf = 'PepsiCo'  (note single quotes)

Select *  For getting all attributes…  SELECT * FROM table WHERE condition  Semantics:  condition (table)

Example  SELECT * FROM Soda WHERE manf = 'PepsiCo'

Renaming Attributes  SELECT attribute1 AS newName1, … FROM table WHERE condition  Semantics:  newName1, … (  attribute1, … (  condition (table)))

Example  SELECT name AS soda, manf AS maker FROM Soda WHERE manf = 'PepsiCo'

Expressions in SELECT Clauses  Can use expressions on attributes in SELECT  SELECT f(attributes), … FROM table WHERE condition  More powerful than the relational algebra we’ve seen Would need functions on tuples

Example  Show selling prices in Yen

Example  Show selling prices in Yen  SELECT rest, soda, price * 115 AS priceInYen FROM Sells

Another Example: Constants  SELECT customer, 'likes Pepsi' AS promotion FROM Likes WHERE soda = 'Pepsi'

Example: Complex Conditions  Find the price that Joe’s Sushi charges for Pepsi

Example: Complex Conditions  Find the price that Joe’s Sushi charges for Pepsi  SELECT price FROM Sells WHERE rest = 'Joe''s Sushi' AND soda = 'Pepsi'  Note: Double apostrophe inside string AND, OR, NOT Case insensitive

Pattern Matching  WHERE clauses can compare string to pattern Attribute LIKE pattern Attribute NOT LIKE pattern  Pattern syntax: Pattern is a string % in string represents any number of characters _ in string represent any single character

Example  Find the customers with exchange 555, regardless of area code

Example  Find the customers with exchange 555, regardless of area code  SELECT name FROM Customer WHERE phone LIKE '%555-____' -- That’s four underscores

Dealing with Null  Why might a tuple have a null value?  SQL uses three-valued logic to handle null A boolean expression can be true, false, or unknown Comparison with null yields unknown instead of error WHERE clause must be true to match

Three-Valued Logic  True = 1  False = 0  Unknown = 1/2  x AND y = min(x, y)  x OR y = max(x, y)  NOT x = 1 - x

Consider  SELECT rest FROM Sells WHERE price = 2.00  If the Sells relation has the value:  Beware of nulls! restsodaprice Joe'sPepsinull

Combining Relations  List multiple tables in FROM  Use Relation.Attribute to distinguish  SELECT soda FROM Likes, Frequents WHERE Frequents.customer = Likes.customer AND rest = 'Joe''s'  Semantics:

Tuple Variables  Can distinguish two copies of same relation  Example: Find all pairs of sodas by the same manufacturer… Omitting trivial pairs like (Pepsi, Pepsi) Omitting permutations of same sodas by listing members of pair alphabetically

Solution  SELECT s1.name, s2.name FROM Soda s1, Soda s2 WHERE s1.manf = s2.manf AND s1.name < s2.name

The Story Thus Far  SELECT … FROM … WHERE  SELECT * …  SELECT Foo AS Bar …  SELECT expression …  SELECT … FROM … WHERE … LIKE …  SELECT … FROM Foo, Bar …  SELECT … FROM Foo f1, Foo f2 …