5 SQL
5 SQL
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)
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
9
NULL value
• 所有数据类型的取值范围都包含null。可以
在声明的时候加上后缀not null ,表示不允
许取null值。
n float [ (n) ] 其中 n 是用于以科学记数法存储浮点
数尾数的位数,因此决定了精度和存储大小。如果
指定了 n,则它必须是 1 到 53 之间的值。n 的默
认值为 53。
10
Schema Definition in SQL
11
Drop Table
12
Alter Table
13
Integrity Constraints (ICs)
完整性约束
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)
16
Basic SQL Query
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,
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 消除重复
P branch_name ( Loan )
23
Data Manipulation Language
24
Arithmetic Operations on Retrieved Results
n 将返回与loan相同的关系,只不过属性amount乘以 100。
25
The where Clause
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 分行拥有贷款的顾客及其贷款记录
28
The from Clause
n
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 :
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.
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”
…
…
…
distinct,所 3 33
以去重了)
Data Manipulation Language (DML)
select customer-name
from customer
where customer-street like “%Main%”
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
n asc 表示升序,也是默认模式。
n desc 表示降序
n 在很多的时候对结果排序很耗时,在必要的时候用。
39
Ordering the Display of Tuples
customer-city customer-name
Guangzhou Betty
Guangzhou Ada
Zhuhai Caroline
40
Ordering the Display of Tuples
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 列中(唯一)值的数量.
43
Aggregate Function
n Find the average account balance at the Perryridge branch.
select avg(balance)
from account
where branch-name=“Perryridge”
44
Examples of Aggregate Functions
select count(*)
from account
n * 表示所有属性
n Same as:
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
47
Set operations
48
Set operations
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.
51
Example Query – NOT IN
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”)
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
select branch-name
from branch
where assets > all
(select assets
from branch
where branch-city=“Brooklyn”)
0
(5 > all 5 ) = false
6
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
这里可以理解为两个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)
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
60
Test for Absence of Duplicate Tuples 检查有无重
复纪录
n unique 返回true如果结果里没有重复的纪录
n Find all customers who have only one account at the Perryridge branch.
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 检查有无重
复纪录
R.customer- R.account-
连接
name number
找到该分
行的储户
Account.account Account.branch_
_number name
Perryridge
Example Query – NOT UNIQUE
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.
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.
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对各组进行计数。
select指定的字段要么就要包含在Group By语句的后面,作为分组的依据;
要么就要被包含在聚合函数中。
select branch-name, balance, count(account-number)
from account
group by branch-name
n 因为同个顾客可能有多个账户。先join两个relation,找出顾客姓
名、账户跟branch的结合。
n 这里distinct是必须的。因为有些customers可能在同个branch有
多个储蓄账号。
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
74
Derived Relations 衍生的relation
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)
76
NULL Value 有两种case
n 值未知
n 加入该纪录的时候没有对属性进行赋值。譬:尚未为新员工分配主
管。
导师 配偶
Employee eno ename supervisor_eno spouse_name
n 纪录不具备该属性,不适用
77
NULL Value
涉及null值的逻辑判断结果
select *
理解为非true,所以不返回
from employee
where supervisor_eno = 123 该记录
78
NULL Value
select *
from employee
where supervisor_eno = 123 and
spouse_name = “Alice”
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
select *
from employee
where supervisor_eno = 123 and
spouse_name = “Alice”
82
NULL Value
select *
from employee
where supervisor_eno = 123 or
spouse_name = “Alice”
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
规定了hourly-wage是一个5位数的十进制数, 其中小数点后
n
86
Primary and Candidate Keys in SQL
n 除主键外,用UNIQUE指明其他的candidate keys。
n 伴随着约束 -- 用unique声明的属性组合,在实际存储中也必须是唯一的,
即不同纪录不能有相同值。
87
Foreign Keys in SQL
n Enrolled表中的纪录,在加入表时,其相应的sid必须存在于students表格中
,不然其插入会被拒绝。
Enrolled
Students
88
Referential Integrity 引用完整性
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),
Customer Depositor
91
Participation Constraints参与约束
since
name dname
HKID lot did budget
Works_In
since
92
Participation Constraints in SQL
93
Weak Entities
name
cost pname age
EID lot
根据关系代数,weak entity与其所依赖索引的relationship
一起映射为一个relation
n 当删除owner entity时,所有拥有的弱实体也必须被删除。
update account
set balance = balance *1.05
where balance £ 10000
n 使用 case 语句可以做得更好
Record updates
update account
set balance = case
when balance <= 10000
then balance *1.05
else balance * 1.06
end
Views
提供一种机制,从relation中创建view,可以实现隐藏部分relation
的属性。
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
授权一个用户可以访问branch-loan,但不可以访问loan,相当于
不让他知道amount列。
102
Views
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. 每个分行的总贷款额必须小于其总存款
额。
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. 每个分行的总贷款额必须小于其总
存款额。
n trigger:当数据库产生特定修改后,触发器的语句就会被执行
n 要设计触发机制,我们必须
n 指定触发条件
n 指定触发后的执行语句
108
Trigger Example
New指代的是更新后的值,如果不加new,则是指代更新前的值。
109
Trigger Example
New指代的是更新后的值,如果不加new,则是指代更新前的值。
如果存款账户余额为负数,那么把该存款账户变成贷款账户
110
Trigger Example
New指代的是更新后的值,如果不加new,则是指代更新前的值。
如果存款账户余额为负数,那么把该存款账户变成贷款账户
111
Trigger Example
New指代的是更新后的值,如果不加new,则是指代更新前的值。
如果存款账户余额为负数,那么把该存款账户变成贷款账户
112
Trigger Example
如果存款账户余额为负数,那么把该存款账户变成贷款账户
113