database system concepts · keys •let k r •k is a superkey of r if values for k are sufficient...
TRANSCRIPT
-
Database System Concepts
Chapter 2: Intro to Relational Model 11-Sep-20
-
Outline
• Structure of Relational Databases
• Database Schema
• Keys
• Schema Diagrams
• Relational Query Languages
• Relational Operations
-
Outline
• Structure of Relational Databases
• Database Schema
• Keys
• Schema Diagrams
• Relational Query Languages
• Relational Operations
-
Relational Model
• Use a collection of tables to represent both data and relationships among those data
• Terminology (basic notions of the relational model)
– relation/table
– tuple/row
– attributes/column
4
-
Example of a Relationattributes
(or columns)
tuples
(or rows)
-
Attribute Types
• The set of allowed values for each attributeis called the domain of the attribute
• Attribute values are (normally) required tobe atomic; that is, indivisible
• The special value null is a member of everydomain. Indicated that the value is“unknown”
• The null value causes complications in thedefinition of many operations
-
Outline
7
• Structure of Relational Databases
• Database Schema
• Keys
• Schema Diagrams
• Relational Query Languages
• Relational Operations
-
Relation Schema and Instance
• Database Schema
– Logical design of the database
– Like type definition in programming-language
• Database Instance
– Snapshot of the data
– Like variable in programming-language
-
Relation Schema and Instance
• Attributes: A1, A2, …, An• Relation schema: R = (A1, A2, …, An )
Example:
instructor = (ID, name, dept_name, salary)
• Formally, given sets D1, D2, …. Dn a relation r is
– a subset of D1 x D2 x … x Dn
– a set of n-tuples (a1, a2, …, an) where each ai Di
– the current values (relation instance) of a relation are
specified by a table
– an element t of r is a tuple, represented by a row in a table
-
Relations are Unordered
• Order of tuples is irrelevant
– tuples may be stored in an arbitrary order
– example: instructor relation with unordered tuples
-
Outline
11
• Structure of Relational Databases
• Database Schema
• Keys
• Schema Diagrams
• Relational Query Languages
• Relational Operations
-
Keys
• How to distinguish the tuples in a given relation?
– the value of the attribute values of a tuple should be able to uniquely identify the tuple
• Terminology
– Superkey
– Candidate Key
– Primary Key
– Foreign Key
-
Keys• Let K R• K is a superkey of R if values for K are sufficient to identify a
unique tuple of each possible relation r(R)
– Example: {ID} and {ID,name} are both superkeys of instructor.
• Superkey K is a candidate key if K is minimal
– Example: {ID} is a candidate key for Instructor
• One of the candidate keys is selected to be the primary key.
– which one?
• Foreign key constraint: Value in one relation must appear inanother– Referencing relation– Referenced relation– Example – dept_name in instructor is a foreign key from instructor
referencing department
Primary Key Constraint
Referential Integrity Constraint
选修 (学号, 课程号, 成绩)
-
外键
假设存在关系𝑟和𝑠:𝑟(A, B, C), 𝑠(B, D),则在关系𝑟上的属性B称作参照𝑠的外码,𝑟也称为外码依赖的参照关系,𝑠叫做外码被参照关系 例 学生(学号,姓名,性别,专业号,年龄) - 参照关系
专业(专业号,专业名称) - 被参照关系 (目标关系)
其中属性专业号 称为关系学生的外码
Instructor (ID, name, dept_name, salary) - 参照关系
Department (dept_name, building, budget) - 被参照关系
参照关系中外码的值必须在被参照关系中实际存在或为null14
Foreign Key
选修 (学号, 课程号, 成绩)课程 (课程号,课程名, 学分, 先修课号)
-
Outline
15
• Structure of Relational Databases
• Database Schema
• Keys
• Schema Diagrams
• Relational Query Languages
• Relational Operations
-
Schema Diagram for University Database
classroom (building, room_number, capacity)
department (dept_name, building, budget)
course (course_id, title, dept_name, credits)
instructor (ID, name, dept_name, salary)
section (course_id, sec_id, semester, year, building, room_number, time_slot_id)
teaches (ID, course_id, sec_id, semester, year)
student (ID, name, dept_name, tot_cred)
takes (ID, course_id, sec_id, semester, year, grade)
advisor (s_ID, i_ID)
time_slot (time_slot_id, day, start_time, end_time)
prereq (course_id, prereq_id)
-
Schema Diagram for University Database
被参照关系 参照关系
-
Outline
18
• Structure of Relational Databases
• Database Schema
• Keys
• Schema Diagrams
• Relational Query Languages
• Relational Operations
-
19
Relational Query Language
• Query Languages– allow manipulation (操纵) and retrieval (检索) of
data from a database
• Relational Query Languages– query languages for Relational Database– “real”/ “practical” query languages
• e.g. SQL
– “pure”/ “mathematical” query languages• Relational Algebra• Relational Calculus
-
20
? ?
-
Formal Relational Query Languages
Two mathematical Query Languages form the basis for “real” languages (e.g. SQL), and for implementation:
Relational Algebra: More operational (过程化), very useful for representing execution plans.
Relational Calculus: Lets users describe what they want, rather than how to compute it. (Non-operational, declarative (陈述).)
Understanding Relational Algebra & Calculus is key to understanding SQL, query processing!
-
22
Declarative vs Procedural
• Procedural programming requires that theprogrammer tell the computer what to do.– how to get the output for the range of required
inputs
– the programmer must know an appropriatealgorithm.
• Declarative programming requires a moredescriptive style.– the programmer must know what relationships
hold between various entities.
-
Why do we need Query Languages anyway?
• Two key advantages– Less work for user asking query
– More opportunities for optimization
• Relational Algebra– Theoretical foundation for SQL
– Higher level than programming language
• but still must specify steps to get desired result
• Relational Calculus– Formal foundation for Query-by-Example
– A first-order logic description of desired result
– Only specify desired result, not how to get it
-
Relational Query Languages
• Procedural vs .non-procedural, or declarative• “Pure” languages:
– Relational algebra– Tuple relational calculus– Domain relational calculus
• The above 3 pure languages are equivalent incomputing power
• We will concentrate in this chapter onrelational algebra– Not turning-machine equivalent– consists of 6 basic operations
-
Outline
25
• Structure of Relational Databases
• Database Schema
• Keys
• Schema Diagrams
• Relational Query Languages
• Relational Operations
-
Relational Operations
在某种程度上是过程化(operational)语言
六个基本运算 Select 选择
Project 投影
Union 并
set difference 差(集合差)
Cartesian product 笛卡儿积
Rename 更名(重命名)
用户输入一个或两个关系,并得到新的关系
-
Relational Operations
附加运算
Set intersection 交
Natural join 自然连接
Division 除
Assignment 赋值
-
Select Operation –selection of rows (tuples)
Relation r
A=B ^ D > 5 (r)
注:执行选择时,选择条件必须是针对同一元组中的相应属性值代入进行比较。
-
Select Operation –selection of rows (tuples)
定义:𝜎𝑝(𝑟) = {𝑡|𝑡 ∈ 𝑟 ∧ 𝑝(𝑡)},𝜎 读作sigma, 其中p为选择谓词
其中p是由逻辑连词 ⋀(与), ∨(或),¬(非)连接起来的公式。逻辑连词的运算对象可以是包含比较运算符=、=和>
-
Project Operation –selection of columns (Attributes)
Relation r:
Π𝐴,𝐶(𝑟)
-
Project Operation –selection of columns (Attributes)
例:关系r
选择属性A、C
投影:Π𝐴,𝐶(𝑟)
-
Union of two relations
Relations r, s:
r s:
-
Union of two relations并运算
定义:𝑟 ∪ 𝑠 = {𝑡|𝑡 ∈ 𝑟 𝑜𝑟 𝑡 ∈ 𝑠 }
𝑟 ∪ 𝑠条件:
等目,同元,即他们的属性数目必须相同
对任意𝑖, 𝑟的第𝑖个属性域和𝑠的第𝑖个属性域相同
例如:Π 𝑛𝑎𝑚𝑒 𝑖𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑜𝑟 ∪ Π 𝑛𝑎𝑚𝑒 (𝑠𝑡𝑢𝑑𝑒𝑛𝑡)
-
Set difference of two relations
Relations r, s:
r – s:
-
Set difference of two relations
定义:𝑟 – 𝑠 = {𝑡|𝑡 ∈ 𝑟 𝑎𝑛𝑑 𝑡 ∉ 𝑠}
𝑟 − 𝑠条件:
等目,同元,即他们的属性数目必须相同
对任意𝑖,𝑟的第𝑖个属性域和𝑠的第𝑖个属性域相同
-
joining two relations -- Cartesian-product
Relations r, s:
r x s:
A B
12
C D E
10102010
aabb
A B C D E
1111
10102010
aabb
2222
10102010
aabb
r
s
-
B
s.Br.B
Cartesian-product – naming issue
Relations r, s:
r x s:
-
Renaming a Table
• Allows us to refer to a relation, (say E) by more than one name.
𝜌𝑥(𝐸)
returns the expression E under the name X Relations r
𝑟 × 𝜌𝑠(𝑟)α
α
β
β
1
1
2
2
α
β
α
β
1
2
1
2
r.A r.B s.A s.B
-
Composition of Operations• Can build expressions using multiple operations
• Example: 𝜎𝐴=C(𝑟 × 𝑠)
r s
𝜎𝐴=C(𝑟 × 𝑠)
𝑟 × 𝑠 =
A B
𝛼 1
𝛽 2
C D E
𝛼 10 a
𝛽 10 a
𝛽 20 b
𝛾 10 b
A B C D E
𝛼 1 𝛼 10 a
𝛼 1 𝛽 10 a
𝛼 1 𝛽 20 b
𝛼 1 𝛾 10 b
𝛽 2 𝛼 10 a
𝛽 2 𝛽 10 a
𝛽 2 𝛽 20 b
𝛽 2 𝛾 10 b
A B C D E
𝛼 1 𝛼 10 a
𝛽 2 𝛽 20 a
𝛽 2 𝛽 20 b
-
银行示例
40
branch (branch-name, branch-city, assets)
customer (customer-name, customer-street,customer-city)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
-
查询示例
41
例1:找出贷款额大于$1200的元组
𝜎𝑎𝑚𝑜𝑢𝑛𝑡> 1200(𝑙𝑜𝑎𝑛)
例2:找出贷款大于$1200的贷款号
loan (loan-number, branch-name, amount)
ෑ𝑙𝑜𝑎𝑛−𝑛𝑢𝑚𝑏𝑒𝑟
(𝜎𝑎mo𝑢𝑛𝑡>1200(𝑙𝑜𝑎𝑛))
-
例3:找出有贷款或有帐户或两者兼有的所有客户姓
Π𝑐𝑢𝑠𝑡𝑜𝑚𝑒𝑟−𝑛𝑎𝑚𝑒 borrower ∪ Π𝑐𝑢𝑠𝑡𝑜𝑚𝑒𝑟−𝑛𝑎𝑚𝑒(𝑑𝑒𝑝𝑜𝑠𝑖𝑡𝑜𝑟)
例4:找出至少有一个贷款及一个账户的客户姓名
Π𝑐𝑢𝑠𝑡𝑜𝑚𝑒𝑟−𝑛𝑎𝑚𝑒 borrower ∩ Π𝑐𝑢𝑠𝑡𝑜𝑚𝑒𝑟−𝑛𝑎𝑚𝑒(𝑑𝑒𝑝𝑜𝑠𝑖𝑡𝑜𝑟)
查询示例
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
-
例5:找出在Perryridge 分支机构有贷款的顾客姓名
• 查询1:
Π𝑐𝑢𝑠𝑡𝑜𝑚𝑒𝑟−𝑛𝑎𝑚𝑒(𝜎𝑏𝑟𝑎𝑛𝑐ℎ−𝑛𝑎𝑚𝑒="𝑃𝑒𝑟𝑟𝑦𝑟𝑖𝑑𝑔𝑒"(𝜎𝑏𝑜𝑟𝑟𝑜𝑤𝑒𝑟.𝑙𝑜𝑎𝑛−𝑛𝑢𝑚𝑏𝑒𝑟=𝑙𝑜𝑎𝑛.𝑙𝑜𝑎𝑛−𝑛𝑢𝑚𝑏𝑒𝑟 bo𝑟𝑟𝑜𝑤𝑒𝑟 × 𝑙𝑜𝑎𝑛 ))
• 查询2:
Π𝑐𝑢𝑠𝑡𝑜𝑚𝑒𝑟−𝑛𝑎𝑚𝑒(𝜎𝑏𝑜𝑟𝑟𝑜𝑤𝑒𝑟.𝑙𝑜𝑎𝑛−𝑛𝑢𝑚𝑏𝑒𝑟=𝑙𝑜𝑎𝑛.𝑙𝑜𝑎𝑛−𝑛𝑢𝑚𝑏𝑒𝑟𝑏𝑜𝑟𝑟𝑜𝑤𝑒𝑟 × 𝜎𝑏𝑟𝑎𝑛𝑐ℎ−𝑛𝑎𝑚𝑒="𝑃𝑒𝑟𝑟𝑦𝑟𝑖𝑑𝑔𝑒" 𝑙𝑜𝑎𝑛
查询示例
查询2更好
loan (loan-number, branch-name, amount)borrower (customer-name, loan-number)
-
例6:找出在Perryridge分支机构有贷款,但在其他分支机构没有账号的顾客姓名
• 查询1:
Π𝑐𝑢𝑠𝑡𝑜𝑚𝑒𝑟−𝑛𝑎𝑚𝑒(𝜎𝑏𝑟𝑎𝑛𝑐ℎ−𝑛𝑎𝑚𝑒="𝑃𝑒𝑟𝑟𝑦𝑟𝑖𝑑𝑔𝑒"
(𝜎𝑏𝑜𝑟𝑟𝑜𝑤𝑒𝑟.𝑙𝑜𝑎𝑛−𝑛𝑢𝑚𝑏𝑒𝑟=𝑙𝑜𝑎𝑛.𝑙𝑜𝑎𝑛−𝑛𝑢𝑚𝑏𝑒𝑟(𝑏𝑜𝑟𝑟𝑜𝑤𝑒𝑟 × 𝑙𝑜𝑎𝑛)))
−Π𝑐𝑢𝑠𝑡𝑜𝑚𝑒𝑟−𝑛𝑎𝑚𝑒(𝑑𝑒𝑝𝑜𝑠𝑖𝑡𝑜𝑟)
• 查询2:
Π𝑐𝑢𝑠𝑡𝑜𝑚𝑒𝑟−𝑛𝑎𝑚𝑒(𝜎𝑏𝑜𝑟𝑟𝑜𝑤𝑒𝑟.𝑙𝑜𝑎𝑛−𝑛𝑢𝑚𝑏𝑒𝑟=𝑙𝑜𝑎𝑛.𝑙𝑜𝑎𝑛−𝑛𝑢𝑚𝑏𝑒𝑟
𝑏𝑜𝑟𝑟𝑜𝑤𝑒𝑟 × 𝜎𝑏𝑟𝑎𝑛𝑐ℎ−𝑛𝑎𝑚𝑒="𝑃𝑒𝑟𝑟𝑦𝑟𝑖𝑑𝑔𝑒" 𝑙𝑜𝑎𝑛
− Π𝑐𝑢𝑠𝑡𝑜𝑚𝑒𝑟−𝑛𝑎𝑚𝑒(𝑑𝑒𝑝𝑜𝑠𝑖𝑡𝑜𝑟)
查询示例loan (loan-number, branch-name, amount)borrower (customer-name, loan-number)
-
查询示例1234
1234
d
123
4
account (account-number, branch-name, balance)
例7:找出银行中最大的账户余额
将account关系重命名为d
第一步:找出由非最大余额构成的临时关系
ෑ𝑎𝑐𝑐𝑜𝑢𝑛𝑡.𝑏𝑎𝑙𝑎𝑛𝑐𝑒
(𝜎𝑎𝑐𝑐𝑜𝑢𝑛𝑡.𝑏𝑎𝑙𝑎𝑛𝑐𝑒
-
附加运算
定义一些附加运算,它们虽不能增加关系代数的表达能力,
但却可以简化一些常用的查询
集合交
自然连接
除
赋值
-
Set intersection of two relations
Relation r, s:
r s
Note: r s = r – (r – s)
-
定义:𝑟 ∩ 𝑠 = { 𝑡 | 𝑡 ∈ 𝑟 𝑎𝑛𝑑 𝑡 ∈ 𝑠 }
假设:
𝑟和𝑠同元
𝑟和𝑠的属性域是可兼容的
𝑟 ∩ 𝑠 = 𝑟 − (𝑟 − 𝑠)
Set intersection of two relations
-
Joining two relations – Natural Join
• Let r and s be relations on schemas R and S respectively.
• “natural join” of relations R and S is a relation on schema R ∪S obtained as follows:
– Consider each pair of tuples tr from r and ts from s.
– If tr and ts have the same value on each of the attributes in R∩S, add a tuple t to the result, where
• t has the same value as tr on r
• t has the same value as ts on s
-
𝑟 ⋈ 𝑠
例:𝑅 = (𝐴,𝐵,𝐶,𝐷) 𝑆 = (𝐸,𝐵,𝐷)
关系𝑟和𝑠自然连接的结果模式为:(𝐴,𝐵,𝐶,𝐷,𝐸)
𝑟 ⋈ 𝑠 = Π𝑟.𝐴,𝑟.𝐵,𝑟.𝐶,𝑟.𝐷,𝑠.𝐸(𝜎𝑟.𝐵=𝑠.𝐵∧𝑟.𝐷=𝑠.𝐷𝑟 × 𝑠)
设关系𝑟和𝑠分别代表模式𝑅和𝑆,那么𝑟 ⋈ 𝑠是对模式𝑅 ∪ 𝑆运算后的关系表示:
考虑𝑟的每一个元组𝑡𝑟,𝑠的每一个元组𝑡𝑠 如果𝑡𝑟和𝑡𝑠在𝑅 ∩ 𝑆的公共属性下有相同的值,那么向结果集中插入元组 𝑡:
– 元组𝑡与𝑡𝑟有相同的值
– 元组𝑡与𝑡𝑠有相同的值
Natural Join Example
-
例:关系𝑟,𝑠
A B C D
12412
aabab
r
A B C D E
11112
aaaa b
B D E
13123
aaabb
s r s
注:(1) 𝑟, 𝑠必须含有共同属性 (名, 域对应相同),(2) 连接二个关系中同名属性值相等的元组(3) 结果属性是二者属性集的并集,但消去重名属性。
Natural Join Example
-
Notes about Relational Languages
• Each Query input is a table (or set of tables)
• Each query output is a table.
• All data in the output table appears in one of the input tables
-
Summary of Relational Algebra Operators
Symbol (Name) Example of Use
(Selection) σ salary > = 85000 (instructor)σ
Return rows of the input relation that satisfy the predicate.
Π (Projection) Π ID, salary (instructor)
Output specified attributes from all rows of the input relation. Remove duplicate tuples from the output.
x (Cartesian Product) instructor x department
Output pairs of rows from the two input relations that have the same value on all attributes that have the same name.
∪ (Union) Π name (instructor) ∪ Π name (student)
Output the union of tuples from the two input relations.
(Natural Join) instructor ⋈ department
Output pairs of rows from the two input relations that have same value on all attributes that have same name.
⋈
- (Set Difference) Π name (instructor) -- Π name (student)
Output the set difference of tuples from two input relations.
-
• 𝑡ℎ𝑒𝑡𝑎连接:𝑟 ⋈𝜃 𝑠 = 𝜎𝜃 𝑟 × 𝑠
• 𝜃是模式 𝑅 ∪ 𝑆 属性上的谓词
• 𝑡ℎ𝑒𝑡𝑎连接是自然连接的扩展
theta 连接
-
除运算
𝑟 ÷ 𝑠适用于包含了“对所有的”此类短语的查询
例:查询选修了所有课程的学生的学号
Sno Cno Grade
95001 1 92
95001 2 85
95001 3 88
95002 2 90
95002 3 80
÷
course
=95001
Sno
Cno
1
2
3
Sno, Cno (enrolled) ÷ Cno (course)
enrolled
除运算
-
除运算
设r和s分别代表模式R和S的关系
R = (A1,…,Am,B1,…,Bn)
S = (B1,…,Bn)
r s 的结果代表模式 R–S 的关系:
–R –S = (A1,…,Am)
–r s = {t|tR-S(r) us(tur)}
注:商来自于R-S(r),并且其元组t与s所有元组的拼接被r覆盖
除运算
-
除运算
例:关系r,s
r s =
A
(r s = {t|tR-S(r) [us(tur)]})
B
1
2
A B
1 2 3 1 1 1 3 4 6 1 2
r
s
除运算
-
A B C D E
a a 1 b a 1 b b 1 a a 1 a c 2 a b 3 a a 1 a b 1 c b 1
r s =
D
a b
E
11
A B C
ba
r
s
A B
bb
C D
E
a a 1a. 1b. 1
a a 1a b 3
a c 2 a a 1 a b 1
c b 1
1
2
3
4
5
除运算
例,关系r,s
除运算
-
除运算
A B C
a1 b1 c1
a2 b1 c1
a1 b2 c1
a1 b2 c2
a2 b1 c2
a1 b2 c3
a1 b2 c4
a1 b1 c5
RC
c1
A B
a1 b1
a2 b1
a1 b2
S QC
c1
c4
c2
c3
A B
a1 b2
S QB C D
b1 c1 d1
b2 c1 d2
SA
a1
Q
C
c1
c2
SA B
a1 b2
a2 b1
Q
例:从SC表中查询至少选修1号课程和3号课程的学生学号
Sno Cno Grade
95001 1 92
95001 2 85
95001 3 88
95002 2 90
95002 3 80
Cno
1
3
临时表K
÷ =Sno
95001
Sno, Cno (sc)÷K
SC
Q = R÷S
除运算
-
性质
若q = r s,则q 是满足q x s r的最大关系
用基本代数运算来定义除运算
设r(R)和s(S)已知,且S R :
r s = R-S(r) – R-S((R-S(r) x s) – R-S,S(r))
为什么:
–R-S,S(r) 重新排列r的属性顺序
–R-S(R-S(r) x s) – R-S,S(r))找出R-S(r)中的元组t,得到某些元组u s, tu r
除运算
-
赋值运算()可以使复杂的查询表达变得简单
使用赋值运算,可以把查询表达为一个顺序程序,该程序包括:
– 一系列赋值
– 一个其值被作为查询结果显示的表达式
对关系代数查询而言,赋值必须是赋给一个临时关系变量
例:可以把r s写作,
temp1 R-S (r)
temp2 R-S ((temp1 x s) – R-S,S (r))
result = temp1 – temp2
将 右侧的表达式的结果赋给 左侧的关系变量,该关系变量可以 在后续的表达式中使用
赋值运算
-
例1:找出至少拥有一个“市区”和“住宅区”分支机构的账户的客户姓名
查询1:CN(BN =“Downtown”(depositor
CN(BN =“Uptown”(depositor
account))
account))
其中,CN表示“customer-name”,BN表示“branch-name”
查询2:customer-name, branch-name (depositor account)
temp(branch-name) ({(“Downtown”), (“Uptown”)})
示例
-
例2:找出拥有布鲁克林市所有分支机构的帐户的客户姓名
customer-name, branch-name (depositor account)
branch-name (branch-city = “Brooklyn” (branch))
例3:查询选修了全部课程的学生学号和姓名 涉及表: 课程信息Course(cno, cname, pre-cno, score), 选课信息
SC(sno, cno, grade) , 学生信息Student(sno, sname, sex, age) 当涉及到求“全部”之类的查询,常用“除法” 找出全部课程号: Cno(Course)
找出选修了全部课程的学生的学号: Sno, Cno(SC) ÷ Cno(Course)
与Student表自然连接(连接条件Sno)获得学号、姓名
( Sno, Cno(SC) ÷ Cno(Course)) Sno,Sname(Student)
示例
-
64
• 学生=(学号, 姓名, 年龄, 性别, 系别)
• 课程=(课程号,课程名, 学分,先行课号)
• 教师=(职工号,姓名, 职称,年龄, 性别, 系别)
• 教材=(教材号,书名,作者名,出版号,出版社)
• 系=(系别,系名, 办公电话, 位置, 职工号)
• 选课=(学号, 课程号, 成绩)
• 参考=(课程号,教师号,教材号)
示例
-
示例 示例
找年龄不小于20的男学生
AGE≥20 ∧ SEX=‘male’(S)
给出所有学生的姓名和年龄
SN, AGE(S)
找001号学生所选修的课程号
C#( S#=001 (SC))
求选修了001号或002号课程的学生号
方案1:∏S#(C# = 001∨ C# = 002(SC))
方案2:∏S#(C# = 001 (SC))∪∏S#(C# = 002(SC))
-
示例 示例
求选修了001号而没有选002号课程的学生号
∏S#(C# = 001 (SC)) -∏S#(C# = 002(SC))
求未选修c1号课程的学生号
方案1:∏S#(S)-∏S#(C# = c1 (SC))
方案2:∏S#(C# ≠ c1 (SC))
都正确吗?
-
示例 求计算机系学生的选课情况
(sno,sname,cname,score)
思考:有几种写法?哪种效率更高?
S SC C
Sno Sname Dept Sno Cno Score Cno Cname
S1 甲 计 S1 C1 80 C1 DS
S2 乙 软 S1 C2 90 C2 DB
S3 丙 软 S2 C1 70
S4 丁 计 S3 C2 60
-
示例
-
示例 求学了c1和c2的学生学号
求各门课的最高成绩
SC
Sno Cno Score
S1 C1 80
S1 C2 90
S2 C1 70
S3 C1 60
-
Exercises• Practice Exercises:
– 2.1 2.5 2.7 2.8
• Exercises:
– 2.9 2.12 2.13
-
Thank you!
Q&A
-
示例