0% found this document useful (0 votes)
6 views113 pages

5 SQL

The document provides an overview of SQL (Structured Query Language), including its pronunciation variations and historical background. It covers key concepts such as Data Definition Language (DDL) and Data Manipulation Language (DML), along with examples of SQL queries and operations. Additionally, it discusses data types, integrity constraints, and various SQL commands for managing database relations.

Uploaded by

2019678536
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views113 pages

5 SQL

The document provides an overview of SQL (Structured Query Language), including its pronunciation variations and historical background. It covers key concepts such as Data Definition Language (DDL) and Data Manipulation Language (DML), along with examples of SQL queries and operations. Additionally, it discusses data types, integrity constraints, and various SQL commands for managing database relations.

Uploaded by

2019678536
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 113

AI323

SQL -Structured Query Language


结构化查询语言

1
SQL怎么念?
n /‘sɜːkl/ – 同circle. 部分开发者独创发音

2
SQL怎么念?
n /‘sɜːkl/ – 同circle. 部分开发者独创
n /ˈsiːkwəl/ - 同Sequel. “这门语言最开始叫做“SEQUEL” –
Structured English Query Language的缩写. 一家名叫
Hawker Siddeley航空和发动机制造公司注册了此商标。为
了避免商业纠纷,SEQUEL被改名为Structured Query
Language,即现在的SQL” 1。

1. SQL到底怎么念? https://zhuanlan.zhihu.com/p/51094392

3
SQL怎么念?
n /‘sɜːkl/ – 同circle. 中国开发者独创
n /ˈsiːkwəl/ - 同Sequel. “这门语言最开始叫做“SEQUEL” –
Structured English Query Language的缩写. 一家名叫
Hawker Siddeley航空和发动机制造公司注册了此商标。为
了避免商业纠纷,SEQUEL被改名为Structured Query
Language,即现在的SQL” 1。
n Ess-cue-ell, 即S-Q-L, ISO标准规定发音2。

1. SQL到底怎么念? https://zhuanlan.zhihu.com/p/51094392
2. 经常有人发错 SQL 的发音,如何华丽丽的引导纠正他们又不失优雅? -
https://www.zhihu.com/question/23965403 4
SQL
最广泛使用的数据库查询语言。
n 我们专注于SQL92标准。
n Data Manipulation Language 数据操作语言
Data Definition Language 数据定义语言
Constraint Specification 定义约束
Embedded SQL 嵌入式SQL
Transaction Management ….
Security Management .....

5
Outline

n Introduction 数据定义语言
n Data Definition Language (DDL)
n Fundamental concepts
n Data Manipulation Language (DML)
n Data Definition Language (DDL)
n Advanced concepts

6
Data Definition Language (DDL)

n Allows the specification of


n Relation schema :定义属性及其定义域
n Integrity constraints (ICs): 完整性约束
n 在磁盘上存储的组织方式
Lec10~12
n 索引方法
n 用户对relation的操作权限

7
Domain Types in SQL 数据类型

n char(n) 长度恒为n的字符串
n varchar(n) 可变长度的字符串,最大长度为n
n int integer (a finite subset of the integers that is
machine-dependent). 整型, 4个字节
n smallint 小整型,一般2个字节,具体与机器有关。
n Numeric(p,d) 数值,一共p位,其中小数点后d位。

8
n real, double precision 浮点数和双精度浮点数,
具有与机器相关的精度。
n float(n) 浮点数,用户通过参数 n 指定精度
(bits)
n date Dates, containing a (4 digits) year,
month and date. YYYY-MM-DD

n time Time of day, in hours, minutes and


seconds. "00:00:00"

9
NULL value
• 所有数据类型的取值范围都包含null。可以
在声明的时候加上后缀not null ,表示不允
许取null值。
n float [ (n) ] 其中 n 是用于以科学记数法存储浮点
数尾数的位数,因此决定了精度和存储大小。如果
指定了 n,则它必须是 1 到 53 之间的值。n 的默
认值为 53。

10
Schema Definition in SQL

Customer customer-name customer-street customer-city

create table customer


( customer-name char(20) not null,
customer-street char(30),
customer-city char(30),
primary key (customer-name) )

11
Drop Table

n 从 SQL 数据库中删除relation r, we use the drop


table command:
n drop table r

12
Alter Table

n alter table: 删除或者增加属性


n 加属性后,现有纪录在新属性的值为null
n alter table customer add phone char(10)
n alter table customer drop phone

13
Integrity Constraints (ICs)
完整性约束

n IC: anytime-constraint – 数据库在任何时候都要成立


n 譬如: not null表示相应属性的值不能为空

n ICs:一般情况下,在创建relation的时候设定IC约束
n ICs: 在relation发生变化时候,IC都会被检查。

14
Outline

n Introduction
n Data Definition Language (DDL)
n Fundamental concepts 数据操作语言
n Data Manipulation Language (DML)
n Data Definition Language (DDL)
n Advanced concepts

15
Data Manipulation Language (DML)

n Basic SQL Query 基本查询语句


n Arithmetic Operation
n Rename Operation
n String Operation
n Ordering the Display of Tuples
n Aggregate Operator
n Set Operation
n Division
n Group By
n NULL value

16
Basic SQL Query

n The basic form of SQL


SELECT [DISTINCT] target-list
FROM relation-list
WHERE qualification
n relation-list
n 关系名称列表(每个名称后面可能带有范围变量)。
n Target-list
n 属性列表,逗号分隔

17
SELECT [DISTINCT] target-list
FROM relation-list
WHERE qualification

n qualification 逻辑表达式
n 数值比较: (Attr op const or Attr1 op Attr2, where op is one

of <, >, =, £, ³, ¹ ).
n 可以用逻辑连接符链接多个表达式:AND, OR and NOT.

n DISTINCT 可选,表示最终结果要去重
n 默认下,查询结果是允许重复纪录存在的

18
n SELECT clause: 后接选择的属性
n 选取 columns. 相当于projection 操作
n FROM clause: 返回relations的笛卡尔积
n specifies a cross-product of tables.
n WHERE clause (optional): 可选语句,用来指
定纪录需满足的条件. 相当于selection操作
n SQL对应着关系代数的几种运算:selections,

projections, and cross-products.

19
SELECT DISTINCT a1, a2, …, an
FROM R1, R2, …, Rm
WHERE P

P a1, a2, …, an ( s P ( R1 x R2 x … x Rm ) )

20
n Note: for our examples we use the tables:
n Branch (branch-name, branch-city, assets)
n Customer (customer-name, customer-street,
customer-city)
n Loan (loan-number, amount, branch-name)
n Account (account-number, balance, branch-name)
n Depositor (customer-name, account-number)
n Borrower (customer-name, loan-number)
仅在我们这个ppt里设定用户名customer-name是唯一的,
并用作primary key

21
n Example: Find the names of all
branches in the loan relation.
SELECT branch-name
FROM Loan

Loan Result

22
n To remove duplications 消除重复

SELECT DISTINCT branch-name


FROM Loan
Loan Result

P branch_name ( Loan )
23
Data Manipulation Language

n Basic SQL Query


n Arithmetic Operation 算术运算
n Rename Operation
n String Operation
n Ordering the Display of Tuples
n Aggregate Operator
n Set Operation
n Division
n Group By
n NULL value

24
Arithmetic Operations on Retrieved Results

n select 子句可以包含涉及运算符 +,-,/ and ´的算术表达


式,并对元组的常量或属性进行操作
n 譬如:

select branch-name, loan-number, amount * 100


from loan

n 将返回与loan相同的关系,只不过属性amount乘以 100。

25
The where Clause

n where 子句指定 from 子句的relation中的元组必须满足的条件


n Find all loan numbers for loans made at the Perryridge branch
with loan amounts greater than $1200.
select loan-number
from loan
where branch-name=“Perryridge” and amount >1200

n where语句中允许使用逻辑运算符and, or, and not


n select and where语句中用到的属性必须存在于from语句的
relation中。

26
The where Clause (Cont.)
n 可以直接用between…and…关键字表示目标区间
n Find the loan number of those loans with loan amounts
between $90,000 and $100,000 (要求 amount³ $90,000 同
时 £ $100,000)
select loan-number
from loan
where amount between 90000 and
100000

loan_number amount
1 95000
2 100000
3 4000
27
The from Clause

n from返回的是relations的笛卡尔积
n Find the Cartesian product: borrower ´ loan:
select *
from borrower, loan
n Find the name and loan number of all customers
having a loan at the Perryridge branch. 找出在
Perryridge 分行拥有贷款的顾客及其贷款记录

select distinct customer-name, borrower.loan-number


from borrower, loan
where borrower.loan-number = loan.loan-number and
branch-name = “Perryridge”

28
The from Clause
n

select distinct customer-name, borrower.loan-number


from borrower, loan
where borrower.loan-number = loan.loan-number and
branch-name = “Perryridge”

Customer- loan- branch- loan-


name number name number
Ada 3 Perryridge 3

Ada 3 Brooklyn 5

Ada 3 Mexico 1


29
Data Manipulation Language
(DML)
n Basic SQL Query
n Arithmetic Operation
n Rename Operation 重命名操作
n String Operation
n Ordering the Display of Tuples
n Aggregate Operator
n Set Operation
n Division
n Group By
n NULL value

30
The Rename Operation

n 使用 as 子句重命名关系和属性
n 重命名语法:
old-name as new-name

n 把 loan-number 属性重命名为loan-id :

select distinct customer-name, borrower.loan-number as loan-id


from borrower, loan
where borrower.loan-number = loan.loan-number and
branch-name = “Perryridge”

31
Tuple Variables/Alias 别名

n 可以在from语句利用as关键字起别名
n Find the customer names and their loan numbers
for all customers having a loan at some branch.

select distinct customer-name, T.loan-number


from borrower as T, loan as S
where T.loan-number = S.loan-number

32
Tuple Variables/Alias
n Find the names of all branches that have greater assets than some
branch located in Brooklyn. 找出资产不是比Zhuhai所有的支行的资
产都少(或相等)的支行。即,只要大于至少Zhuhai一家支行就行。
select distinct T.branch-name
from branch as T, branch as S
where T.assets > S.assets and S.branch-city=“Zhuhai”

name City Assets name …. name City Asset


1 Zhuhai 10000 s
2 Zhuhai 50000 2 …. 1 Zhuhai 10000
3 Guangzhou 60000
2 …. 2 Zhuhai 50000
结果:
(本来应该 2 …. 3 Guang 60000
会有两个3, name zhou
why? 因为 2



distinct,所 3 33
以去重了)
Data Manipulation Language (DML)

n Basic SQL Query


n Arithmetic Operation
n Rename Operation
n String Operation 字符串操作
n Ordering the Display of Tuples
n Aggregate Operator
n Set Operation
n Division
n Group By
n NULL value
n Sequence
34
String Operations
n 在匹配中,可以用特殊字符描述字符串模式:
%指代任意字符组合(可以为空)
_ 指代任意单个字符
n Find the name of all customers whose street includes the
substring ‘Main’. (E.g., Mainroad, Small Main Road,
AMainroad,…) 找出住址名字带有Main的顾客

select customer-name
from customer
where customer-street like “%Main%”

customer-street like “%Main%” ---》 “the Main building”


customer-street like “_Main%” ---》 “xMain building”
customer-street like “__Main%” ---》 “我Main building
35
String Operations
select customer-name
from customer where customer-street like “%Main%”
n 在上述sql语句中如果还想要匹配 “main”,即包括Main和main,

甚至MAIN等,怎么做呢? Oracle 匹配区分大小写

Oracle中正表达式函数REGEXP_LIKE的使用:
REGEXP_LIKE(x, pattern, [match_option])

n 当源字符串x匹配正则表达式pattern时,返回true。可以使用match_option修改默认
匹配选项,该参数可以被设置为:
n - 'c', 说明在进行匹配时区分大小写(默认选项)
n - 'i', 说明在进行匹配时不区分大小写
n - 'n' 允许使用可以匹配任意字符的操作符(通常是'.')
n - 'm', 将x作为一个包含多行的字符串

36
String Operations
SELECT * FROM test_reg
WHERE REGEXP_LIKE(name, '(a)\1', 'i');

上面的SQL语句匹配test_reg表中name列含有两个连续字符'a'
(不区分大小写)的行,如name='SaAs'。

此外,这里我们还使用了正则表达式中的后引用语法——\n表
示重复n次上次匹配的内容,此处(a)\1表示匹配两个连续的字符
'a'。
最后一点,不要混淆LIKE操作符的通配符和正则表达式的语法,
也就是说不要再正则表达式中使用LIKE操作符中的通配符,如
果这样做会得到未知的结果,因为'_'和'%'会被正则表达式当做
普通字符进行匹配.
https://docs.oracle.com/cd/B12037_01/server.101/b10759/conditions018.htm 37
Data Manipulation Language
(DML)
n Basic SQL Query
n Arithmetic Operation
n Rename Operation
n String Operation
n Ordering the Display of Tuples 给结果排序
n Aggregate Operator
n Set Operation
n Division
n Group By
n NULL value

38
Ordering the Display of Tuples
n List in alphabetic字母的 order the names of all customers 按名字给结
果排序
select distinct customer-name
from customer
order by customer-name

select distinct customer-name


from customer
order by customer-name desc

n asc 表示升序,也是默认模式。
n desc 表示降序
n 在很多的时候对结果排序很耗时,在必要的时候用。

39
Ordering the Display of Tuples

n List the names of all customers in the ascending order of


customer-city and then descending order of customer-name

select distinct customer-name 按 customer-city 升序、


from customer customer-name 降序列
order by customer-city asc, 出所有客户的姓名
customer-name desc

customer-city customer-name
Guangzhou Betty
Guangzhou Ada
Zhuhai Caroline

40
Ordering the Display of Tuples

n List in alphabetic order the names of all


customers having a loan at Perryridge
branch
select distinct customer-name
from borrower, loan
where borrower.loan-number = loan.loan-number and
branch-name = “Perryridge”
order by customer-name

41
Data Manipulation Language
(DML)
n Basic SQL Query
n Arithmetic Operation
n Rename Operation
n String Operation
n Ordering the Display of Tuples
n Aggregate Operator
n Set Operation
n Division
n Group By
n NULL value

42
Aggregate Function

COUNT([DISTINCT] A) 计数 A 列中(唯一)值的数量.

SUM ( [DISTINCT] A) 求和 A 列中所有(唯一)值的总和。

AVG ([DISTINCT A) A 列中所有(唯一)值的平均值。


求均值
MAX (A) 求最大 A列中的最大值.
MIN (A) 求最小 A 列中的最小值。

43
Aggregate Function
n Find the average account balance at the Perryridge branch.

select avg(balance)
from account
where branch-name=“Perryridge”

Balances of Perryridge accounts


account select balance
from account
where branch-name Avg()
=“Perryridge” 120,000

44
Examples of Aggregate Functions

n Find the numbers of tuples in the account relation.

select count(*)
from account

n * 表示所有属性
n Same as:

select count(distinct account-number)


from account How about
select
n Because account-number is a key
count(distinct
branch-name)
from account

45
Data Manipulation Language
(DML)
n Basic SQL Query
n Arithmetic Operation
n Rename Operation
n String Operation
n Ordering the Display of Tuples
n Aggregate Operator
n Set Operation
n Division
n Group By
n NULL value

46
Set Operations

n 集合运算union, intersect, and except 表示 È, Ç and -.


并集,交集,差集
n 以上每次操作自动消除重复自动去重;要保留所有重
复项,我们可以使用 union all、intersect all 和 except
all
n 假设一个元组在 r 中出现 m 次,在 s 中出现 n
次,则它出现
n m + n times in r union all s
n min(m,n) times in r intersect all s
n max(0,m-n) times in r except all s

47
Set operations

n Find all customers who have a loan, or an account, or


both:
(select customer-name from depositor)
union
(select customer-name from borrower)
n Find all customers who have both a loan and an account.
(select customer-name from depositor)
intersect
(select customer-name from borrower)
n Find all customers who have an account but no loan.
(select customer-name from depositor)
except
(select customer-name from borrower)

48
Set operations

n In Oracle, minus is used instead of


except

49
SQL - Nested Subqueries 嵌套查询

n 每个 SQL 语句都会返回
n Relations/纪录集合 或
n Null 或
n 一个数值
n 可在where语句中运用子查询
select * select *
from loan from loan
where amount > 1200
where amount > select avg(amount)
from loan
n 子查询返回的类型须与不等式另一边相同。

50
Example Query - IN.

n Find all customers who have both an account and a


loan in the bank.

select distinct customer-name


from borrower
where customer-name in (select customer-name
from depositor)

For each borrower,


check if he/she is also a depositor
Return the set of depositors

51
Example Query – NOT IN

n 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)

52
The Some Clause

n Find all branches that have greater assets than some branches
located in Brooklyn
n 其实就是:大于Brooklyn分行资产中的最小个

select branch-name
from branch
where assets > some
Equivalent to min (select assets
in this SQL from branch
where branch-city = “Brooklyn”)

Assets of all branches in Brooklyn.


返回的是asserts列

53
Some Semantics. Some的语义

0
(5 > some 5 ) returns true (5 > 0)
6

5
(5 > some 6 ) returns false

0
(5 = some 5 ) = true

0
(5 ¹ some ) = true (since 0 ¹ 5)
5
54
The All Clause

n Find the names of all branches that have greater


assets than all branches located in Brooklyn.

select branch-name
from branch
where assets > all
(select assets
from branch
where branch-city=“Brooklyn”)

Assets of all branches in Brooklyn


55
All Semantics

0
(5 > all 5 ) = false
6

(5 > all 0 ) = true


4

4
(5 = all 5 ) = false

6
(5 ¹ all ) = true
10
56
Test for Empty Relations

n exists 判断列表是否空

exists( ) true

exists( ) false

57
Test for Empty Relations

n Find all customer names who have both a


loan and an account.
For each depositor d,
- 检查其是否出现在
select customer-name borrower中
from depositor as D - 是的话(exists),包含在
where exists 结果中
(select *
from borrower as B
where D.customer-name = B.customer-name)

这里可以理解为两个for loop
58
Test for Empty Relations
select customer-name
from depositor as D 这里可以理解为两个for loop
where exists
(select *
from borrower as B
where D.customer-name = B.customer-name)

For every D(one row) in depositor

Check if exists(…..with D as an input) returns true( for every B in borrower, check equal name)

true false
Save D in the outer-select outcome Abandon D
59
Test for Empty Relations

n Find all customer names who have an


account but no loan.
For each depositor d,
- find a set S of borrowers
select customer-name with the same name as d
- if not exists(S) (i.e., there
from depositor as D does not exist at least one
where not exists element in S),
return d
(select *
from borrower as B
where D.customer-name = B.customer-name)

60
Test for Absence of Duplicate Tuples 检查有无重
复纪录

n unique 返回true如果结果里没有重复的纪录
n Find all customers who have only one account at the Perryridge branch.

select T.customer-name For each depositor T, check ...


from depositor as T
where unique (
select R.customer-name
from account, depositor as R
where T.customer-name = R.customer-name and
R.account-number = account.account-number and
account.branch-name = “Perryridge”)

Customers at Perryridge with same name as T


Test for Absence of Duplicate Tuples 检查有无重
复纪录

For every T(one row) in depositor

Check if unique(…..with D as an input) returns true( for every R in depositor, and every account)

true false
Save D in the outer-select outcome Abandon D
Test for Absence of Duplicate Tuples 检查有无重
复纪录

找到该用 T.customer- T.account-


户的储户 name number

R.customer- R.account-
连接
name number
找到该分
行的储户
Account.account Account.branch_
_number name

Perryridge
Example Query – NOT UNIQUE

n Find all customers with at least 2 accounts at the Perryridge


branch.

select T.customer-name
from depositor as T
where not unique(
select R.customer-name
from account, depositor as R
where T.customer-name = R.customer-name and
R.account-number = account.account-number and
account.branch-name = “Perryridge”)
Data Manipulation Language
(DML)
n Basic SQL Query
n Arithmetic Operation
n Rename Operation
n String Operation
n Ordering the Display of Tuples
n Aggregate Operator
n Set Operation
n Division
n Group By
n NULL value

65
Division in SQL
n Find all customers with an account at all branches located in Brooklyn.
找出在所有Brooklyn分行都有账号的顾客
select distinct S.customer-name
from depositor as S
where not exist (
(select branch-name
For each from branch Branches in Brooklyn
customer S, where branch-city=“Brooklyn”)
check ... except
(select R.branch-name
All branches where from account as R, depositer as T
customer S where S.customer-name = T.customer-name and
has an account T.account-number = R.account-number) )
Customer branch-name
Ada a
Ada b 66
Ada c
Division in SQL
n Find all customers with an account at all branches located in Brooklyn.

select distinct S.customer-name


from depositor as S
where not exist (
(select branch-name
For each from branch Branches in Brooklyn
customer S, where branch-city=“Brooklyn”)
check ... except
(select R.branch-name
All branches where from account as R, depositer as T
customer S where S.customer-name = T.customer-name and
has an account T.account-number = R.account-number) )

branch-name Branch-city
a Brooklyn
67
b Brooklyn
Division in SQL
n Find all customers with an account at all branches located in Brooklyn.

select distinct S.customer-name


from depositor as S
where not exist (
(select branch-name
For each from branch Branches in Brooklyn
customer S, where branch-city=“Brooklyn”)
check ... except
(select R.branch-name
All branches where from account as R, depositer as T
customer S where S.customer-name = T.customer-name and
has an account T.account-number = R.account-number) )

Branch-city branch-name branch- Customer


name
Brooklyn a except
a Ada
68
Brooklyn b b Ada
c Ada
Data Manipulation Language
(DML)
n Basic SQL Query
n Arithmetic Operation
n Rename Operation
n String Operation
n Ordering the Display of Tuples
n Aggregate Operator
n Set Operation
n Division
n Group By
n NULL value

69
Group by
n Find the number of accounts for each branch. 找出各个分行拥有的账
号数目。
select branch-name, count(account-number)
from account
group by branch-name
n 首先要分组,用group by,然后用count对各组进行计数。

account table Group by结果


branch-name account-number balance branch-name account-number balance

Perryridge a-102 400 Perryridge a-102 400


Brighton a- 217 750 Perryridge a-201 900
Perryridge a-201 900 Brighton a-217 750
Brighton a-215 750 Brighton a-215 750
Redwood a-222 700 Redwood a-222 700
branch-name Count(account-number)
Perryridge 2
最终结果 Brighton 2
Redwood 1 70
Group by Attributes

select指定的字段要么就要包含在Group By语句的后面,作为分组的依据;
要么就要被包含在聚合函数中。
select branch-name, balance, count(account-number)
from account
group by branch-name

select branch-name, balance, count( distinct


account-number) branch-nam account-numb balance
e er
from account Perryridge a-102 400
group by branch-name, balance Perryridge a-201 900
OR Brighton a-217 750
Brighton a-215 750
select branch-name, sum(balance), count(…) Redwood a-222 700
from account
group by branch-name
71
Group by with Join

n Find the number of depositors储蓄用户 for each branch.


select branch-name, count( distinct customer-name)
from depositor, account
where depositor.account-number = account.account-number
group by branch-name

n 因为同个顾客可能有多个账户。先join两个relation,找出顾客姓
名、账户跟branch的结合。
n 这里distinct是必须的。因为有些customers可能在同个branch有
多个储蓄账号。

depositor (customer-name, account-number)


account (branch-name, account-number, balance)
Join Þ (customer-name, account-number, branch-name, balance)

72
Group by Evaluation求解

branch-name cust-name
Perryridge John Wong
Perryridge Jacky Chan
select branch-name, customer-name count
Uptown John Wong
from depositor, account Uptown Mary Kwan
Downtown John Wong
where depositor.account-number branch-name count
Downtown Pat Lee
= account.account-number Downtown May Cheung Perryridge 2
Uptown 2
distinct Downtown 3

branch-name cust-name branch-name cust-name


Perryridge John Wong Perryridge John Wong
Downtown Pat Lee Perryridge Jacky Chan
Uptown John Wong group by Perryridge John Wong
Perryridge Jacky Chan Uptown John Wong
Uptown Mary Kwan Uptown Mary Kwan
Downtown John Wong Downtown John Wong
Perryridge John Wong Downtown Pat Lee
Downtown May Cheung Downtown May Cheung
73
Having Clause Having语句

n Find the name of each branch where the average account


balance is more than $700 找出平均账户余额超过700块的分行

select branch-name, avg(balance)


from account
group by branch-name
having avg (balance) >700

n predicates in the having clause are applied to each group after


the formation of groups. Having后接的语句是应用到每个分组的,
而不是全部纪录。

74
Derived Relations 衍生的relation

n Find the name(s) of branches with the maximum average


account balance.找出平均账户余额最大的分行

select branch-name
from (select branch-name, avg(balance)
from account Return avg balance
group by branch-name) of each branch
as result (branch-name, avg-balance) 用as给结果起别名
where avg-balance =
(select max(avg-balance)
from result)

75
Data Manipulation Language (DML)

n Basic SQL Query


n Arithmetic Operation
n Rename Operation
n String Operation
n Ordering the Display of Tuples
n Aggregate Operator
n Set Operation
n Division
n Group By
n NULL value

76
NULL Value 有两种case

n 值未知
n 加入该纪录的时候没有对属性进行赋值。譬:尚未为新员工分配主
管。
导师 配偶
Employee eno ename supervisor_eno spouse_name

n 纪录不具备该属性,不适用

eno ename supervisor_eno spouse_name


123 Raymond NULL NULL
200 Peter 200 Mary
300 Mary 123 Peter

77
NULL Value

eno ename supervisor_eno spouse_name


123 Raymond NULL unknown NULL
200 Peter 300 false Mary
300 Mary 123 true Peter

涉及null值的逻辑判断结果
select *
理解为非true,所以不返回
from employee
where supervisor_eno = 123 该记录

eno ename supervisor_eno spouse_name


300 Mary 123 Peter

78
NULL Value

eno ename supervisor_eno spouse_name


123 Raymond NULL unknown Alice true
200 Peter 300 Mary
300 Mary 123 Peter

select *
from employee
where supervisor_eno = 123 and
spouse_name = “Alice”

What is the answer?

79
n Traditional logic
p not p p q p and q
true false true true true
false true true false false
false true false
false false false

p q p or q
true true true
true false true
false true true
false false false 80
n Logic with unknown values
p not p p q p and q
unknown unknown true unknown unknown
unknown true unknown
false unknown false
unknown false false

p q p or q
true unknown true
unknown true true
false unknown unknown
unknown false unknown 81
NULL Value

eno ename supervisor_eno spouse_name


123 Raymond NULL unknown Alice true unknown
200 Peter 300 false Mary false false
300 Mary 123 true Peter false false

select *
from employee
where supervisor_eno = 123 and
spouse_name = “Alice”

The answer contains no tuples

82
NULL Value

eno ename supervisor_eno spouse_name


123 Raymond NULL unknown Alice true true
200 Peter 300 false Mary false false
300 Mary 123 true Peter false true

select *
from employee
where supervisor_eno = 123 or
spouse_name = “Alice”

eno ename supervisor_eno spouse_name


123 Raymond NULL Alice
300 Mary 123 Peter
83
Data Manipulation Language
(DML)
n Basic SQL Query
n Arithmetic Operation
n Rename Operation
n String Operation
n Ordering the Display of Tuples
n Aggregate Operator
n Set Operation
n Division
n Group By
n NULL value

84
Outline

n Introduction
n Data Definition Language (DDL)
n Fundamental concepts
n Data Manipulation Language (DML)
n Data Definition Language (DDL)
n Advanced concepts 更高级的概念

85
Domain Constraints 定制domain

new domain name


create domain hourly-wage numeric(5,2)
constraint wage-value-test check (value > 4.00)
name of constraint condition must be TRUE

Constraint name (optional可不写): 在运行时如果违反该约束,则会报错,


错误信息包含相应约束的名字。

规定了hourly-wage是一个5位数的十进制数, 其中小数点后
n

有2位。 该域有一个约束,确保每小时工资大于 4.00。

86
Primary and Candidate Keys in SQL

n 除主键外,用UNIQUE指明其他的candidate keys。
n 伴随着约束 -- 用unique声明的属性组合,在实际存储中也必须是唯一的,
即不同纪录不能有相同值。

CREATE TABLE Enrolled CREATE TABLE Enrolled


(sid CHAR(20) (sid CHAR(20)
cid CHAR(20), cid CHAR(20),
grade CHAR(2), grade CHAR(2),
PRIMARY KEY (sid,cid) ) PRIMARY KEY (sid),
UNIQUE (cid, grade) )
• 滥用的话,可能会导致现实中一些纪录不能被成功存储。因为IC(数
据完整性) 会确保该约束成立

87
Foreign Keys in SQL

CREATE TABLE Enrolled


(sid CHAR(20), cid CHAR(20), grade CHAR(2),
PRIMARY KEY (sid,cid),
FOREIGN KEY (sid) REFERENCES Students )

n Enrolled表中的纪录,在加入表时,其相应的sid必须存在于students表格中
,不然其插入会被拒绝。
Enrolled
Students

88
Referential Integrity 引用完整性

What should be done if a Students tuple is deleted?

n (cascading deletion级联删除):把其相应在enrolled纪录也删除
n 拒绝该操作,不让删除被Enrolled表所引用的student
n 设置默认值,被删学生在enrolled相关纪录的sid变成一个默认值
n 变成null 值 (not applicable in this example because sid is part
of the primary key).

89
Referential Integrity 引用完整性

SQL92: SQL的一个国际标准
nSQL92 包括所有 4 个删除和更新选项 CREATE TABLE Enrolled
(sid CHAR(20),
n默认为 NO ACTION(删除被拒绝)
cid CHAR(20),
nCASCADE(同时删除引用已删除元组的 grade CHAR(2),

所有元组) PRIMARY KEY (sid,cid),


FOREIGN KEY (sid)
nSET NULL / SET DEFAULT(设 REFERENCES Students
ON DELETE CASCADE
置引用元组的外键值) ON UPDATE CASCADE)

ON UPDATE CASCADE : 表示同步更新


如果更新 Students 元组的主键,则该学生在Enrolled中的 sids 也会
更新。
90
加了关键词“on delete cascade”以后,删除customer里的纪录,
会导致其相应在depositor的纪录也被删除。

Customer Depositor

91
Participation Constraints参与约束

n Total participation: Every department have a manager.


n 图里表明了departments totally participates in manages,所以

实际存储时其foreign key EID 属性是不能为空的

since
name dname
HKID lot did budget

Employees Manages Departments

Works_In

since
92
Participation Constraints in SQL

We can capture total participation constraints using


NOT NULL. 用not null表示该constraint

CREATE TABLE Department(


did INTEGER,
dname CHAR(20),
budget REAL,
EID CHAR(11) NOT NULL,
PRIMARY KEY (did),
FOREIGN KEY (EKID) REFERENCES Employees,
ON DELETE NO ACTION)

93
Weak Entities

n Weak entity依赖于owner entity进行索引

name
cost pname age
EID lot

Employees Policy Dependents


Translating Weak Entity Sets

根据关系代数,weak entity与其所依赖索引的relationship
一起映射为一个relation
n 当删除owner entity时,所有拥有的弱实体也必须被删除。

CREATE TABLE Dep_Policy (


pname CHAR(20),
age INTEGER,
cost REAL,
EID CHAR(11) NOT NULL,
PRIMARY KEY (pname, EID),
FOREIGN KEY (EID) REFERENCES Employees,
ON DELETE CASCADE)
Record Deletion(delete from table_name)

n Delete all account records at the Perryridge branch

delete from account


where branch-name = “Perryridge”
n Delete all accounts at every branch located in Needham.

delete from depositor


where account-number in (select account-number
from branch, account
where branch-city = “Needham”
and branch.branch-name = account.branch-
name)
Record Insertion

n Add a new tuple to account 按默认属性顺序(创建relation时)


插入:

insert into account values (“Perryridge”, A-9732, 1200)

To reorder attributes, specify attribute names explicitly 自定义属


性顺序插入:

insert into account (branch-name, balance, account-number)


values (“Perryridge”, 1200, A-9732)

n Add a new tuple to account with balance set to null

insert into account values ( “Perryridge”, null, A-777)


Record Insertion

n Create a $200 savings account for all loan customers of the


Perryridge branch. Let the loan number serve as the account
number for the new savings account. 给所有在Perryridge分行的
贷款顾客创建200块的储蓄账户。并且把贷款账号作为该储蓄账
户的账号

insert into account


select loan-number, 200, branch-name 1. 以贷款账号
from loan 创建储蓄账户
where branch-name=“Perryridge”

insert into depositor


select customer-name, loan-number 2. 把储蓄账户跟
from loan, borrower 顾客关联起来
where branch-name=“Perryridge”
and loan.account-number = borrower.account-number
Record updates

将所有余额超过 10,000 美元的账户增加 6%;所有其他帐户收到 5%。


n Increase all accounts with balance over $10,000 by 6%; all
other accounts receive 5%.
n 写两条更新语句:
update account
set balance = balance *1.06
where balance >10000

update account
set balance = balance *1.05
where balance £ 10000
n 使用 case 语句可以做得更好
Record updates

n 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
Views

提供一种机制,从relation中创建view,可以实现隐藏部分relation
的属性。

create view view-name as <query expression>

where: <query expression> is any legal SQL query

101
Views
EXAMPLE: Create a view from loan(branch-name, loan-number,
amount) that hides the amount information. 隐藏amount列
create view branch-loan as
select branch-name, loan-number
from loan

QUERY: Find all loans in the Perryridge branch (from view


branch-loan)
select loan-number
from branch-loan
where branch-name = “Perryridge”

授权一个用户可以访问branch-loan,但不可以访问loan,相当于
不让他知道amount列。
102
Views

n We can remove the views:


Drop view branch-loan

103
Two Components in SQL

n Assertion断言
n checking
n Trigger
n checking and manipulation

104
Assertion

n An assertion 是一个时时刻刻都要遵循的约束
create assertion <assertion-name> check <predicate>
n 与一般约束的区别:
n constraint与单个relation关联,并在该特定关系有更新时进行

检查。
n 一个断言可能与多个关系相关联,并且每次在任何地方有更

新时都会进行检查。
n 即,Assertion的语句可以涉及多个relations

n 符合SQL语法的逻辑语句都可以用作其中的predicate

105
Assertion Example
The sum of all loan amounts for each branch must be less than the sum of
all account balances at the branch. 每个分行的总贷款额必须小于其总存款
额。

create assertion sum-constraint check


(not exists (select * from branch
where (select sum(amount) from loan
where loan.branch-name=branch.branch-name)
>=
(select sum(amount) from account
where. account.branch-name =branch.branch-name)))

Note that the assertion refers to multiple tables. Therefore it cannot be


included as a constraint in the definition of loan or amount. 这里的assertion
用到了多个relations,所以是没办法单单写成在loan or account relation里的
一个约束。

106
Assertion Example
The sum of all loan amounts for each branch must be less than the sum
of all account balances at the branch. 每个分行的总贷款额必须小于其总
存款额。

create assertion sum-constraint check


该分行的总贷款额
(not exists (select * from branch
where (select sum(amount) from loan
where loan.branch-name=branch.branch-name)
>=
(select sum(amount) from account
where loan.number-name=branch.branch-name)
)) 该分行的总存款额

Note that the assertion refers to multiple tables. Therefore it cannot be


included as a constraint in the definition of loan or amount. 这里的
assertion用到了多个relations,所以是没办法单单写成在loan or account
relation里的一个约束。
107
Trigger 触发器

n trigger:当数据库产生特定修改后,触发器的语句就会被执行

n 要设计触发机制,我们必须
n 指定触发条件

n 指定触发后的执行语句

108
Trigger Example

define trigger overdraft on update of account T


(if new T.balance < 0
then (insert into loan values
(T.branch-name, T.account-number, - new T.balance);
insert into borrower
(select customer-name, account-number
from depositor
where T.account-number = depositor.account-number);
update account S
set S.balance =0
where S.account-number =T.account-number))

New指代的是更新后的值,如果不加new,则是指代更新前的值。

109
Trigger Example

define trigger overdraft on update of account T


(if new T.balance < 0
then (insert into loan values
(T.branch-name, T.account-number, - new T.balance);
insert into borrower
(select customer-name, account-number
from depositor
where T.account-number = depositor.account-number);
update account S
set S.balance =0
where S.account-number =T.account-number))

New指代的是更新后的值,如果不加new,则是指代更新前的值。

如果存款账户余额为负数,那么把该存款账户变成贷款账户
110
Trigger Example

define trigger overdraft on update of account T 创建贷款账户,额度为该负


(if new T.balance < 0 存款的负数
then (insert into loan values
(T.branch-name, T.account-number, - new T.balance);
insert into borrower
(select customer-name, account-number
from depositor
where T.account-number = depositor.account-number);
update account S
set S.balance =0
where S.account-number =T.account-number))

New指代的是更新后的值,如果不加new,则是指代更新前的值。

如果存款账户余额为负数,那么把该存款账户变成贷款账户
111
Trigger Example

define trigger overdraft on update of account T


(if new T.balance < 0
then (insert into loan values
(T.branch-name, T.account-number, - new T.balance);
找到原存
款账户的 insert into borrower
(select customer-name, account-number
顾客名,
from depositor
以改 名字
记录该贷 where T.account-number = depositor.account-number);
update account S
款账户的
顾客名 set S.balance =0
where S.account-number =T.account-number))

New指代的是更新后的值,如果不加new,则是指代更新前的值。

如果存款账户余额为负数,那么把该存款账户变成贷款账户
112
Trigger Example

define trigger overdraft on update of account T


(if new T.balance < 0
then (insert into loan values
(T.branch-name, T.account-number, - new T.balance);
insert into borrower
(select customer-name, account-number
from depositor
where T.account-number = depositor.account-number);
设置原存 update account S
款账户的 set S.balance =0
余额为0 where S.account-number =T.account-number))

如果存款账户余额为负数,那么把该存款账户变成贷款账户
113

You might also like