初级SQL
Data Definition Language
Create
create table r ( A 1 D 1,
A 2 D 2,
......,
A n D n ,
(integrity-constraint1),
......,
(integrity-constraintk) )
# r is the name of the relation
# each Ai is an attribute name in the schema of relation r
# Di is the data type of values in the domain of attribute Ai
Create Table Extensions
Drop Table
Alter Table Constructs
alter table r add A D
alter table r drop A
Data Manipulation Language
Deletion
delete from r where P;
Insertion
insert into course values ( ‘C5-437' , 'Database System' , 'Comp. sci. ’ ,4) ;
insert into student
select ID, name, dept_name, 0
from instructor;
Updates
update instructor
set salary = salary * 1.03
where salary > 100000;
update student S
set tot_cred = (select sum(credits)
from takes natural join course
where S.ID= takes.ID and takes.grade <> 'F' and
takes.grade is not null);
Basic Query Structure
Typical SQL query
select A1, A2, ..., An
from r1, r2, ..., rm
where P
The select Clause
The select clause list the attributes desired in the result of a query
- distinct:elimination of duplicates(去重)
- all:duplicates not be removed(default)
The where Clause
The where clause specifies conditions that the result must satisfy
The from Clause
The from clause lists the relations involved in the query
Multy Relation Query(Join)
inner join
nature join
join…using
Outer Join
on 条件是外连接声明的一部分(连接条件显示两次)
where 子句不是外连接声明的一部分
Left Outer Join
Right Outer Join
**Full Outer Join **
Additional Basic Query Structure
The Rename Operation:as
String Operations
- percent (%): The % character matches any substring.
- underscore (_): The _ character matches any character.
比如:like '%dar%’
- concatenation (using “||”) 串联
- converting from upper to lower case (and vice versa)
- upper(s)
- lower(s)
- finding string length
- lengthb(s):返回字符串s所占的字节长度,单位是字节
- length(s) :返回字符串s所占的字符长度,单位是字符
- extracting substrings: substr( string, start_position, [ length ] )
- trim(s) 去掉字符串后的空格
Attribute Specification in Select Clause
“ select * ” 查询数据字典,会慢
Ordering the Display of Tuples
order by 子句默认使用升序。 要说明排序顺序 ,我们可以用 desc 表示降序,或者用 asc 表示升序
between
SQL中记号(v1,v2,……,vn)来表示一个分量值为v1,v2,……,vn的n维元组。
Set Operations
union:并运算
intersect:交运算
except(minus) :差运算
默认去重,保留在关键字后加all
NULL
三值逻辑(Three-valued logic ):true、false、unknown
对待空值的方式与谓词中对待空值的方式是不同的
I ( ‘A’ , null ) , ( ’ A’ , null) I 这样的两个元组拷贝被认为是相同的
Aggregate Functions(聚集)
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
Find the number of tuples in the course relation
select count (*) from course;
SQL 不允许在用 count( • ) 时使用 distinct。在用 max 和 min 时使用 distinct 是合法的,尽管结果并无差别。
group by
- predicates in the where clause are applied before forming groups
- predicates in the having clause are applied after the formation of
group
包含聚集、 group by 或 having 子句的查询的含义可通过下述操作序列来定义 :
- 与不带聚集的查询情况类似,最先根据 from 子句来计算出一个关系 。
- 如果出现了 where 子句, where 子句中的谓词将应用到 from 子句的结果关系上。
- 如果出现了 group by 子句 , 满足 where 谓词的元组通过 group by 子句形成分组。如果没有 group by 子句,满足 where 谓词的整个元组集被当作一个分组。
- 如果出现了 having 子句,它将应用到每个分组上;不满足 having 子句谓词的分组将被抛弃。
- select 子句利用剩下的分组产生出查询结果中的元组,即在每个分组上应用聚集函数来得到单个结果元组。
Aggregate Functions – null Values and Aggregates
- All aggregate operations except count(*) ignore tuples with null values on the aggregated attributes
- if collection has only null values
- count returns 0
- all other aggregates return null
Nested Subqueries(嵌套子查询)
tests for set membership(集合成员资格 )
in or not in
tests for Set Comparison(集合的比较)
some:存在
>some: 至少比某一个要大
<some:至少比某一个小
=some 等价于 in,然而<> some 并不等价于not in
all:任意
>all:比所有的都大
<all:比所有的都小
<> all等价于not in ,但=all 并不等价于 in
tests for Set Member(空关系测试)
exists r:如果关系实例r不是空集(r ≠ Ø),则exists r为真,否则为假
not exists r:如果关系实例r 是空集(r = Ø),则not exists r为真,否则为假
# 举个例子
# Find all courses taught in both the Fall 2009 semester and in the Spring 2010 semester
select course_id
from section S
where semester='Fall' and year=2009 and
exists ( select *
from section T
where semester='Spring' and year=2010 and
S.course_id=T.course_id);
上述还包含了相关子查询(Correlated subquery),使用了来自外层查询相关名称的子查询——section S,类似与变量作用域
将"关系 A 包含关系 B" 写 成 " not exists ( B except A ) ‘’
# 还是一个例子,比较绕(应该没人这么麻烦的使用这个了吧)
# Find all students who have taken all courses offered in the Biology department.
select distinct S.ID, S.name
from student S
where not exists (
(select course_id
from course w
here dept_name='Biology')
minus
(select T.course_id
from takes T
where S.ID = T.ID));
tests for Absence of Duplicate Tuples(重复元组存在性测试 )
The unique construct tests whether a subquery has any duplicate tuples in its result. (Evaluates to “true” on an empty set)
如果作为参数的子查询结果中没有重复的元组, unìque 结构将返回 true 值。
Subqueries in the From Clause
Oracle 允许对子查询结果关系命名(省略掉关键字 as) ,但是不允许对关系中的属性重命名
nested subqueries in the from clause cannot use correlation variables from other relations in the from clause——using lateral
# 很少的数据库支持lateral语法
# 打印每位教师的姓名,以及他们的工资和所在系的平均工资
select name , salary , avg_salary
from instructor I1,
lateral (select avg( salary ) as avg_salary
from instructor I2
where I2.dept_name= I1.dept_name);
with: provides a way of defining a temporary view whose definition is available only to the query in which the with clause occurs
# 栗子
# Find all departments where the total salary is greater than the average of the total salary at all departments
with
dept_total (dept_name, value) as
(select dept_name, sum(salary)
from instructor group by dept_name),
dept_total_avg(value) as
(select avg(value)
from dept_total)
select dept_name
from dept_total, dept_total_avg
where dept_total.value >= dept_total_avg.value;
scalar subquerγ(标量子查询)
只返回单个元组的单个属性值的子查询