Download Document

Document related concepts
no text concepts found
Transcript
Database Systems
DataBase System
Major Content & Grade
 Introduction
*
 The
***
Relational Model
 SQL
****
 Transaction
Management
***
 Database
Design (E-R)
***
 Database
Design (Normalization)
***
Haichang Gao , Software School , Xidian University
2
DataBase System
Unit 3 SQL
 3.1 Introduction
 3.2 Setting Up the Database
 3.3 Queries
 3.4 View
 3.5
Data manipulation
 3.6
Security
Haichang Gao , Software School , Xidian University
3
DataBase System
About SQL
 SQL is of the ability to implementing database in a
computerized environment.
 The SQL is an non-procedural language.
 Power of SQL:
 Data
definition (DDL)
( Include Other database object definition )
 Interactive Data
 Embedded
manipulation (DML)
SQL and dynamic SQL(数据库应用课讲)
 Integrity
 Authorization (Security)
 Transaction Control
Haichang Gao , Software School , Xidian University
4
DataBase System
History of SQL
 IBM
Sequel (Specifying QUeries As Relational Expression,
1972) language developed as part of System R project at the
IBM San Jose Research Lab
 SEQUEL (Structured English QUEry Language,1974)
Renamed SQL (Structured Query Language, 1976)
 ANSI and ISO standard SQL:
 SQL-86, SQL-89 (SQL1)
 SQL-92 (SQL2)
 SQL:1999 (SQL3)
 SQL:2003
 Commercial systems offer most, if not all, SQL-92 features,
plus varying feature sets from later standards and special
proprietary features.
Haichang Gao , Software School , Xidian University
5
DataBase System
Unit 3 SQL
 3.1 Introduction
 3.2 Setting Up the Database
 3.3 Queries
 3.4 View
 3.5
Data manipulation
 3.6
Security
Haichang Gao , Software School , Xidian University
6
DataBase System
Data Definition Language
 Data Definition Language (DDL) provide the abilities
to setting up a database.
 DDL allows the specification of not only a set of
relations but also information about each relation,
including:
 The
schema for each relation.
 The
domain of values associated with each attribute.
 Integrity constraints
 The
set of indices to be maintained for each relations.
 Security
 The
and authorization information for each relation.
physical storage structure of each relation on disk.
Haichang Gao , Software School , Xidian University
7
DataBase System
Domain Types in SQL
 char(n):
Fixed length character string, with user-specified
length n.
 varchar(n):
Variable length character strings, with userspecified maximum length n.
 int:
Integer (a finite subset of the integers that is machinedependent).
 smallint:
Small integer (a machine-dependent subset of the
integer domain type).
 numeric(p,d):
Fixed point number, with user-specified
precision of p digits, with n digits to the right of decimal
point.
Haichang Gao , Software School , Xidian University
8
DataBase System
Domain Types in SQL
 real,
double precision: Floating point and double-precision
floating point numbers, with machine-dependent precision.
 float(n):
Floating point number, with user-specified
precision of at least n digits.
 date:

Example: date ‘2005-7-27’
 time:

Dates, containing a (4 digit) year, month and date
Time of day, in hours, minutes and seconds.
Example: time ‘09:00:30’
 timestamp:

time ‘09:00:30.75’
date plus time of day
Example: timestamp ‘2005-7-27 09:00:30.75’
Haichang Gao , Software School , Xidian University
9
DataBase System
Domain Types in SQL
 blob:
binary large object -- object is a large collection of
uninterpreted binary data (whose interpretation is left to an
application outside of the database system).
 clob:
character large object -- object is a large collection of
character data.
 User-defined

domain:
Example: create domain money numeric(12, 2)
Haichang Gao , Software School , Xidian University
10
DataBase System
Creating DataBase
 Creating the Banking database:


Database Schema:
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
depositor (customer_name, account_number)
account (account_number, branch_name, balance)
borrower (customer_name, loan_number)
loan (loan_number, branch_name, amount)
Creating database Banking steps:
1) CREATE DATABASE Banking … (syntax lie on DBMS)
2) Creating referenced tables(被参照关系)
3) Creating referencing tables
4) Creating other object of database
Haichang Gao , Software School , Xidian University
11
DataBase System
Creating Tables
 Syntax
An SQL relation is defined using the create table command:

CREATE TABLE <table-name> (
[
[ <column-name1 data_type> [<column_constraint>] ,
[ ...n ] ]
[ <table_constraint> ],
]
);
Haichang Gao , Software School , Xidian University
12
DataBase System
Creating Tables
 Creating Tables in the Banking database:
//Creating table customer in SQL
CREATE TABLE customer (
customer_name char(20),
customer_street char(30) NOT NULL,
customer_city char(30)
);
Haichang Gao , Software School , Xidian University
13
DataBase System
Integrity Constraints in Tables
 Integrity constraints ENSURE that changes made to the database
by authorized DO NOT result in a loss of data consistency.

PRIMARY KEY Constrants (Entity integrity)

FROEIGE KEY Constrants (Referential integrity)

[NOT] NULL Constrants

UNIQUE Constrants

DEFAULT Constrants

CHECK Constrants

Assertion Constrants
 Syntax
CONSTRAINT <constraint_name> <constraint>
Haichang Gao , Software School , Xidian University
14
DataBase System
Integrity Constraints in Tables
 Create table with constraints
CREATE TABLE account (
account_number char(10),
branch_name char(30) NOT NULL,
balance numeric(12.2),
PRIMARY KEY (account_number),
FOREIGN KEY (branch_name)
REFERENCES branch(branch_name),
CONSTRAINT chk_balance CHECK (balance >= 0 ) ) ;
 The
referenced table must be an existing relation!
 Integrity
constraints can be added to an existing relation,
if the relation does not satisfies the constraint, reject!
 Constraint
name make it easy to drop.
Haichang Gao , Software School , Xidian University
15
DataBase System
Drop and Alter Table
 The drop table command deletes all information about the
dropped relation from the database.
 Syntax: DROP TABLE <table_name>;
 Example: DROP TABLE customer
 The alter table command is used to add attributes or constraints
to an existing relation.
 Syntax:
ALTER TABLE < table_name > ADD|DROP|ALTER …;
 Examples:
ALTER TABLE customer
ADD customer_id CHAR(10);
ALTER TABLE customer DROP customer_city;
ALTER TABLE account ALTER balance numeric(10.2);
Haichang Gao , Software School , Xidian University
16
DataBase System
More about create tables
 After
create table statement executed, the defination will
been stored in Data Dictionary as metadata.
A
foreign key specification is accepted only if it references
an existing table.
 There
are more complexed integrity Constrants will be
introduced later.
 Only
after tables been created, data can be entered into
the table in database.
 Integrity
Constrants are important part of table to avoid
invalid data into database.
Haichang Gao , Software School , Xidian University
17
课后阅读
DataBase System
 3.1, 3.2
 4.1, 4.2
Haichang Gao , Software School , Xidian University
18
DataBase System
Database File (ch11)
 The database is stored as a collection of files. Each file is a
sequence of records
 A database file is partitioned into fixed-length storage units
called blocks. Blocks are units of both storage allocation and
data transfer.
 Database system seeks to minimize the number of block
transfers between the disk and memory. We can reduce the
number of disk accesses by keeping as many blocks as
possible in main memory.
 Buffer – portion of main memory available to store copies of
disk blocks.
Haichang Gao , Software School , Xidian University
19
DataBase System
Database File
 A block is composed by block header, records and free space
 A page header contains:
 number
of record entries
 end of free space in the block
 location and size of each record
 Records can be moved around within a page to keep them
contiguous with no empty space between them
 Blocks are linked together as a file
Haichang Gao , Software School , Xidian University
20
DataBase System
Index of Table (ch12)
 Records may be stored in the Sequential File Organization(顺
序文件) ordered by a search-key.
 Example:
the account relation storage ordered by
branch_name:
 To find the records
 By
a given branch_name
value?
-- binary search(二分查找)
 By
a given balance value?
--Linear search(线性查找)
NOT a good idea!
Haichang Gao , Software School , Xidian University
21
DataBase System
Index of Table
 An index file consists of records (called index entries) of the
form:
search-key
pointer
 Search
Key - attribute or set of attributes used to look up
records in a file. Search Key is ordered or hashed.
 Indexing
mechanisms used to speed up access to desired
data.
Haichang Gao , Software School , Xidian University
22
DataBase System
Index of Table
 Types of index
 Primary
index: in a sequentially ordered file, the index
whose search key specifies the sequential order of the file.

Also called clustering index

The search key of a primary index is usually but not
necessarily the primary key.
 Secondary
index: an index whose search key specifies an
order different from the sequential order of the file. Also
called non-clustering index.
 Unique index: an index was the accepted way in some
database systems to guarantee a uniqueness constraint
for a candidate key.
Haichang Gao , Software School , Xidian University
23
DataBase System
Index of Table
 SQL DDL about index:
 CREATE
[UNIQUE | CLUSTER] INDEX <index_name>
ON < table_name > ( <column_name1> [ASC | DESC]
[<column_name2 > [ASC | DESC] ]…) ;
E.g.: CREATE INDEX b-index ON branch(branch_name)
 DROP
INDEX < index_name > ;
Haichang Gao , Software School , Xidian University
24
Database Systems
DataBase System
Unit 3 SQL
 3.1 Introduction
 3.2 Setting Up the Database
 3.3 Queries
 3.4 View
 3.5
Data manipulation
 3.6
Security
Haichang Gao , Software School , Xidian University
26
DataBase System
Basic Query Structure
 SQL is based on set and relational operations with certain
modifications and enhancements
 A typical SQL query has the form:
SELECT A1, A2, ..., An
FROM r1, r2, ..., rm
WHERE P ;
 Ai represents
an attribute
 Ri represents a relation
 P is a predicate
 This query is equivalent to the relational algebra expression:
 A1,A2 ,,An ( P (r1  r2    rm ))
 The
result of an SQL query is a relation.
Haichang Gao , Software School , Xidian University
27
DataBase System
The SELECT Clause
 The SELECT clause list the attributes desired in the result
of a query
 corresponds
to the projection operation of the relational
algebra
 Example:
find the names of all branches in the loan relation:
SELECT branch_name
FROM loan ;
 In
the relational algebra, the query would be:
branch_name (loan)
 NOTE:
SQL names are case insensitive (i.e., you may use
upper- or lower-case letters.)
Haichang Gao , Software School , Xidian University
28
DataBase System
The SELECT Clause
 SQL allows duplicates in relations as well as in query results.
 To
force the elimination of duplicates, insert the keyword
DISTINCT after select.
 Example:
Find the names of all branches in the loan
relations, and remove duplicates
SELECT DISTINCT branch_name
FROM loan ;
 The
keyword all specifies that duplicates not be removed.
SELECT ALL branch_name
FROM loan ;
Haichang Gao , Software School , Xidian University
29
DataBase System
The SELECT Clause
 An asterisk(*) in the select clause denotes “all attributes”
SELECT *
FROM loan ;
 The SELECT clause can contain arithmetic expressions
involving the operation, +, –, , and /, and operating on
constants or attributes of tuples.
 The
query:
SELECT loan_number, branch_name, amount  100
FROM loan ;
would return a relation that is the same as the loan relation,
except that the value of the attribute amount is multiplied by
100.
Haichang Gao , Software School , Xidian University
30
DataBase System
The WHERE Clause
 The WHERE clause specifies conditions that the result must
satisfy.
 Corresponds
to the selection predicate of the relational
algebra.
 Example:
Find all loan number at the Perryridge branch
with loan amounts greater than $1200.
SELECT loan_number
FROM loan
WHERE branch_name=‘ Perryridge’ AND amount>1200;
 Comparison
results can be combined using the logical
connectives AND, OR, and NOT.
Haichang Gao , Software School , Xidian University
31
DataBase System
The WHERE Clause
 SQL includes a BETWEEN comparison operator
 Example:
Find the loan number of those loans with loan
amounts between $90,000 and $100,000 (that is,  $90,000
and  $100,000)
SELECT loan_number
FROM loan
WHERE amount BETWEEN 90000 AND 100000 ;
Haichang Gao , Software School , Xidian University
32
DataBase System
The FROM Clause
 The FROM clause lists the relations involved in the query
 Corresponds
to the Cartesian product operation of the
relational algebra.
 Example:
Find the Cartesian product borrower × loan
SELECT 
FROM borrower, loan ;
 Example:
Find the name, loan number and loan amount of
all customers having a loan at the Perryridge branch
SELECT customer_name, borrower.loan_number, amount
FROM borrower, loan
WHERE borrower.loan_number = loan.loan_number
AND branch_name = ‘Perryridge’ ;
Haichang Gao , Software School , Xidian University
33
DataBase System
The RENAME Operation
 The SQL allows renaming relations and attributes using the
AS clause:
old-name AS new-name
 Example:
Find the name, loan number and loan amount of
all customers; rename the column name loan_number as
loan_id.
SELECT customer_name, borrower.loan_number
AS loan_id, amount
FROM borrower, loan
WHERE borrower.loan_number = loan.loan_number ;
Haichang Gao , Software School , Xidian University
34
DataBase System
The RENAME Operation
 Relation variables are defined in the FROM clause via the
use of the as clause.
 Example:
Find the customer names, loan number and loan
amount for all customers having a loan from the bank.
SELECT customer_name, B.loan_number, amount
FROM borrower AS B, loan AS L
WHERE B.loan_number = L.loan_number ;
Haichang Gao , Software School , Xidian University
35
Branch_
name
Branch_
city
DataBase System
The RENAME Operation
Brighton
 Example: Find the names of all
Downtown
branches that have assets greater Mianus
than at least one (some) branch North Town
located in Brooklyn.
…
B1.bn
B1.bc
Brighton
Brighton
Downtown
Downtown
Mianus
Mianus
North Town
North Town
…
Brooklyn
Brooklyn
Brooklyn
Brooklyn
Horseneck
Horseneck
Rye
Rye
…….
B1.a
B2.bn
3100000Brighton
3100000Downtown
9000000Brighton
9000000Downtown
400000Brighton
400000Downtown
3700000Brighton
3700000Downtown
…
…
assets
Brooklyn 3100000
Brooklyn 9000000
Horseneck 400000
Rye
3700000
……
…
branchB2.a
B2.bc
Brooklyn
Brooklyn
Brooklyn
Brooklyn
Brooklyn
Brooklyn
Brooklyn
Brooklyn
……
3100000
9000000
3100000
9000000
3100000
9000000
3100000
9000000
…
πB1.branch_name(B1.assets>B2.assets (
B1)( branch) × B2.branch_cith=‘Brooklyn’(B2( branch)) ) )
Haichang Gao , Software School , Xidian University
36
Branch_
name
Branch_
city
The RENAME Operation
Brighton
 Example: Find the names of all
Downtown
branches that have assets greater Mianus
than at least one (some) branch North Town
located in Brooklyn.
…
DataBase System
assets
Brooklyn 3100000
Brooklyn 9000000
Horseneck 400000
Rye
3700000
……
…
branch
SELECT DISTINCT B1.branch_name
FROM branch AS B1, branch AS B2
WHERE B1.assets > B2.assets AND
B2.branch_city = ‘ Brooklyn’ ;
πB1.branch_name(B1.assets>B2.assets B2.branch_cith=‘Brooklyn’(
B1)( branch) × B2( branch) ) )
Haichang Gao , Software School , Xidian University
37
DataBase System
String Operations
 SQL includes a string-matching operator for comparisons
on character strings. The operator “LIKE” uses patterns
that are described using two special characters:

percent (%). The % character matches any substring.

underscore (_). The _ character matches any character.
 Example:
Find the names of all customers whose street
includes the substring “Main”.
SELECT customer_name
FROM customer
WHERE customer_street LIKE ‘%Main%’ ;
 Example:
Match the name “Main%”
LIKE ‘Main\%’ escape ‘\’
Haichang Gao , Software School , Xidian University
38
DataBase System
The ORDER BY Clause
 The ORDER BY Clause Ordering the Display of Tuples
 Example:
List in alphabetic order the names of all customers
having a loan in Perryridge branch
SELECT DISTINCT customer_name
FROM borrower, loan
WHERE borrower loan_number = loan.loan_number
AND branch_name = ‘Perryridge’
ORDER BY customer_name ;
 We
may specify DESC for descending order or ASC for
ascending order, for each attribute; ascending order is
the default.
 Example:
ORDER BY customer_name DESC
Haichang Gao , Software School , Xidian University
39
DataBase System
Set Operations
 The set operations union, intersect, and except operate on
relations and correspond to the relational algebra operations

 Each of the above operations automatically eliminates
duplicates; to retain all duplicates use the corresponding
multiset versions union all, intersect all and except all.
 Set operations may NOT been implemented in some DBMS.
Haichang Gao , Software School , Xidian University
40
DataBase System
Set Operations
 Examples:
 Find
all customers who have a loan, an account, or both:
(SELECT customer_name FROM depositor)
UNION
(SELECT customer_name FROM borrower) ;
 Find all customers who have both a loan and an account.
(SELECT customer_name FROM depositor)
INTERSECT
(SELECT customer_name FROM borrower) ;
 Find all customers who have an account but no loan.
(SELECT customer_name FROM depositor)
EXCEPT
(SELECT customer_name FROM borrower) ;
Haichang Gao , Software School , Xidian University
41
Database Systems
DataBase System
Unit 3 SQL
 3.1 Introduction
 3.2 Setting Up the Database
 3.3 Queries
 3.4 View
 3.5
Data manipulation
 3.6
Security
Haichang Gao , Software School , Xidian University
43
DataBase System
Aggregate Functions(集函数)
 These functions operate on the multiset of values of a column
of a relation, and return a value
 avg([DISTINCT
| ALL] <column_name>): average value
 min([DISTINCT | ALL] <column_name>): minimum value
 max([DISTINCT| ALL] <column_name>): maximum value
 sum([DISTINCT | ALL] <column_name>): sum of values
 count([DISTINCT | ALL] <column_name>)
count([DISTINCT | ALL] *) : number of values
Haichang Gao , Software School , Xidian University
44
DataBase System
Aggregate Functions
 Examples:
 Find
the average account balance at the Perryridge branch.
SELECT avg (balance)
FROM account
WHERE branch_name = ‘Perryridge’ ;
 Find the number of tuples in the customer relation.
SELECT count (*)
FROM customer ;
 Find the number of depositors in the bank.
SELECT count (DISTINCT customer_name)
FROM depositor ;
Haichang Gao , Software School , Xidian University
45
DataBase System
The GROUP BY Clause
 The GROUP BY Clause division records in the middle result
into different part(group) according attribute or attributes
given by the GROUP BY Clause.
 Tuples with the same value on all attributes in the GROUP BY
Clause are placed in one group.
 After the tuples was grouped, Aggregate Functions will act on
the group, NOT the whole tuples.
Haichang Gao , Software School , Xidian University
46
DataBase System
The GROUP BY Clause
 Example:
Find the number of depositors for each branch.
SELECT branch_name, count (customer_name)
(DISTINCT customer_name)
FROM depositor, account
WHERE depositor.account_number =
account.account_number
GROUP BY branch_name ;
Note: Attributes in SELECT clause outside of aggregate
functions must appear in GROUP BY list.
Haichang Gao , Software School , Xidian University
47
DataBase System
The HAVING Clause
 The HAVING Clause is used to choose the specific groups
according the given predicate following the HAVING Clause.
 The HAVING Clause is usually after the GROUP BY Clause.
 Example:
Find the names of all branches where the average
account balance is more than $1,200.
SELECT branch_name, avg (balance)
FROM account
GROUP BY branch_name
HAVING avg (balance) > 1200 ;
Haichang Gao , Software School , Xidian University
48
DataBase System
Executing Orders of Query Clauses
④ SELECT [ALL | DISTINCT ] [*] | [<object_exp1> [ ,
<object_exp1> ]… ]
① FROM <table_name1>[, <table_name2 > ] …
② [ WHERE <condition_exp> ]
③ [ GROUP BY <column_names> ]
[ HAVING <condition_exp> ]
⑤ [ ORDER BY
<column_name1 > [ ASC | DESC ]
[<column_name2> [ ASC | DESC ] …]] ;
Haichang Gao , Software School , Xidian University
49
DataBase System
Null Values
 It is possible for tuples to have a null value, denoted by null,
for some of their attributes
 null signifies an unknown value or that a value does not exist.
 The predicate IS NULL can be used to check for null values.
 Example: 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 with null involving
null is null
 Example: 5 + null returns null
 Any comparison with null returns unknown
 Example: 5 < null or null <> null or null = null
Haichang Gao , Software School , Xidian University
50
DataBase System
Null Values
 Three-valued logic using the truth value unknown:
 OR:
(unknown OR true) = true,
(unknown OR false) = unknown
(unknown OR unknown) = unknown
 AND:
 NOT:
(true AND unknown) = unknown,
(false AND unknown) = false,
(unknown AND unknown) = unknown
(NOT unknown) = unknown
 “P
is unknown” evaluates to true if predicate P evaluates
to unknown
 Result of WHERE clause predicate is treated as false if it
evaluates to unknown
Haichang Gao , Software School , Xidian University
51
DataBase System
Null Values
 Aggregate functions simply ignore nulls
 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.
Haichang Gao , Software School , Xidian University
52
DataBase System
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.
 Nested Query indicates that SQL is a structured language.
SELECT [ALL | DISTINCT ] [*] | [<object_exp1>[,
<object_exp1> ]… ]
FROM <table_name1>[, <table_name2 > ] …
[ WHERE <condition_exp> ]
[ GROUP BY <column_names> ]
[ HAVING <condition_exp> ]
[ ORDER BY <column_name1> [ ASC | DESC ]
[<column_name2> [ ASC | DESC ] …]] ;
Haichang Gao , Software School , Xidian University
53
DataBase System
The IN predicate
 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 ) ;
Haichang Gao , Software School , Xidian University
54
DataBase System
The IN predicate
 Find all customers who have both an account and a loan at
the Perryridge branch.
A1 (IN predicate)
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 ) ;
Haichang Gao , Software School , Xidian University
55
DataBase System
The IN predicate
 Find all customers who have both an account and a loan at
the Perryridge branch.
account_
number
A-101
A-215
A-102
A-305
A-201
A-222
A-217
branch_
name
Downtown
Mianus
Perryridge
Round Hill
Brighton
Redwood
Brighton
account
balance
500
700
400
350
900
700
750
loan_
number
L-11
L-14
L-15
L-16
L-17
L-23
L-93
branch_
name
Round Hill
Downtown
Perryridge
Perryridge
Downtown
Redwood
Mianus
loan
amount
900
1500
1500
1300
1000
2000
500
πdepositor.customer_name(  depositor.customer_name = borrower.customer_name 
account.branch_name = ‘Perryridge’  loan.branch_name = ‘Perryridge’ (
(depositor
account) × (borrower
loan) )
Haichang Gao , Software School , Xidian University
56
DataBase System
The IN predicate
 Find all customers who have both an account and a loan at
the Perryridge branch.
A2 (Algebra expression)
SELECT DISTINCT depositor.customer_name
FROM depositor , account, borrower, loan
WHERE depositor.account_number = account.account_number
AND borrower.loan_number = loan.loan_number
AND depositor.customer_name = borrower.customer_name
AND account.branch_name = ‘Perryridge’
AND loan.branch_name = ‘Perryridge’;
πdepositor.customer_name(  depositor.customer_name = borrower.customer_name 
account.branch_name = ‘Perryridge’  loan.branch_name = ‘Perryridge’ (
(depositor
account) × (borrower
loan) )
Haichang Gao , Software School , Xidian University
57
DataBase System
Set Comparison
 >some ( > any ), including >=, <, <=, = …
 >all, including >=, <, <=, = …
 Example: Find all branches that have greater assets than
some branch located in Brooklyn. Branch_
SELECT branch_name
FROM branch
WHERE assets > some (
Branch_
assets
name
city
Brighton
Brooklyn 3100000
Downtown Brooklyn 9000000
Mianus
Horseneck 400000
North Town Rye
3700000
…
……
…
branch
SELECT assets
FROM branch
WHERE branch_city = ‘Brooklyn’)
Haichang Gao , Software School , Xidian University
58
DataBase System
Set Comparison
 Find the names of all branches that have greater assets than
all branches located in Brooklyn.
Branch_
name
Brighton
Downtown
Mianus
North Town
…
Branch_
assets
city
Brooklyn
3100000
Brooklyn
9000000
Horseneck 400000
Rye
3700000
……
…
SELECT branch_name
FROM branch
branch
WHERE assets > all (
SELECT assets
FROM branch
WHERE branch_city = ‘Brooklyn’)
Haichang Gao , Software School , Xidian University
59
DataBase System
Set Comparison
 Find the branch that has the highest average balance.
SELECT branch_name
FROM account
account_
number
A-101
A-215
A-102
A-305
A-201
A-222
A-217
GROUP BY branch_name
HAVING avg(balance) = max (
原因:集函数不能作用于查
询外部!
branch_
balance
name
Downtown
500
Mianus
700
Perryridge
400
Round Hill
350
Brighton
900
Redwood
700
Brighton
750
account
SELECT avg(balance)
FROM account
GROUP BY branch_name) ;
Haichang Gao , Software School , Xidian University
60
DataBase System
Set Comparison
 Find the branch that has the highest average balance.
SELECT branch_name
FROM account
GROUP BY branch_name
HAVING avg(balance) = (
原因:集函数不能复合!
account_
number
A-101
A-215
A-102
A-305
A-201
A-222
A-217
branch_
balance
name
Downtown
500
Mianus
700
Perryridge
400
Round Hill
350
Brighton
900
Redwood
700
Brighton
750
account
SELECT avg(balance)
max(avg(balance))
FROM account
GROUP BY branch_name) ;
Haichang Gao , Software School , Xidian University
61
DataBase System
Set Comparison
 Find the branch that has the highest average balance.
SELECT branch_name
FROM account
account_
number
A-101
A-215
A-102
A-305
A-201
A-222
A-217
GROUP BY branch_name
HAVING avg(balance) >= ALL (
branch_
balance
name
Downtown
500
Mianus
700
Perryridge
400
Round Hill
350
Brighton
900
Redwood
700
Brighton
750
account
SELECT avg(balance)
FROM account
GROUP BY branch_name) ;
Haichang Gao , Software School , Xidian University
62
DataBase System
Test for Empty Relations
 The exists construct returns the value true if the argument
subquery is nonempty.
 Example: Find the customers name who have at least one
deposit of a balance greater than $700.
{t | u( depositor(u)  v( account(v)  u[account_number ]
= v[account_number]  v[balance ] > ‘700’ ) 
t[customer_name] = u[customer_name ] ) }
SELECT DISTINCT customer_name
FROM depositor
WHERE EXISTS (
SELECT *
FROM account
WHERE depositor.account = account.account
AND balance
700 School
) ; , Xidian University 63
Haichang Gao>
, Software
DataBase System
Test for Empty Relations
 Find all customers who have an account at all branches
located in Brooklyn.
{t | u ( depositor(u)  v (branch(v)  v[branch_city] = ‘Brooklyn’
w( account(w)  w[account_number] = u[account_number] 
w[branch_name] = v[branch_name]) )  t[customer_name] =
u[customer_name] ) }
等价转换
{t | u ( depositor(u)    v (branch(v)  v[branch_city] = ‘Brooklyn’ 
 w( account(w)  w[account_number] = u[account_number] 
w[branch_name] = v[branch_name]) )  t[customer_name] = u
[customer_name] ) }
Haichang Gao , Software School , Xidian University
64
DataBase System
Test for Empty Relations
SELECT DISTINCT customer_name
FROM depositor AS D
WHERE NOT EXISTS (
SELECT *
FROM branch AS B
WHERE branch_city = ‘Brooklyn’ AND
NOT EXISTS (
SELECT *
FROM account
WHERE account_number = D.account_number
AND branch_name = B. branch_name));
{t | u ( depositor(u)    v (branch(v)  v[branch_city] = ‘Brooklyn’ 
 w( account(w)  w[account_number] = u[account_number] 
w[branch_name] = v[branch_name]) )  t[customer_name] = u
[customer_name] ) }
Haichang Gao , Software School , Xidian University
65
Queries
 Find the largest account balance.
A1 (Algebra expression)
DataBase System
account_
number
Branch_
name
A-101
A-215
A-102
…
Downtown
Mianus
Perryridge
……
balance
500
700
400
…
account
πbalance(account) –
πA1.balance (A1.balance < A2.balance (A1(account) × A2(account) ) )
SELECT DISTINCT balance
FROM account
EXCEPT
SELECT A1.balance
FROM account AS A1, account AS A2
WHERE A1.balance < A2.balance ;
Haichang Gao , Software School , Xidian University
66
Queries
 Find the largest account balance.
A2 (Aggregate Functions)
DataBase System
account_
number
Branch_
name
A-101
A-215
A-102
…
Downtown
Mianus
Perryridge
……
balance
500
700
400
…
account
SELECT DISTINCT balance
FROM account
(
WHERE balance = max(
balance ) ;
SELECT max( balance )
FROM account );
Haichang Gao , Software School , Xidian University
67
Queries
 Find the largest account balance.
A3 (Set Comparison)
DataBase System
account_
number
Branch_
name
A-101
A-215
A-102
…
Downtown
Mianus
Perryridge
……
balance
500
700
400
…
account
SELECT DISTINCT balance
FROM account
WHERE balance >= ALL (
SELECT balance
FROM account );
Haichang Gao , Software School , Xidian University
68
Queries
 Find the largest account balance.
A4 (EXISTS predicate)
DataBase System
account_
number
Branch_
name
A-101
A-215
A-102
…
Downtown
Mianus
Perryridge
……
balance
500
700
400
…
account
{t | u ( account(u)    v( account(v)  u[balance] 
v[balance])  t[balance] = u[balance] ) }
SELECT DISTINCT A1.balance
FROM account A1
WHERE NOT EXISTS (
SELECT *
FROM account A2
WHERE A1.balance < A2.balance);
Haichang Gao , Software School , Xidian University
69
DataBase System
Test for Absence of Duplicate Tuples
 The unique construct tests whether a subquery has any
duplicate tuples in its result.
 Example: Find all customers who have at most one account at
the Perryridge branch.
SELECT DISTINCT D1.customer_name
FROM depositor AS D1
WHERE UNIQUE (
SELECT D2.customer_name
FROM account AS A, depositor AS D2
WHERE A.account_number = D2.account_number
AND D1.customer_name = D2.customer_name
AND A.branch_name = ‘ Perryridge’ ) ;
Haichang Gao , Software School , Xidian University
70
DataBase System
Test for Absence of Duplicate Tuples
 Find all customers who have at least two accounts at the
Perryridge branch.
A1 (using UNIQUE)
SELECT DISTINCT D1.customer_name
FROM depositor AS D1
WHERE NOT UNIQUE (
SELECT D2.customer_name
FROM account AS A, depositor AS D2
WHERE A.account_number = D2.account_number
AND D1.customer_name = D2.customer_name
AND A.branch_name = ‘ Perryridge’ ) ;
Haichang Gao , Software School , Xidian University
71
DataBase System
Test for Absence of Duplicate Tuples
 Find all customers who have at least two accounts at the
Perryridge branch.
A2 (using GROUP BY)
SELECT customer_name
FROM depositor AS D, account AS A
WHERE D.account_number = A.account_number
AND A.branch_name = ‘Perryridge’
GROUP BY customer_name
HAVING count(*) > 2 ;
Haichang Gao , Software School , Xidian University
72
DataBase System
Derived Relations
 SQL allows a subquery expression to be used in the FROM
clause
 Example: 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 branch_avg ( branch_name, avg_balance )
WHERE avg_balance > 1200
Haichang Gao , Software School , Xidian University
73
DataBase System
With Clause
 The with clause provides a way of defining a temporary view
whose definition is available only to the query in which the
with clause occurs.
 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
Haichang Gao , Software School , Xidian University
74
DataBase System
With Clause
 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
Haichang Gao , Software School , Xidian University
75
DataBase System
Homework 3
 3.2
 3.9
注:尽可能用多种方法实现查询!
Haichang Gao , Software School , Xidian University
76
Database Systems
DataBase System
Unit 3 SQL
 3.1 Introduction
 3.2 Setting Up the Database
 3.3 Queries
 3.4 View
 3.5
Data manipulation
 3.6
Security
Haichang Gao , Software School , Xidian University
78
DataBase System
Views
 In some cases, it is not desirable for all users to see the entire
logical model (that is, all the actual relations stored in the
database.)
 Consider a person who needs to know a customer’s loan
number but has no need to see the loan amount. This person
should see a relation described, in SQL, by
(select customer_name, loan_number
from borrower, loan
where borrower.loan_number = loan.loan_number );
 A view provides a mechanism to hide certain data from the
view of certain users.
 Any relation that is not of the conceptual model but is made
visible to a user as a “virtual relation” is called a view.
Haichang Gao , Software School , Xidian University
79
DataBase System
Create Views
 A view is defined using the create view statement which has
the form:
CREATE VIEW v AS < query expression> ;
where <query expression> is any legal SQL expression. The
view name is represented by v.
Haichang Gao , Software School , Xidian University
80
DataBase System
Create Views
 Once a view is defined, the view name can be used to refer to
the virtual relation that the view generates.
 When a view is created, the definition of the view is placed in
the database, but no data is retrieved or stored.
 A view is a window on the data of the base tables, thus queries
on views are immediately responsive to changes in the
underlying base table data.
Haichang Gao , Software School , Xidian University
81
DataBase System
Create Views
 A view consisting of branches and their customers
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
depositor (customer_name, account_number)
account (account_number, branch_name, balance)
borrower (customer_name, loan_number)
loan (loan_number, branch_name, amount)
Haichang Gao , Software School , Xidian University
82
DataBase System
Create Views
 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’ ;
Haichang Gao , Software School , Xidian University
83
DataBase System
Create Views
 If the optional column name list is not specified, then the columns
of the new view table will inherit(继承) names of single columns
in the target list of the Subquery statements. However, names must
be provided when any view columns represent expressions in the
target list.
 Example: create a view for each branch the sum of the amounts of
all the loans at the branch.
CREATE VIEW branch_total_loan(branch_name, total_loan)
AS
SELECT branch_name, sum(amount)
FROM loan
GROUP BY branch_name ;
Haichang Gao , Software School , Xidian University
84
DataBase System
Create Views
 One view may be used in the expression defining another
view
 A view relation v1 is said to depend directly on a view relation
v2 , if v2 is used in the expression defining v1
 A view relation v1 is said to depend on view relation v2 , if
either v1 depends directly to v2 or there is a path of
dependencies from v1 to v2
 A view relation v is said to be recursive if it depends on
itself.(不要求掌握)
Haichang Gao , Software School , Xidian University
85
DataBase System
Drop Views
 Syntax:
DROP VIEW viewname {CASCADE|RESTRICT};
Haichang Gao , Software School , Xidian University
86
DataBase System
Views & ANSI/SPARC architecture
 View is the external level of the DBMS architecture.
Haichang Gao , Software School , Xidian University
87
DataBase System
ANSI/SPARC architecture
成绩单
学号:xxxxx 姓名:xxx
性别:x
离散数据
75
数据库系统
81
……
……
模式(Logical Schema,
Sno Sname Ssex Sage Sdept
Schema,也称逻辑模式)
——数据库中全体数据的逻辑
Sno Cno
结构和特征(型)的描述
外模式(External Schema,
也称子模式或用户模式)
——数据库用户使用的局部数据
的逻辑结构和特征的描述
Cno Cname Cpno Ccredit
Grade
内模式(Physical Schema,
也称存储模式)
——数据物理结构和存储方式
的描述
Haichang Gao , Software School , Xidian University
88
DataBase System
CREATE VIEW scores AS
(SELECT sno, sname,ssex,cname,grade
FROM S, C, SC
成绩单WHERE S.sno = SC.sno AND SC.cno = C.cno ;
ANSI/SPARC architecture
学号:xxxxx 姓名:xxx
性别:x
离散数据
75
数据库系统
81
……
……
S
NOTE: A view create the
External Schema and also the
map from External Schema to
Logical Schema.
C
Sno Sname Ssex Sage Sdept
SC
Sno
Cno
Cno Cname Cpno Ccredit
Grade
Haichang Gao , Software School , Xidian University
89
DataBase System
CREATE VIEW scores AS
(SELECT sno, sname,ssex,cname,grade
FROM S, C, SC
成绩单WHERE S.sno = SC.sno AND SC.cno = C.cno ;
ANSI/SPARC architecture
学号:xxxxx 姓名:xxx
性别:x
离散数据
75
数据库系统
81
……
External/Conceptual
mapping
……
Sno Sname Ssex Sage Sdept
Cno Cname Cpno Ccredit
………………
Sno
…………………
Cno
Grade
……
Conceptual/Internal
mapping
MAPPING
Haichang Gao , Software School , Xidian University
90
DataBase System
Data Independence
 Two type data independence
 Logical
data independence

the immunity of the external schemas to changes in the
conceptual schema.

related to External/Conceptual mapping
 Physical
data independence

the immunity of the conceptual schema to changes in the
internal schema.

related to Conceptual/Internal mapping
Haichang Gao , Software School , Xidian University
91
DataBase System
The value of views
 Views provide a way to make complex, commonly issued
queries easier to compose.(简化查询)
 Views allow obsolete tables, and programs that reference
them, to survive reorganization. (数据库重组)
 Views add a security aspect to allow different users to see the
same data in different ways. (不同角度看待同一数据)
Haichang Gao , Software School , Xidian University
92
DataBase System
Unit 3 SQL
 3.1 Introduction
 3.2 Setting Up the Database
 3.3 Queries
 3.4 View
 3.5
Data manipulation
 3.6
Security
Haichang Gao , Software School , Xidian University
93
DataBase System
Insertion
To insert ONE tuple or a query result into a table.
 Syntax
INSERT INTO table_name [(col_name1 {, col_name2…})]
VALUES (expr1| NULL {, expr2|NULL…})
| Subquery ;
 Add a new tuple to account .
INSERT INTO account
VALUES ( ‘A-9732’, ‘Perryridge’,1200 ) ;
or
INSERT INTO account (branch_name, balance,
account_number)
VALUES ( ‘Perryridge’, 1200, ‘A-9732’) ;
Haichang Gao , Software School , Xidian University
94
DataBase System
Insertion
 Present a new $200 savings account as a gift to all loan
customers of the Perryridge branch, for each loan they have.
Let the loan number serve as the account number for the
new savings account.
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
depositor (customer_name, account_number)
account (account_number, branch_name, balance)
borrower (customer_name, loan_number)
loan (loan_number, branch_name, amount)
Haichang Gao , Software School , Xidian University
95
DataBase System
Insertion
 Present a new $200 savings account as a gift to all loan
customers of the Perryridge branch, for each loan they have.
Let the loan number serve as the account number for the
new savings account.
INSERT INTO account
SELECT loan_number, branch_name, 200
FROM loan
WHERE branch_name = ‘Perryridge’ ;
INSERT INTO depositor
SELECT customer_name, loan_number
FROM loan, borrower
WHERE branch_name = ‘ Perryridge’
AND loan. loan_number = borrower. loan_number ;
 The two SQL statements must be ONE transaction.
Haichang Gao , Software School , Xidian University
96
DataBase System
Updates
To change a value in a tuple in current table without changing
all values in the tuple.
 Syntax
UPDATE table_name
SET col_name1 = {expr | NULL| (Subquery1)}
{, col_name2= {expr|NULL| (Subquery2)…}}
[WHERE search_condition];
Haichang Gao , Software School , Xidian University
97
DataBase System
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 > 10000 ;
UPDATE account
SET balance = balance  1.05
WHERE balance <= 10000 ;
 The
order is important
 Also
should be a transaction
Haichang Gao , Software School , Xidian University
98
DataBase System
Updates
 Same query as before: Increase all accounts with balances over
$10,000 by 6%, all other accounts receive 5%.
 UPDATE account
SET balance =
CASE
WHEN balance <= 10000 THEN balance *1.05
ELSE balance * 1.06
END ;
Haichang Gao , Software School , Xidian University
99
DataBase System
Deletion
To delete the tuple(s) in the current table.
 Syntax
DELETE FROM table_name
[WHERE search_conditon];
 Delete all account tuples at the Perryridge branch.
DELETE FROM account
WHERE branch_name = ‘Perryridge’ ;
 Delete all accounts at every branch located in the city ‘Needham’.
DELETE FROM account
WHERE branch_name IN (
SELECT branch_name
FROM branch
WHERE branch_city = ‘Needham’ ) ;
Haichang Gao , Software School , Xidian University
100
DataBase System
Deletion
 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)
Haichang Gao , Software School , Xidian University
101
DataBase System
Update of a view
 A modification to a view must be translated to a
modification to the actual relations in the logical model of
the database.
 Example: Create a view of all loan data in the loan relation,
hiding the amount attribute:
CREATE VIEW branch_loan AS
SELECT branch_name, loan_number
FROM loan ;
 Add a new tuple to branch_loan :
INSERT INTO branch_loan
VALUES (‘Perryridge’, ‘L-307’) ;
This insertion must be represented by the insertion of the
tuple
(‘L-307’, ‘Perryridge’, null )
into the loan relation
Haichang Gao , Software School , Xidian University
102
DataBase System
Update of a view
 Suppose a view downtown_account is defined as follow:
CREATE VIEW downtown_account AS
SELECT account_number, branch_name, balance
FROM account
WHERE branch_name = ‘Downtown’ ;
 Then
the tuple (‘A-999’, ‘Perryridge’, 100) can be inserted
into the downtown_account view, in fact, the tuple is inserted
into the TABLE account, but this is not what we want.
 This
problem can be solved at the defination of view:
CREATE VIEW v AS < query expression>
[WITH CHECK OPTION];
The clause WITH CHECK OPTION add the view’s WHERE
conditions to the modifications of view.
Haichang Gao , Software School , Xidian University
103
DataBase System
Update of a view
 Redefine the view downtown_account as follow:
CREATE VIEW downtown_account AS
SELECT account_number, branch_name, balance
FROM account
WHERE branch_name = ‘Downtown’
WITH CHECK OPTION ;
 INSERT
INTO downtown_account(account_number, balance)
VALUES (‘A-999’, 100);
will insert the tuple (‘A-999’, ‘downtown’, 100) into TABLE
account, and insert the tuple (‘A-999’, ‘Perryridge’, 100) to the
view downtown_account will be rejected by the DBMS.
 Updates
and Deletes are similarly rejected if the new value does
not satisfy the WHERE clause condition.
Haichang Gao , Software School , Xidian University
104
DataBase System
Update of a view
 A view is either updatable or read-only. Insert, Update, and
Delete operations are permitted for updatable view and not
permitted for read-only views.
 To be an updatable view, the following conditions must be
all satisfied:
 The
FROM clause has only one database relation.
 The
SELECT clause contains only attribute names of the
relation, and does not have any expressions, aggregates, or
DISTINCT specification.
 Any
attribute not listed in the SELECT clause can be set to
NULL.
 The
query does not have a GROUP BY or HAVING clause.
Haichang Gao , Software School , Xidian University
105
DataBase System
Joined Relations
 Join operations take two relations and return as a result another
relation.
 These additional operations are typically used as subquery
expressions in the from clause
 Join condition – defines which tuples in the two relations match,
and what attributes are present in the result of the join.
 Join type – defines how tuples in each relation that do not match
any tuple in the other relation (based on the join condition) are
treated.
Haichang Gao , Software School , Xidian University
106
DataBase System
Joined Relations
customer_name
loan_number
branch_name amount
L-170
Downtown
3000 Jones
L-230
Redwood
4000 Smith
L-155
Perryridge
1700 Hayes
loan
borrower
 loan INNER JOIN borrower ON
loan.loan_number = borrower.loan_number
loan_number
L-170
L-230
L-260
 loan NATURAL INNER JOIN borrower
loan_number branch_Name
L-170
Downtown
L-230
Redwood
amount customer_name
3000 Jones
4000 Smith
Haichang Gao , Software School , Xidian University
107
DataBase System
Joined Relations
loan_number
L-170
L-230
L-260
customer_name
loan_number
branch_name amount
L-170
Downtown
3000 Jones
L-230
Redwood
4000 Smith
L-155
Perryridge
1700 Hayes
loan
borrower
 loan LEFT OUTER JOIN borrower on
loan.loan_number = borrower.loan_number
Haichang Gao , Software School , Xidian University
108
DataBase System
Joined Relations
loan_number
L-170
L-230
L-260
customer_name
loan_number
branch_name amount
L-170
Downtown
3000 Jones
L-230
Redwood
4000 Smith
L-155
Perryridge
1700 Hayes
loan
borrower
 loan natural right outer join borrower
Haichang Gao , Software School , Xidian University
109
DataBase System
Joined Relations
loan_number
L-170
L-230
L-260
customer_name
loan_number
branch_name amount
L-170
Downtown
3000 Jones
L-230
Redwood
4000 Smith
L-155
Perryridge
1700 Hayes
loan
borrower
 loan full outer join borrower using (loan_number)
Haichang Gao , Software School , Xidian University
110
DataBase System
Joined Relations
 Find all customers who have either an account or a loan
(but not both) at the bank.
SELECT customer_name
FROM (depositor natural full outer join borrower )
WHERE account_number IS NULL
OR loan_number IS NULL;
Haichang Gao , Software School , Xidian University
111
DataBase System
Homework 4
 3.15
 3.19
Haichang Gao , Software School , Xidian University
112
Related documents