International Computer Institute, Izmir, Turkey SQL Asst.Prof.Dr.İlker Kocabaş UBİ502 at

Slides:



Advertisements
Similar presentations
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 5: Other Relational.
Advertisements

Ver 1,12/09/2012Kode :CCs 111,sistem basisdataFASILKOM Chapter 2: Relational Model Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
E-R Diagram for a Banking Enterprise
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
©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.
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.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Query by example (based on notes by Silberchatz,Korth, and Sudarshan.
CMSC424: Database Design Instructor: Amol Deshpande
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
MySQL Tutorial (2) Introduction to Database. Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-city)
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
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.
©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.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE) Datalog.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
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.
Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to the following schema diagram.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all tuples from the loan relation. delete.
©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.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational Calculus Domain Relational.
Computing & Information Sciences Kansas State University Monday, 08 Sep 2008CIS 560: Database System Concepts Lecture 5 of 42 Monday, 08 September 2008.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Relational Model.
Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Example.
Computing & Information Sciences Kansas State University Monday, 17 Sep 2007CIS 560: Database System Concepts Lecture 12 of 42 Monday, 18 February 2008.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 5 Other Relational Languages.
Chapter 2: Relational Model II Relational Algebra basics Relational Algebra basics.
2.1 Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Database System Concepts, 5th Ed. Bin Mu at Tongji University Chapter 5: Other Relational Languages.
©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.
Relational Algebra HW2 Turn in as a hardcopy at the start of next class period. You may work this assignment in groups.
ICOM 5016 – Introduction to Database Systems Lecture 6 Dr. Manuel Rodriguez Department of Electrical and Computer Engineering University of Puerto Rico,
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.
ASET Relational Algebra continues…. ASET Rename Operation Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
IS 230Lecture 7Slide 1 Relational Algebra Lecture 8 Text Book – Chapter.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
International Computer Institute, Izmir, Turkey Relational Model Asst.Prof.Dr.İlker Kocabaş UBİ502 at
PMIT-6102 Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
Database System Concepts ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan Chapter 2: Relational Model.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
International Computer Institute, Izmir, Turkey Relational Model Asst.Prof.Dr.İlker Kocabaş UBİ502 at
Relational Algebra.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
הקדמה היתרון במודל הטבלאי: המודל כקבוצה של יחסים (טבלאות)
Lecture 4 of 42 Relational Joins Wednesday, 30 January 2008
Introduction to Database
Relational Model B.Ramamurthy 5/28/2019 B.Ramamurthy.
Presentation transcript:

International Computer Institute, Izmir, Turkey SQL Asst.Prof.Dr.İlker Kocabaş UBİ502 at

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.2 of 48 UBI 502 Database Management Systems Overview Joins: types and conditions Example 1: our languages  relational algebra  tuple relational calculus  domain relational calculus  SQL Examples 2, 3  joins, aggregates, MySQL Join tricks

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.3 of 48 UBI 502 Database Management Systems Schema Diagram for the Banking Enterprise

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.4 of 48 UBI 502 Database Management Systems Joins Join types:  Inner join  Left outer join  Right outer join  Full outer join Join conditions:  Natural  On  Using (A 1, …, A n )

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.5 of 48 UBI 502 Database Management Systems The loan and borrower Relations

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.6 of 48 UBI 502 Database Management Systems loan INNER JOIN borrower ON loan.loan-number = borrower.loan-number

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.7 of 48 UBI 502 Database Management Systems loan LEFT OUTER JOIN borrower USINGloan-number) loan LEFT OUTER JOIN borrower USING (loan-number)

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.8 of 48 UBI 502 Database Management Systems Join Conditions a INNER JOIN b USING (c 1,..., c n ) SELECT * FROM loan INNER JOIN borrower USING (loan_number); | loan_number | branch_name | amount | customer_name | loan_number | | L-16 | Perryridge | 1300 | Adams | L-16 | | L-93 | Mianus | 500 | Curry | L-93 |... | L-17 | Downtown | 1000 | Williams | L-17 | Equivalent: a INNER JOIN b ON (a.c 1 = b.c 1, …, a.c n = b.c n )

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.9 of 48 UBI 502 Database Management Systems Join Conditions Suppose (c 1, …, c n ) is a complete list of attributes common to A and B Then: a INNER JOIN b USING (c 1, …, c n ) is equivalent to: a NATURAL JOIN b E.g. since there is only one common attribute between loan and borrower, the following queries give the same result: SELECT * FROM loan INNER JOIN borrower USING (loan_number); SELECT * FROM loan NATURAL JOIN borrower;

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.10 of 48 UBI 502 Database Management Systems Example 1: Our languages Query: What branches are in Brooklyn?  branch-name (  branch-city = “Brooklyn” (branch)) {t |  s  branch (t[branch-name] = s[branch-name]  s [branch-city] = “Brooklyn”)} {  n  |  c, a (  n, c, a   branch  c = “Brooklyn”)} SELECT branch-name FROM branch WHERE branch_city = “Brooklyn”

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.11 of 48 UBI 502 Database Management Systems Example 2 Who has a loan with a greater value than every loan at Perryridge branch? t1 ←  mp(max) ( g max(amount) (  branch-name = “Perryridge” (loan))) (  customer-name (  loan-number (  amount > t1.max (loan)) ⋈ borrower)

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.12 of 48 UBI 502 Database Management Systems Example 2 (cont)  branch-name = “Perryridge” (loan) mysql> SELECT * FROM loan -> WHERE branch_name = "Perryridge"; | loan_number | branch_name | amount | | L-15 | Perryridge | 1500 | | L-16 | Perryridge | 1300 |

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.13 of 48 UBI 502 Database Management Systems Example 2 (cont) g max(amount) (  branch-name = “Perryridge” (loan)) mysql> SELECT MAX(amount) FROM loan -> WHERE branch_name = "Perryridge"; | max(amount) | | 1500 |

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.14 of 48 UBI 502 Database Management Systems Example 2 (cont)  mp(max) ( g max(amount) (  branch-name = “Perryridge” (loan))) mysql> SELECT MAX(amount) AS max FROM loan -> WHERE branch_name = "Perryridge"; | max | | 1500 |

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.15 of 48 UBI 502 Database Management Systems Example 2 (cont) t1 ←  mp(max) ( g max(amount) (  branch-name = “Perryridge” (loan))) mysql> CREATE TEMPORARY TABLE t1 -> SELECT MAX(amount) AS max FROM loan -> WHERE branch_name = "Perryridge";

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.16 of 48 UBI 502 Database Management Systems Example 2 (cont)  amount > t1.max (loan) mysql> SELECT * FROM loan, t1 -> WHERE amount > t1.max; | loan_number | branch_name | amount | max | | L-23 | Redwood | 2000 | 1500 | | L-20 | North Town | 7500 | 1500 |

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.17 of 48 UBI 502 Database Management Systems Example 2 (cont) (  amount (  amount > t1.max (loan)) mysql> SELECT amount FROM loan, t1 -> WHERE amount > t1.max; | amount | | 2000 | | 7500 |

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.18 of 48 UBI 502 Database Management Systems Example 2 (cont)  customer-name (  amount (  amount > t1.max (loan)) ⋈ borrower) mysql> SELECT customer_name -> FROM loan NATURAL JOIN borrower, t1 -> WHERE amount > t1.max; | customer_name | | McBride | | Smith |

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.19 of 48 UBI 502 Database Management Systems Example 3 Query: What is the average loan amount held by customers in each city? branch-city g avg(amount) (loan ⋈ branch)

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.20 of 48 UBI 502 Database Management Systems Example 3 (cont) mysql> select * -> from loan natural join branch; | loan_number | branch_name | amount | branch_name | branch_city | assets | | L-17 | Downtown | 1000 | Downtown | Brooklyn | | | L-23 | Redwood | 2000 | Redwood | Palo Alto | | | L-15 | Perryridge | 1500 | Perryridge | Horseneck | | | L-14 | Downtown | 1500 | Downtown | Brooklyn | | | L-93 | Mianus | 500 | Mianus | Horseneck | | | L-11 | Round Hill | 900 | Round Hill | Horseneck | | | L-16 | Perryridge | 1300 | Perryridge | Horseneck | | | L-20 | North Town | 7500 | North Town | Rye | | | L-21 | Central | 570 | Central | Rye | | Would like to see just the branch_name and branch_city columns

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.21 of 48 UBI 502 Database Management Systems Example 3 (cont) mysql> select branch_city, amount -> from loan natural join branch; | branch_city | amount | | Brooklyn | 1000 | | Palo Alto | 2000 | | Horseneck | 1500 | | Brooklyn | 1500 | | Horseneck | 500 | | Horseneck | 900 | | Horseneck | 1300 | | Rye | 7500 | | Rye | 570 | Would like to see this table sorted by branch_city

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.22 of 48 UBI 502 Database Management Systems Example 3 (cont) mysql> select branch_city, amount -> from loan natural join branch -> order by branch_city; | branch_city | amount | | Brooklyn | 1000 | | Brooklyn | 1500 | | Horseneck | 1300 | | Horseneck | 1500 | | Horseneck | 500 | | Horseneck | 900 | | Palo Alto | 2000 | | Rye | 7500 | | Rye | 570 | Next: compute average for each group

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.23 of 48 UBI 502 Database Management Systems Example 3 (cont) mysql> select branch_city, avg(amount) -> from loan natural join branch -> group by branch_city; | branch_city | avg(amount) | | Brooklyn | | | Horseneck | | | Palo Alto | | | Rye | | Suppose we wanted to impose a constraint on the result – use a HAVING clause

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.24 of 48 UBI 502 Database Management Systems Example 3 (cont) mysql> select branch_city as city, avg(amount) as average -> from loan natural join branch -> group by branch_city -> having (avg(amount) > 1500); | city | average | | Palo Alto | | | Rye | | Note: we also renamed the columns

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.25 of 48 UBI 502 Database Management Systems Join tricks Restrict a to just those rows compatible with b  SELECT DISTINCT a.* FROM a NATURAL JOIN b; E.g. list tuples from the borrower table only for those borrowers who are depositors:  SELECT DISTINCT borrower.* FROM borrower NATURAL JOIN depositor;

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.26 of 48 UBI 502 Database Management Systems Join tricks (cont) Compute set difference without using EXCEPT (MySQL 3.23 doesn’t have set operations) i.e.remove from a those tuples compatible with b  SELECT DISTINCT a.* FROM a NATURAL LEFT OUTER JOIN b WHERE b.x is NULL E.g. list those tuples from the borrower table only for those borrowers who are not depositors:  SELECT DISTINCT borrower.* FROM borrower NATURAL LEFT OUTER JOIN depositor WHERE depositor.customer_name is NULLS

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.27 of 48 UBI 502 Database Management Systems Review A NATURAL JOIN uses common column headings, and it is not possible to attach an ON or USING clause An INNER JOIN results in a table where each tuple is a combination of information from both argument tables An OUTER JOIN is an INNER JOIN padded with additional tuples The ON clause lists common attributes which must be equal on both sides of the join The USING clause permits us to enforce equality between attributes having different names, e.g. ON a.x = b.y It also permits us to use inequalities, e.g. ON a.x > b.y

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.28 of 48 UBI 502 Database Management Systems PS: Theta Join A ⋈ C B ≝  C (A ⋈ B) For notational convenience, we permit ourselves to write a select condition on the join operator This is called “theta join” because the condition is sometimes represented as a theta subscript: ⋈ θ

©Silberschatz, Korth and Sudarshan Modifications & additions by Cengiz Güngör 5.29 of 48 UBI 502 Database Management Systems READING MySQL manual chapter 3: Tutorial Introduction bychapter/manual_Tutorial.html