Download International Computer Institute, Izmir, Turkey SQL, MySQL and PHP

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
International Computer Institute, Izmir, Turkey
SQL
Asst.Prof.Dr.İlker Kocabaş
UBİ502 at http://ube.ege.edu.tr/~ikocabas
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
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.2 of 48
Modifications & additions by Cengiz Güngör
Schema Diagram for the Banking Enterprise
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.3 of 48
Modifications & additions by Cengiz Güngör
Joins
 Join types:
 Inner join
 Left outer join
 Right outer join
 Full outer join
 Join conditions:
 Natural
 On <predicate>
 Using (A1, …, An)
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.4 of 48
Modifications & additions by Cengiz Güngör
The loan and borrower Relations
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.5 of 48
Modifications & additions by Cengiz Güngör
loan INNER JOIN borrower
ON loan.loan-number = borrower.loan-number
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.6 of 48
Modifications & additions by Cengiz Güngör
loan LEFT OUTER JOIN borrower
USING (loan-number)
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.7 of 48
Modifications & additions by Cengiz Güngör
Join Conditions
 a INNER JOIN b USING (c1, ..., cn)
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.c1 = b.c1, …, a.cn = b.cn)
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.8 of 48
Modifications & additions by Cengiz Güngör
Join Conditions
 Suppose (c1, …, cn) is a complete list of attributes
common to A and B
 Then:
a INNER JOIN b USING (c1, …, cn)
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;
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.9 of 48
Modifications & additions by Cengiz Güngör
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”
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.10 of 48
Modifications & additions by Cengiz Güngör
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)
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.11 of 48
Modifications & additions by Cengiz Güngör
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 |
+-------------+-------------+--------+
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.12 of 48
Modifications & additions by Cengiz Güngör
Example 2 (cont)
g max(amount)
(branch-name = “Perryridge” (loan))
mysql> SELECT MAX(amount) FROM loan
-> WHERE branch_name = "Perryridge";
+-------------+
| max(amount) |
+-------------+
|
1500 |
+-------------+
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.13 of 48
Modifications & additions by Cengiz Güngör
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 |
+------+
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.14 of 48
Modifications & additions by Cengiz Güngör
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";
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.15 of 48
Modifications & additions by Cengiz Güngör
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 |
+-------------+-------------+--------+------+
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.16 of 48
Modifications & additions by Cengiz Güngör
Example 2 (cont)
(amount (amount > t1.max (loan))
mysql> SELECT amount FROM loan, t1
-> WHERE amount > t1.max;
+--------+
| amount |
+--------+
|
2000 |
|
7500 |
+--------+
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.17 of 48
Modifications & additions by Cengiz Güngör
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
|
+---------------+
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.18 of 48
Modifications & additions by Cengiz Güngör
Example 3
Query: What is the average loan amount held by customers in each
city?
branch-city
g avg(amount) (loan
⋈ branch)
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.19 of 48
Modifications & additions by Cengiz Güngör
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
| 900000 |
| L-23
| Redwood
|
2000 | Redwood
| Palo Alto
| 2100000 |
| L-15
| Perryridge |
1500 | Perryridge | Horseneck
| 1700000 |
| L-14
| Downtown
|
1500 | Downtown
| Brooklyn
| 900000 |
| L-93
| Mianus
|
500 | Mianus
| Horseneck
| 400200 |
| L-11
| Round Hill |
900 | Round Hill | Horseneck
| 8000000 |
| L-16
| Perryridge |
1300 | Perryridge | Horseneck
| 1700000 |
| L-20
| North Town |
7500 | North Town | Rye
| 3700000 |
| L-21
| Central
|
570 | Central
| Rye
| 400280 |
+-------------+-------------+--------+-------------+-------------+---------+
 Would like to see just the branch_name and branch_city columns
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.20 of 48
Modifications & additions by Cengiz Güngör
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
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.21 of 48
Modifications & additions by Cengiz Güngör
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
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.22 of 48
Modifications & additions by Cengiz Güngör
Example 3 (cont)
mysql> select branch_city, avg(amount)
-> from loan natural join branch
-> group by branch_city;
+-------------+-------------+
| branch_city | avg(amount) |
+-------------+-------------+
| Brooklyn
|
1250.0000 |
| Horseneck
|
1050.0000 |
| Palo Alto
|
2000.0000 |
| Rye
|
4035.0000 |
+-------------+-------------+
 Suppose we wanted to impose a constraint on the result – use a
HAVING clause
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.23 of 48
Modifications & additions by Cengiz Güngör
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 | 2000.0000 |
| Rye
| 4035.0000 |
+-----------+-----------+
 Note: we also renamed the columns
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.24 of 48
Modifications & additions by Cengiz Güngör
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;
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.25 of 48
Modifications & additions by Cengiz Güngör
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
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.26 of 48
Modifications & additions by Cengiz Güngör
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
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.27 of 48
Modifications & additions by Cengiz Güngör
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:
⋈θ
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.28 of 48
Modifications & additions by Cengiz Güngör
READING
MySQL manual chapter 3:
Tutorial Introduction
http://www.mysql.com/documentation/mysql/
bychapter/manual_Tutorial.html
UBI 502
Database Management Systems
©Silberschatz, Korth and Sudarshan
5.29 of 48
Modifications & additions by Cengiz Güngör