42
数据库系统概论讲义,第三章 SQL语言,20142 3章关系数据库SQL语言 孟小峰 [email protected] 信息学院 2014/3/13

第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

  • Upload
    lynhu

  • View
    235

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

第3章关系数据库SQL语言

孟小峰

[email protected]

信息学院

2014/3/13

Page 2: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

上节课……

• SQL:– SQL86,SQL89,SQL92,SQL99– DDL,DML,DCL

• DDL– 基本表,索引,视图

– CREATE TABLE,CREATE INDEX– ALTER TABLE, – DROP TABLE, DROP INDEX– 数据字典与DDL的处理

• Basic Select

Page 3: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Computation in SQL (1)Consider

Employees (SSN, Name, Monthly_Salary)

Example: Find the SSN, name and annual salary of each employee.

select SSN, Name, 12*Monthly_Salary from Employees

Result: SSN Name 12*Monthly_Salary------ ------- -------------------------… …… ……

Page 4: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Computation in SQL (2)• Naming the expression in the output.

– In Oracle, 12*Monthly_Salary will be used for our example.

– A more general solution is to assign a new name to the column.

select SSN, Name, 12*Monthly_Salary as Annual_Salary

from Employees• “as” is optional.

Result: SSN Name Annual_Salary------ ------- -------------------------… …… ……

Page 5: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Computation in SQL (3)

例使用列别名改变查询结果的列标题SELECT Sname NAME, 'Year of Birth: ' BIRTH,2014-Sage

BIRTHDAYFROM Student;输出结果:

NAME BIRTH BIRTHDAY

------- ---------------- ---------李勇 Year of Birth: 1976刘晨 Year of Birth: 1977王名 Year of Birth: 1978张立 Year of Birth: 1978

Page 6: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Computation in SQL (4)

Some remarks:• The set of mathematical operators

supported includes +, -, *, /.

• Other operators include length(x), lower(x), upper(x), x || y, …

• Computations and functions can be used in select and where clauses.

Page 7: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Computation in SQL (4)

Examples:

select SSN, upper(First_Name || ' '

|| Last_Name)from Employees

select substr(SSN, 6, 4), Gradefrom Enrollmentwhere Course_no = 'CS532'

Page 8: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Computation in SQL (5)

Common Oracle Functions:mod(m,n) : remainder of m divided by n power(x,y) : x raised to the power yround(n,m) : round n to the m-th digit

following the point

Page 9: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Computation in SQL (6)Common Oracle Functions:sign(x) : 0 if x = 0; 1 if x > 0; -1 if x < 0 sqrt(x) : the square root of xinitcap(s) : change the first char of each word in s

to uppercaselower(s) : change all chars in s to lowercasereplace(s,s1,s2) : change each occurrence of s1 in s

to s2 substr(s,m,n) : select n chars from s starting at the

m-th char

Page 10: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Computation in SQL (7)

Common Oracle Functions:length(s) : the length of s sysdate : the current dateuser: user id of the current user last_day : the last day of current month to_char(x) : convert x to char data type to_number(x) : convert numeric string x to

number type to_date(x) : convert x of proper format to date

type

Page 11: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Computation in SQL (8)

to_char(x) is used for displaying date and time in different formats.

• Format: to_char(attribute, ‘format_mask’)Examples:• select name, to_char(birthdate,

‘MM/DD/YYYY’) birth_date from students;• Select name, to_char(birthdate,

‘MM/DD/YYYY day HH:MM:SS’) birth_time from students;

Page 12: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Computation in SQL (9)

• Oracle’s decode function.select SSN, Name,

decode(Year, 1, ‘freshman’,2, ‘sophomore’,3, ‘junior’,4, ‘senior’) Status

from Students

Page 13: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Computation in SQL (10)

• Any computation involves a null value yields a null value.

Eaxmple: Consider table Employees

Emp# Name Salary Bonus

123 Smith 25000 5000234 John 28000 null

Page 14: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Computation in SQL (11)

select Name, Salary+Bonus Total_wagefrom Employees

result: Name Total_wage

Smith 30000John null

Page 15: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Computation in SQL (12)

• Use NVL function to convert null valuesformat: nvl(exp1, exp2)– exp1 is a source value that may be null– exp2 is the target value for converting null

Ex: select Name, Salary + nvl(Bonus, 0) Total_wage

from Employeeswill compute all total wages correctly.

Page 16: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Computation in SQL (11)

select Name, Salary+ nvl(Bonus, 0) Total_wage from Employees

result: Name Total_wage

Smith 30000John 2800

Page 17: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Find the names and GPAs of all students who take database systems.

select Name, GPAfrom Students, Enrollment, Courseswhere Title = `database systems'

and Students.SSN = Enrollment.SSNand Enrollment.Course_no =

Courses.Course_no

连接查询(1)

Page 18: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

连接查询(2)

The semantics of the previous query can be expressed (almost correctly!) as:

π Name, GPA (σ Title = `database systems’ and Students.SSN =

Enrollment.SSN and Enrollment.Courses_no = Courses.Course_no

(Students × Enrollment × Courses))= π Name, GPA (Students (Enrollment

σ Title = `database systems'(Courses)))

Page 19: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

连接查询(3)

In general,select distinct Ri.A, Rj.B, ..., Rk.Cfrom R1, R2, ..., Rn where Conditions

is equivalent toπ Ri.A, Rj.B, ..., Rk.C (σConditions

(R1 × R2 × ... × Rn))

Page 20: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

连接查询(4)Example: Find the names and GPAs of all students who take

database systems.

select Name, GPA from Students s, Enrollment e, Courses cwhere Title = `database systems'

and s.SSN = e.SSN and e.Course_no = c.Course_no

RANGE OF c is CoursesRANGE OF s is StudentsRANGE OF e is Enrollment{(s.name,s.GPA) | ∃s (s.SSN=e.SSN∧e.Course_no=c.Course_no)}

Page 21: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

连接查询(5)

• s, e and c are (relational) tuple variables (aliases, correlation names) for relations Students, Enrollment and Courses.

• Tuple variables can be used to simplify query specification and save time.

• Tuple variables are also useful when tuples from the same relation need to be compared.

Page 22: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

连接查询(6)

Example: Find all pairs of students who have the same GPA.

select s1.SSN, s2.SSN from Students s1, Students s2where s1.GPA = s2.GPA

and s1.SSN < s2.SSN• Question: Why use “s1.SSN < s2.SSN”?

Page 23: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

连接查询(7)Example: Find the names of all students whose

GPA is higher than Tom's GPA.select s1.Name from Students s1, Students s2where s2.Name = `Tom'

and s1.GPA > s2.GPA • Question: Is a student qualified if his/her GPA

is higher than some Tom's GPAs but not all Tom's GPAs?

Page 24: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

连接查询(8)

s1 SSN Name GPA123456789 John 3.6234567891 Tom 3.2345678912 Tom 3.8

s2 SSN Name GPA123456789 John 3.6234567891 Tom 3.2345678912 Tom 3.8

• Joins imply the existence semantics.

Page 25: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Joining Tables in From Clause (1)Example: Find the titles of all courses offered by

departments located in building INFO.Departments (Name, Location, Chairman)Departments1 (Dept_Name, Location,

Chairman)select Courses.Titlefrom Courses join Departments on

Dept_Name = Namewhere Location = ‘INFO'

Page 26: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Joining Tables in From Clause (2)or

select Courses.Titlefrom Courses join Departments1 using

(Dept_Name)where Location = 'EB'

orselect Courses.Titlefrom Courses natural join Departments1where Location = 'EB'

Page 27: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Outerjoin (1)R1 R2 R1 R2

A B C C D E A B C D Ea1 b1 c1 c1 d1 e1 a1 b1 c1 d1 e1 a4 b3 c2 c6 d3 e2

• The second tuples of R1 and R2 are not present in the result (called dangling tuples).

• Applications exist that require to retain dangling tuples.

Page 28: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Outerjoin (2)

10. o --- outer joinFormat: R1 o R2 Semantics: like join except (a) it retains dangling

tuples from both R1 and R2; (b) it uses null to fill out missing entries.

R1 o R2

A B C D E a1 b1 c1 d1 e1 a4 b3 c2 null nullnull null c6 d3 e2

Page 29: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Left Outerjoin and Right Outerjoin

11. --- left outer join Format: R1 R2 Semantics: like outerjoin but retains only

dangling tuples of the relation on the left.

12. --- right outer join Format: R1 R2 Semantics: like outerjoin but retains only dangling tuples of the relation on the right.

Page 30: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Outerjoining Tables in From Clause (1)

Employees (SSN, Name, Position, Proj_no)Projects(Proj_no, Title, Budget)Example: Find who works for which project.

• Not quite correct:

select SSN, Name, Title from Employees e, Projects pwhere e.Proj_no = p.Proj_no

Page 31: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Outerjoining Tables in From Clause (2)

• Identify also those who do not work for any project.

select SSN, Name, Title

from Employees left join Projects onEmployees.Proj_no = Projects.Proj_no

Page 32: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Outerjoining Tables in From Clause (3)• Identify also those projects no one works for.

select SSN, Name, Titlefrom Employees right join Projects

using (Proj_no)• Identify even those who do not work for any

project and those projects no one works for:select SSN, Name, Title from Employees natural full join

Projects

Page 33: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

集合查询 (1)

• SQL supports three set operations: union, intersect, except (Sybase) or minus (Oracle version of set difference)

• Union compatibility is required.

Consider the following two relations: G_Students(SSN, Name, GPA, GRE, Dept_Name)

Instructors(SSN, Name, Office, Salary, Dept_Name)

Page 34: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

集合查询 (2)

Example: Find the names of those people who are either a graduate student or an instructor or both in CS department.(select Name from G_Students

where Dept_Name = 'CS')union(select Name from Instructorswhere Dept_Name = 'CS')

• union removes duplicate rows.• union all keeps duplicate rows.

Page 35: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

集合查询 (3)

Example: Find the SSNs and names of those who are both a graduate student and an instructor in CS department.

(select SSN, Name from G_Studentswhere Dept_Name = 'CS')

intersect(select SSN, Name from Instructors where Dept_Name = 'CS')

Page 36: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

集合查询 (4)

Example: Find the SSNs and names of those who are an instructor but not a graduate student in CS department.

(select SSN, Name from Instructors where Dept_Name = 'CS')minus(select SSN, Name from G_Studentswhere Dept_Name = 'CS')

Page 37: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

集合查询 (4)

Example: Find all students who are 20, 22, or 24 years old.

select * from Studentswhere Age in (20, 22, 24)

• not in is the opposite of in.

Page 38: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

嵌套查询(1)

Example: Find the names of all students who take at least one course offered by the CS department.select Name from Students s, Enrollment ewhere s.SSN = e.SSN and e.Course_no in

(select Course_no from Courses where Dept_Name = 'CS')

Page 39: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

嵌套查询(2)

• The previous query is a nested query. The query outside is an outer query and the query nested under the outer query is an inner query. Multiple nestings are allowed.

• The semantics of the above query is to evaluate the inner query first and evaluate the outer query last.

• Many nested queries have equivalent non-nested versions.

Page 40: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

嵌套查询(3)The previous query is equivalent to:(1) select Name

from Students s, Enrollment e, Courses cwhere s.SSN = e.SSN and e.Course_no =

c.Course_no and c.Dept_Name = 'CS’

(2) select Name from Students where SSN in(select SSN from Enrollment

where Course_no in(select Course_no from Courses where Dept_Name = 'CS'))

Page 41: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

• 第三章习题1-4• 补充习题

dbhw2中的查询练习

Page 42: 第3章关系数据库SQL语言idke.ruc.edu.cn/xfmeng/course/Introduction to Database... · 2014-03-17 · 数据库系统概论讲义,第三章SQL语言,2014,2. Computation

数据库系统概论讲义,第三章 SQL语言,2014,2

Book Time

• 阅读:

– 《民国风度》,徐百柯

– 曾经有那样一个时代,曾经有那样一批人物。他们那相样地想着,那样地活着。他们离我们今天并不遥远,但他们守护、在意、体现的精神、传统、风骨,已与我们相去甚远

– 冯友兰:两束雄文,一抹背影