第三章关系数据库标准语言 sql (2)idke.ruc.edu.cn/xfmeng/course/introduction to...
TRANSCRIPT
第三章 关系数据库标准语言SQL (2)
2012/3/21
上节课……• SQL:
– SQL86,SQL89,SQL92,SQL99– DDL,DML,DCL
• DDL– 基本表,视图,索引
– CREATE TABLE,CREATE INDEX– ALTER TABLE, – DROP TABLE, DROP INDEX– 数据字典与DDL的处理
Major Components of SQL
• DDL --- Data Definition Language used to define tables.
• DML --- Data Manipulation Language used to manipulate data directly.– Query:Select– Modify: Insert, Update, Delete
• DCL---Data Control Language
Basic Structure of an SQL Query
select target-attribute-listfrom table-listwhere conditions
Relational algebra correspondence:• select-clause projection (π)• from-clause Cartesian product (×)• where-clause selection (σ)
基本查询(1)
Relation schemas under consideration:
Students (SSN, Name, GPA, Age)
Courses (Course_no, Title, Dept_Name)
Enrollment (SSN, Course_no, Grade)
基本查询(2)
Find the SSN, Name and GPA of all students whose GPA is higher than 3.8.
SQL> select SSN, Name, GPA2 from Students3 where GPA > 3.8;
基本查询(3)
• To retrieve all attributes of a relation, use the shorthand * .
Find all students whose GPA is higher than 3.8.
select * from Students where GPA > 3.8• The where-clause may be absent.Find the Names of all students.
select Name from Students
基本查询(4)
• The select command does not remove duplicate rows in the result table.
• Duplicate rows can provide additional information.
• It may be costly to remove duplicate rows.
• To remove duplicate rows, select distinct should be used.
基本查询(5)Example: Find the Names of all students
without duplicate rows.select distinct Name from Students
• select all can be used to explicitly request that all duplicate rows are kept.
Questions:• How select distinct is implemented?• Is the following query an efficient query?
select distinct SSN, Name from Students
基本查询(6)
Example: Find the names of all students whose GPA is between 3.5 and 3.8.
select Name from Studentswhere GPA between 3.5 and 3.8
• not between ... and … is the opposite of between … and ...
基本查询(7)
Example: Find the course numbers and titles of all courses whose title contains “systems”.
select Course_no, Title from Courseswhere Title like `%systems%'
• % matches 0 or more characters.
基本查询(8)
Example: Find all students whose name starts with M and is six-character long.
select * from Studentswhere Name like `M_ _ _ _ _'
• _ matches exactly one character
• not like is the opposite of like.
基本查询(9)
Example: Find all students whose name contains a _.select * from Studentswhere Name like `%\_ %’ escape ‘\’
• Escape character can be defined in a
query.
基本查询(10)
Consider relation: Employees (SSN, Name, Age, Manager_SSN)• Find all employees who have no managers.
select * from Employees where Manager-SSN is null
• Find all employees who have a manager.select * from Employeeswhere Manager-SSN is not null
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------ ------- -------------------------… …… ……
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_Salaryfrom Employees
• “as” is optional.
Result: SSN Name Annual_Salary------ ------- -------------------------… …… ……
Computation in SQL (3)例使用列别名改变查询结果的列标题
SELECT Sname NAME,'Year of Birth: ' BIRTH,2000-SageBIRTHDAY
FROM Student;输出结果:
NAME BIRTH BIRTHDAY------- ---------------- ---------李勇 Year of Birth: 1976刘晨 Year of Birth: 1977王名 Year of Birth: 1978张立 Year of Birth: 1978
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.
Computation in SQL (4)
Examples:
select SSN, upper(First_Name || ' ' || Last_Name)
from Employees
select substr(SSN, 6, 4), Gradefrom Enrollmentwhere Course_no = 'CS532'
Computation in SQL (5)
Common Oracle Functions:ceil(x) : smallest integer >= x floor(x) : largest integer <= x 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
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
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
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;
Computation in SQL (9)
• Oracle’s decode function.select SSN, Name,
decode(Year, 1, ‘freshman’,2, ‘sophomore’,3, ‘junior’,4, ‘senior’) Status
from Students
Computation in SQL (10)
• Any computation involves a null value yields a null value.
Eaxmple: Consider table Employees
Emp# Name Salary Bonus123 Smith 25000 5000234 John 28000 null
Computation in SQL (11)
select Name, Salary+Bonus Total_wagefrom Employees
result: Name Total_wageSmith 30000John null
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.
Computation in SQL (11)
select Name, Salary+ nvl(Bonus, 0) Total_wage from Employees
result: Name Total_wageSmith 30000John 2800
Date Arithmetic in Oracle (1)
One may add/subtract days to/from a date. • Month and year boundaries will be taken
care of automatically.select Name, Birth_date + 20
from Students
If a student’s birth date is 23-DEC-95, then Birth_date + 20 returns 12-JAN-96.
Date Arithmetic in Oracle (2)• Calculate age from birth date:
select Name,
floor((sysdate – birthdate)/365.25) Age
from Students
• Add a month to a date:
add_month(date_value, an integer)
• Compute number of months between two dates: months_between(date1, date2)
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)
连接查询(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)))
连接查询(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))
连接查询(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
连接查询(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.
连接查询(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”?
连接查询(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?
连接查询(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.
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.Title from Courses join Departments on
Dept_Name = Namewhere Location = ‘INFO'
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'
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 null
null null c6 d3 e2
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.
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
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 on
Employees.Proj_no = Projects.Proj_no
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
集合查询 (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)
集合查询 (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.
集合查询 (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_Students where Dept_Name = 'CS')
intersect(select SSN, Name from Instructors where Dept_Name = 'CS')
集合查询 (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')
集合查询 (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.
嵌套查询(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')
嵌套查询(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.
嵌套查询(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'))
嵌套查询(4)Definition: If some attributes of a relation
declared in the from clause of an outer query are referenced in the where clause of an inner query, then the two queries are said to be correlated, and the inner query is called a correlated inner query.
Example: Consider the relations:Employees (SSN, Name, Age), Dependents (Name, Sex, ESSN)
嵌套查询(4)Find the names of all employees who
has a dependent with the same name.select Name from Employees where Name in
(select Name from Dependents where ESSN = SSN)
Evaluation of correlated queries: for each tuple in the outer query, evaluate the inner query once.
嵌套查询(5)
The scoping rule of attribute names: select ... from R1, ..., Rk where ...
(select ... from S1, ..., Sm where S1.A = R2.B and C = D and ... )
(1) If attribute C is not an attribute in S1, ..., Sm, and C is an attribute in some Ri, then C = Ri.C.
(2) If C is in both Sj and Ri, then C = Sj.C.
嵌套查询(6)• In Oracle, in accepts multi-column list.Example: Find all enrollments that have
25 years old students taking CS courses. select * from Enrollment where (SSN, Course_no) in (select s.SSN, c.Course_no from Students s, Courses cwhere s.Age = 25 and c.Dept_Name = 'CS')
嵌套查询(7)Example: Find the names of those students
who are 18 or younger and whose GPA is higher than the GPA of some students who are 25 or older.
select Name from Students where Age <= 18 and GPA >some
(select GPA from Studentswhere Age >= 25)
嵌套查询(8)
• Other set comparison operators: <some, <=some, >=some, =some, <>some, >any, <any, <=any, >=any, =any, <>any, >all, <all, <=all, >=all, =all, <>all
• some and any have identical meaning.
嵌套查询(9)
• =some is equivalent to in
• <>some is not equivalent to not in.
• <>all is equivalent to not in.
Let x = a and S = {a, b}. Then
x <>some S is true but x not in S is false. x <>all S is also false.
• 第三章习题1-4• 补充习题
dbhw2中的查询练习