1 the oracle database system querying the data database course the hebrew university of jerusalem
Post on 21-Dec-2015
218 views
TRANSCRIPT
![Page 1: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/1.jpg)
1
The Oracle Database SystemQuerying the Data
Database CourseThe Hebrew University of Jerusalem
![Page 2: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/2.jpg)
2
Basic SQL Query
SELECT [Distinct] target-list
FROM from-list
WHERE condition;
•from-list: A list of relation names (possibly with a range-variable after each name)
•target-list: A list of fields of relations in relation-list
•condition: A boolean condition
•DISTINCT: Optional keyword to delete duplicates
![Page 3: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/3.jpg)
3
Basic SQL Query (continues)
SELECT [Distinct] target-list
FROM from-list
WHERE condition;
The result evaluation:
1. Compute the cross product of the tables in from-list.2. Delete all rows that do not satisfy condition.3. Delete all columns that do not appear in target-list.4. If Distinct is specified eliminate duplicate rows.
![Page 4: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/4.jpg)
4
Basic SQL Query (continues)
SELECT Distinct A1,…,An
FROM R1,…,Rm
WHERE C;
This translates to the expression in relational algebra:
A1,…,An (C(R1 x…x Rm))
![Page 5: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/5.jpg)
5
Example Tables Used
Reserves
sid bid Day
22
58
101
103
10/10/02
11/12/02
Sailors
sid sname rating age
22
31
58
Dustin
Lubber
Rusty
7
8
10
45.0
55.5
35.0
Boats
bid bname color
101
103
Nancy
Gloria
red
green
KeyKey
![Page 6: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/6.jpg)
6
Boat Names and Reservation Dates
SELECT bname,day
FROM Boats,Reserves
WHERE Boats.bid = Reserves.bid
![Page 7: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/7.jpg)
7
Boat Names and Reservation Dates
SELECT bname,day
FROM Boats,Reserves
WHERE Boats.bid = Reserves.bid
Reserves
sid bid day
22
22
101
101
10/10/02
10/10/02
58
58
103
103
11/12/02
11/12/02
Boats
bid bname color
101
103
Nancy
Gloria
red
green
101
103
Nancy
Gloria
red
green
![Page 8: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/8.jpg)
8
Boat Names and Reservation Dates
SELECT bname,day
FROM Boats,Reserves
WHERE Boats.bid = Reserves.bid
Reserves
sid bid day
22 101 10/10/02
58 103 11/12/02
Boats
bid bname color
101 Nancy red
103 Gloria green
![Page 9: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/9.jpg)
9
Boat Names and Reservation Dates
SELECT bname,day
FROM Boats,Reserves
WHERE Boats.bid = Reserves.bid
Reserves
day
10/10/02
11/12/02
Boats
bname
Nancy
Gloria
![Page 10: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/10.jpg)
10
Sailors Who Reserved Boat 103
SELECT sname
FROM Sailors, Reserves
WHERE Sailors.sid = Reserves.sid and
bid = 103;
sname
(Sailors.sid = Reserves.sid bid = 103 (Sailors x Reserves))
![Page 11: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/11.jpg)
11
Sailors Reserves
sid sname rating age sid bid day
22 Dustin 7 45.0 22 101 10/10/02
22 Dustin 7 45.0 58 103 11/12/02
31 Lubber 8 55.5 22 101 10/10/02
31 Lubber 8 55.5 58 103 11/12/02
58 Rusty 10 35.0 22 101 10/10/02
58 Rusty 10 35.0 58 103 11/12/02
Sailors x Reserves
Sailors Who Reserved Boat 103
![Page 12: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/12.jpg)
12
Sailors Reserves
sid sname rating age sid bid day
22 Dustin 7 45.0 22 101 10/10/02
22 Dustin 7 45.0 58 103 11/12/02
31 Lubber 8 55.5 22 101 10/10/02
31 Lubber 8 55.5 58 103 11/12/02
58 Rusty 10 35.0 22 101 10/10/02
58 Rusty 10 35.0 58 103 11/12/02
Sailors Who Reserved Boat 103
Sailors.sid = Reserves.sid bid = 103
![Page 13: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/13.jpg)
13
Sailors Reserves
sid sname rating age sid bid day
22 Dustin 7 45.0 22 101 10/10/02
22 Dustin 7 45.0 58 103 11/12/02
31 Lubber 8 55.5 22 101 10/10/02
31 Lubber 8 55.5 58 103 11/12/02
58 Rusty 10 35.0 22 101 10/10/02
58 Rusty 10 35.0 58 103 11/12/02
Sailors Who Reserved Boat 103
sname
![Page 14: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/14.jpg)
14
Range Variables
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid = R.sid and
R.bid = 103;
• Range variables are good style
• They are necessary if the same relation appears twice in the FROM clause
![Page 15: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/15.jpg)
15
A Few SELECT Options
Select all columns:SELECT *
FROM Sailors S;
Rename selected columns:SELECT S.sname AS Sailors_Name
FROM Sailors S;
![Page 16: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/16.jpg)
16
A Few SELECT Options Applying functions:
Mathematical manipulations:
SELECT (age-5)*2 FROM Sailors S;
Aggregate functions:
SELECT COUNT(*) FROM Sailors S;
SELECT MAX(age) FROM Sailors S;
![Page 17: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/17.jpg)
17
The WHERE Clause
Numerical and string comparison: !=,<>,=, <, >, >=, <=, between(between val1 AND val2)
String comparisson is according to the alphabetical order!
Logical components: AND, OR Null verification: IS NULL, IS NOT NULL
Example:SELECT sname
FROM Sailors
WHERE age>=40 AND rating IS NOT NULL ;
![Page 18: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/18.jpg)
18
The WHERE Clause (continues)
The LIKE operator: A pattern matching operator Basic format: colname LIKE pattern Example:
_ is a single character
% is 0 or more characters
SELECT sid FROM Sailors WHERE sname LIKE ‘B_%g’;
![Page 19: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/19.jpg)
19
Sailors Who’ve Reserved a Boat
SELECT sname
FROM Sailors S, Reserves R
WHERE S.sid = R.sid;
Would adding DISTINCT give a different result?
![Page 20: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/20.jpg)
20
Exercise Formulate a query that finds the names of
sailors who reserved a yellow boat.
Sailors
sid sname rating age
22
31
58
Dustin
Lubber
Rusty
7
8
10
45.0
55.5
35.0
Boats
bid bname color
101
103
Nancy
Gloria
red
green
Reserves
sid bid Day
22
58
101
103
10/10/02
11/12/02
![Page 21: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/21.jpg)
21
A Harder Exercise
1. Formulate a query that finds the bid of boats that are reserved in at least two different days
2. Fix the query you formulated to find the names of these boats
Hint: A relation may appear more than once in the FROM list…
![Page 22: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/22.jpg)
22
Union, Intersect and Except
Sqlplus supports the union, intersection and difference operators
The syntax: query1 UNION query2; query1 INTERSECT query2; query1 MINUS query2;
![Page 23: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/23.jpg)
23
Sailors who’ve reserved a red or green boat
SELECT S.sname
FROM Sailors S, Boats B, Reserves R
WHERE S.sid = R.sid and R.bid = B.bid and (B.color = ‘red’ or
B.color = ‘green’);
sname
(color = ‘red’ color = ‘green’
(Sailors ⋈ Reserves ⋈ Boats))
What would happen if we replaced or by and ?
![Page 24: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/24.jpg)
24
Sailors who’ve reserved red or green boat
SELECT S.sname
FROM Sailors S, Boats B, Reserves R
WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’
UNION
SELECT S.sname
FROM Sailors S, Boats B, Reserves R
WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘green’;
![Page 25: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/25.jpg)
25
The Second Version in Relational Algebra
sname
(color = ‘red’
(Sailors ⋈ Reserves ⋈ Boats))
sname
(color = ‘green’
(Sailors ⋈ Reserves ⋈ Boats))
![Page 26: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/26.jpg)
26
Nested Queries
A query may be nested within another through the following operators:
(NOT) IN (NOT) EXISTS ANY ALL
A query with nested queries is computed using nested loops
![Page 27: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/27.jpg)
27
The IN Operator
SELECT S.sname
FROM Sailors S
WHERE S.sid IN (SELECT R.sid
FROM Reserves R
WHERE R.bid = 103);
Names of sailors who’ve reserved boat 103:
What would happen if we wrote NOT IN?
![Page 28: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/28.jpg)
28
The EXISTS Operator
SELECT S.sname
FROM Sailors S
WHERE EXISTS (SELECT *
FROM Reserves R
WHERE R.bid = 103 and
S.sid = R.sid);
Names of sailors who’ve reserved boat 103:
What would happen if we wrote NOT EXISTS?
Notice the correlation between the examined row
and the inner query
![Page 29: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/29.jpg)
29
Set-Comparison Queries: ANY,ALL
SELECT *
FROM Sailors S1
WHERE S1.age > ANY (SELECT S2.age
FROM Sailors S2);
Sailors who are not the youngest:
We can also use op ALL (op is >, <, =, >=, <=, or <>)
![Page 30: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/30.jpg)
30
Another Exercise What does the following query compute?
Using IN and INTERSECT operators, formulate a query that finds the names of the sailors who reserved both green and red boats.
SELECT S.sname
FROM Sailors S, Boats B, Reserves R
WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’
INTERSECT
SELECT S.sname
FROM Sailors S, Boats B, Reserves R
WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘green’;
![Page 31: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/31.jpg)
31
Another Exercise (continues)
What does the following query compute?
SELECT S.snameFROM Sailors S WHERE NOT EXISTS (
(SELECT B.bid FROM Boats B)MINUS(SELECT R.bid FROM Reserves R
WHERE R.sid=S.sid));
![Page 32: 1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem](https://reader030.vdocuments.site/reader030/viewer/2022032522/56649d615503460f94a430a1/html5/thumbnails/32.jpg)
32
For The Brave Ones…
1. Formulate a query that computes the sid of all sailors who have not reserved a green boat.
2. Prove formally that in the Sailors-Reserves-Boats database, the query of question 1 cannot be as simple as a query of the form:
SELECT Vj.sid
FROM R1 V1, R2 V2…Rn Vn
WHERE C
Where C is a simple condition (contains only comparison clauses, AND and OR, and does not contain nested queries)Notice that the records in each table are arbitrary!