CMSC424: Database Design Instructor: Amol Deshpande

Slides:



Advertisements
Similar presentations
SQL Query Examples Database Management COP4540, SCS, FIU.
Advertisements

Ver 1,12/09/2012Kode :CCs 111,Sistem basis DataFASILKOM Chapter 3: SQL Bambang Irawan Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
Database Modifications CIS 4301 Lecture Notes Lecture /30/2006.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
E-R Diagram for a Banking Enterprise
BRANCH-SCHEME (BRANCH-NAME, ASSETS, BRANCH-CITY)
Relational Algebra – Basis for Relational Query Languages Based on presentation by Juliana Freire.
CMSC424, Spring 2005 CMSC424: Database Design Lecture 5.
Subqueries Example Find the name of the producer of ‘Star Wars’.
Instructor: Amol Deshpande  Data Models ◦ Conceptual representation of the data  Data Retrieval ◦ How to ask questions of the database.
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
Instructor: Amol Deshpande  Data Models ◦ Conceptual representation of the data  Data Retrieval ◦ How to ask questions of the database.
Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin.
CMSC424: Database Design Instructor: Amol Deshpande
Relational Algebra on Bags A bag is like a set, but an element may appear more than once. –Multiset is another name for “bag.” Example: {1,2,1,3} is a.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Outer Join Aggregate Functions.
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.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all account records at the Perryridge branch.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL Database Management Systems I Alex Coman, Winter 2006.
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 7.
CMSC424: Database Design Instructor: Amol Deshpande
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.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
CMSC424: Database Design Instructor: Amol Deshpande
MySQL Tutorial (2) Introduction to Database. Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-city)
Instructor: Mohamed Eltabakh
CMSC424: Database Design Instructor: Amol Deshpande
CIS552SQL1 Data Definition Language Insertions Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations.
SQL I. SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language”
SQL By: Toan Nguyen. Download Download the software at During the installation –Skip sign up for fast installation.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
3.1 Chapter 3: SQL. 3.2 Chapter 3: SQL Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex Queries Views Modification.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
Basic Structure Given sets A 1, A 2,..., A n a relation r is a subset of A 1 x A 2 x... x A n Thus a relation is a set of n-tuples (a 1, a 2,..., a n )
Structured Query Language 2 Presented by: Annisa, M.Kom. Source: Database System Concepts 5 th edition.
Relational Algebra Instructor: Mohamed Eltabakh 1.
SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language” (create.
Chapter 3: SQL. 3.2Unite International CollegeDatabase Management Systems Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Outer Join n An extension of the join operation that avoids loss.
Chapter 6: Formal Relational Query Languages. 6.2 Chapter 6: Formal Relational Query Languages Relational Algebra Tuple Relational Calculus Domain Relational.
Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture /03/2006.
Computing & Information Sciences Kansas State University Monday, 08 Sep 2008CIS 560: Database System Concepts Lecture 5 of 42 Monday, 08 September 2008.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
Referential Integrity checks, Triggers and Assertions Examples from Chapter 7 of Database Systems: the Complete Book Garcia-Molina, Ullman, & Widom.
© D. Wong Normalization  Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies.  Decompose relation.
More Relation Operations 2015, Fall Pusan National University Ki-Joune Li.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
Computing & Information Sciences Kansas State University Friday, 08 Sep 2006CIS 560: Database System Concepts Lecture 07 of 42 Tuesday, 30 January 2007.
Relational Algebra Instructor: Mohamed Eltabakh 1.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
SQL Exercises – Part I April
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems.
The Database Language SQL Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
Chap 5. The DB Language (SQL)
Introduction to Structured Query Language (SQL)
Instructor: Mohamed Eltabakh
More Relation Operations
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
Presentation transcript:

CMSC424: Database Design Instructor: Amol Deshpande

Today Mapping relational algebra to SQL Complex SQL Queries See “movies database sql queries” on class webpage for the actual queries SQL Formal Semantics Advanced SQL Features

SQL Query Examples Movie(title, year, length, inColor, studioName, producerC#) StarsIn(movieTitle, movieYear, starName) MovieStar(name, address, gender, birthdate) MovieExec(name, address, cert#, netWorth) Studio(name, address, presC#) Queries: Producer with maximum average length of movies Find producer of Star Wars. All producers of movies in which harrison ford stars

SQL Query Examples Movie(title, year, length, inColor, studioName, producerC#) StarsIn(movieTitle, movieYear, starName) MovieStar(name, address, gender, birthdate) MovieExec(name, address, cert#, netWorth) Studio(name, address, presC#) Queries: Find movie titles that appear more than once Find number of people 3 hops away from Kevin Bacon

More SQL Select * into temptable from X1, … Having WHERE is to FROM what HAVING is to GROUPBY

Duplicates By definition, relations are sets So  No duplicates allowed Problem: Not practical to remove duplicates after every operation Why ? So… SQL by default does not remove duplicates SQL follows bag semantics, not set semantics Implicitly we keep count of number of copies of each tuple

Formal Semantics of SQL RA can only express SELECT DISTINCT queries cnameccity Johnson Smith Johnson Smith Brighton Perry Brighton R.H. To express SQL, must extend RA to a bag algebra  Bags (aka: multisets) like sets, but can have duplicates e.g: {5, 3, 3} e.g: homes = Next: will define RA * : a bag version of RA

Formal Semantics of SQL: RA*  * p (r): preserves copies in r cnameccity Johnson Brighton cname Johnson Smith Johnson Smith e.g:  * city = Brighton (homes) =  * A1, …, An (r): no duplicate elimination e.g:  * cname (homes) =

= Formal Semantics of SQL: RA* r  * s : additive union AB ααββααααββαα AB ααβααβ AB βααβαα AB 1α AB 3α 4. r -* s: bag difference e.g: r -* s = s -* r = ** r s

** Formal Semantics of SQL: RA* r  * s: cartesian product ABC ααααββααααββ AB ααβααβ C +-+- 

Formal Semantics of SQL Query: SELECTa 1, ….., a n FROMr 1, ….., r m WHEREp Semantics:  * A1, …, An (  * p (r 1   * …   * r m ) )(1) Query: SELECT DISTINCT a 1, ….., a n FROMr 1, ….., r m WHEREp Semantics: What is the only operator to change in (1)?  A1, …, An (  * p (r 1   * …   * r m ) )(2)

Set/Bag Operations Revisited Set Operations UNION ≡ U INTERSECT ≡ ∩ EXCEPT ≡ - Bag Operations UNION ALL ≡ U* INTERSECT ALL ≡ ∩* EXCEPT ALL ≡ -* Duplicate Counting: Given m copies of t in r, n copies of t in s, how many copies of t in: r UNION ALL s? r INTERSECT ALL s? A: m + n A: min (m, n) r EXCEPT ALL s? A: max (0, m-n)

SQL: Summary ClauseEval Order Semantics (RA/RA*) SELECT [(DISTINCT)] FROM WHERE INTO GROUP BY HAVING ORDER BY AS UNION ALL UNION (similarly intersection, except)  (or  *)  *  *  Extended relational operator g  * Can’t express: requires ordered sets, bags  U* U

Next Database updates

Modification of the Database – Deletion Delete all account records at the Perryridge branch delete from account where branch-name = ‘Perryridge’ Delete all accounts at every branch located in Needham city. delete from account where branch-name in (select branch-name from branch where branch-city = ‘Needham’) delete from depositor where account-number in (select account-number from branch, account where branch-city = ‘Needham’ and branch.branch-name = account.branch-name)

Example Query Delete the record of all accounts with balances below the average at the bank. delete from account where balance < (select avg (balance) from account) Problem: as we delete tuples from deposit, the average balance changes Solution used in SQL: 1. First, compute avg balance and find all tuples to delete 2. Next, delete all tuples found above (without recomputing avg or retesting the tuples)

Modification of the Database – Insertion Add a new tuple to account insert into account values (‘A-9732’, ‘Perryridge’,1200) or equivalently insert into account (branch-name, balance, account- number) values (‘Perryridge’, 1200, ‘A-9732’) Add a new tuple to account with balance set to null insert into account values (‘A-777’,‘Perryridge’, null)

Modification of the Database – Updates Increase all accounts with balances over $10,000 by 6%, all other accounts receive 5%. Write two update statements: update account set balance = balance  1.06 where balance > update account set balance = balance  1.05 where balance  The order is important Can be done better using the case statement