©Silberschatz, Korth and Sudarshan4.1Database System Concepts Null Values It is possible for tuples to have a null value for some of their attributes The.

Slides:



Advertisements
Similar presentations
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
Advertisements

Chapter 4: Immediate SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined.
E-R Diagram for a Banking Enterprise
SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
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 Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL Database Management Systems I Alex Coman, Winter 2006.
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 7.
Database Design and Implementation (including SQL) Also see MS Access notes: Access: Part1, Part2, Part3, Part4Part1Part2Part3Part4.
©Silberschatz, Korth and Sudarshan1.1Database System Concepts - 5 th Edition, May 23, 2005 Introduction to Database System Concepts.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
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 Aandachtspunten Deadlines – alles op tijd inleveren! n Lees goed wat van je verwacht wordt!
MySQL Tutorial (2) Introduction to Database. Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-city)
SQL Lecture 3 SQL Lecture 3. 2 SQL SQL Data Definition Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex.
CIS552SQL1 Data Definition Language Insertions Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations.
Dr. Kalpakis CMSC 461, Database Management Systems URL: SQL.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. Bin Mu at Tongji University Chapter 3: SQL.
-- Introduction to database principles Maoying Wu March 11, 2013 Chapter 3: Basic SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
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.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
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 Model: Examples. Banking Example branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) account.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts (Schema) Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested.
Advanced SQL Murat Kantarcioglu Adapted from Silberchatz et al. slides.
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.
Computing & Information Sciences Kansas State University Monday, 04 Feb 2008CIS 560: Database System Concepts Lecture 6 of 42 Monday, 04 February 2008.
Chapter 3: SQL. 3.2Unite International CollegeDatabase Management Systems Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate.
Computing & Information Sciences Kansas State University Monday, 08 Sep 2008CIS 560: Database System Concepts Lecture 5 of 42 Monday, 08 September 2008.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts, 5 th Edition, Oct 5, 2006 SQL Data Definition Basic Query Structure Set Operations Aggregate.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com ICOM 5016 – Introduction.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Computing & Information Sciences Kansas State University Wednesday, 05 Sep 2007CIS 560: Database System Concepts Lecture 6 of 42 Wednesday, 05 September.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
©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.
MySQL Tutorial Introduction to Database. Introduction of MySQL  MySQL is an SQL (Structured Query Language) based relational database management system.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Nested Subqueries in SQL By Kenneth Cheung CS 157A Section 2 Professor Lee.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU 3-1 Introduction to Database CHAPTER 3 SQL Data Definition Basic.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL Bambang.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Chapter 5: SQL. Data Definition Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex Queries Views Modification.
Database System Concepts ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
SQL and Relational Algebra
Chapter 3: SQL.
Module 4: SQL.
Chapter 3: SQL.
Structured Query Language (SQL)
Chapter 3: SQL.
Chapter 3: SQL.
Introduction to Database
Presentation transcript:

©Silberschatz, Korth and Sudarshan4.1Database System Concepts Null Values It is possible for tuples to have a null value for some of their attributes The predicate ‘is null’ can be used to check for null values.  E.g. Find all loan number which appear in the loan relation with null values for amount. select loan-number from loan where amount is null The result of any arithmetic expression involving null is null  E.g. 5 + null returns null

©Silberschatz, Korth and Sudarshan4.2Database System Concepts Null Values and Three Valued Logic Any comparison with null returns unknown  E.g. 5 null or null = null Three-valued logic using the truth value unknown  OR: (unknown or true) = true (unknown or false) = unknown (unknown or unknown) = unknown  AND:(true and unknown) = unknown (false and unknown) = false (unknown and unknown) = unknown  NOT:(not unknown) = unknown  “P is unknown” If predicate P evaluates to unknown  true where clause predicate  If evaluates to unknown  false

©Silberschatz, Korth and Sudarshan4.3Database System Concepts Null Values and Aggregates Total all loan amounts select sum (amount) from loan  Above statement ignores null amounts  Result is null  If there is no non-null amount All aggregate operations except count(*) ignore tuples with null values on the aggregated attributes. The count of an empty collection is defined to be 0. All other aggregates return null value.

©Silberschatz, Korth and Sudarshan4.4Database System Concepts Nested Subqueries SQL provides a mechanism for the nesting of subqueries. A subquery is a select-from-where expression that is nested within another query. A common use of subqueries is to perform tests for  Set membership  Set comparisons  Set cardinality

©Silberschatz, Korth and Sudarshan4.5Database System Concepts Set Membership Find all customers who have both an account and a loan at the bank. select distinct customer-name from borrower where customer-name in (select customer-name from depositor) Find all customers who have a loan at the bank but do not have an account at the bank select distinct customer-name from borrower where customer-name not in (select customer-name from depositor)

©Silberschatz, Korth and Sudarshan4.6Database System Concepts Membership in Arbitrary Relation Find all customers who have both an account and a loan at the Perryridge branch select distinct customer-name from borrower, loan where borrower.loan-number = loan.loan-number and branch-name = “Perryridge” and (branch-name, customer-name) in (select branch-name, customer-name from depositor, account where depositor.account-number = account.account-number) in and not in can used on enumerate sets. Find all customers who have a loan at the bank, and whose names are neither Smith nor Jones select distinct customer-name from borrower where customer-name not in (‘Smith’, ‘Jones’)

©Silberschatz, Korth and Sudarshan4.7Database System Concepts Set Comparison Find all branches that have greater assets than some branch located in Brooklyn. select distinct T.branch-name from branch as T, branch as S where T.assets > S.assets and S.branch-city = ‘ Brooklyn ’  using ‘> some’ clause select branch-name from branch where assets > some (select assets from branch where branch-city = ‘ Brooklyn ’ )

©Silberschatz, Korth and Sudarshan4.8Database System Concepts Definition of Some Clause F some r  t  r  s.t. (F t) where can be =, =, <> ‘= some’ is identical to in ‘<> some’ is not the same as not in (5< some) = true ) = false (5 <> some ) = true (5< some ) = true (5 = some

©Silberschatz, Korth and Sudarshan4.9Database System Concepts Definition of All Clause F all r  t  r  (F t) ‘<> all’ is identical to not in (5< all) = false ) = true (5  all ) = true (5< all ) = false (5 = all

©Silberschatz, Korth and Sudarshan4.10Database System Concepts Example Query Find the names of all branches that have greater assets than all branches located in Brooklyn. select branch-name from branch where assets > all (select assets from branch where branch-city = ‘Brooklyn’)

©Silberschatz, Korth and Sudarshan4.11Database System Concepts Test for Empty Relations The exists construct returns the value true if the argument subquery is nonempty. exists r  r  Ø not exists r  r = Ø Find all customers who have both an account and a loan at the bank select customer-name from borrower where exists (select * from depositor where depositor.customer-name = borrower.customer-name)

©Silberschatz, Korth and Sudarshan4.12Database System Concepts Example Query Find all customers who have an account at all branches located in Brooklyn. select distinct S.customer-name from depositor as S where not exists ( (select branch-name from branch where branch-city = ‘Brooklyn’) except (select R.branch-name from depositor as T, account as R where T.account-number = R.account-number and S.customer-name = T.customer-name))

©Silberschatz, Korth and Sudarshan4.13Database System Concepts Test for Absence of Duplicate Tuples The unique construct tests whether a subquery has any duplicate tuples in its result.

©Silberschatz, Korth and Sudarshan4.14Database System Concepts Example Query Find all customers who have at most one account at the Perryridge branch. select T.customer-name from depositor as T where unique ( select R.customer-name from account, depositor as R where T.customer-name = R.customer-name and R.account-number = account.account-number and account.branch-name = ‘Perryridge’) Find all customers who have at least two accounts at the Perryridge branch. select distinct T.customer-name from depositor T where not unique ( select R.customer-name from account, depositor as R where T.customer-name = R.customer-name and R.account-number = account.account-number and account.branch-name = ‘ Perryridge ’ )

©Silberschatz, Korth and Sudarshan4.15Database System Concepts Views Provide a mechanism to hide certain data from the view of certain users. To create a view: create view v as where  : any legal expression  v : view name To delete a view: drop view v

©Silberschatz, Korth and Sudarshan4.16Database System Concepts Example Query A view consisting of branches and their customers create view all-customer as (select branch-name, customer-name from depositor, account where depositor.account-number = account.account-number) union (select branch-name, customer-name from borrower, loan where borrower.loan-number = loan.loan-number) Find all customers of the Perryridge branch select customer-name from all-customer where branch-name = ‘Perryridge’

©Silberschatz, Korth and Sudarshan4.17Database System Concepts Complex Queries Two ways of composing multiple SQL blocks to express a complex query:  derived relations  with clause

©Silberschatz, Korth and Sudarshan4.18Database System Concepts Derived Relations SQL allows a subquery expression to be used in the from clause Find the average account balance of those branches where the average account balance is greater than $1200. select branch-name, avg-balance from (select branch-name, avg (balance) from account group by branch-name) as result (branch-name, avg-balance) where avg-balance > 1200

©Silberschatz, Korth and Sudarshan4.19Database System Concepts With Clause with clause allows views to be defined locally to a query, rather than globally. Find all accounts with the maximum balance with max-balance(value) as select max(balance) from account select account-number from account, max-balance where account.balance = max-balance.value

©Silberschatz, Korth and Sudarshan4.20Database System Concepts Example Query Find all branches where the total account deposit is greater than the average of the total account deposits at all branches with branch-total (branch-name, value) as select branch-name, sum (balance) from account group by branch-name with branch-total-avg(value) as select avg (value) from branch-total select branch-name from branch-total, branch-total-avg where branch-total.value >= branch-total-avg.value