数据库系统概论

Similar documents
数据库系统概论

SQL: Interactive Queries (2)

数据库系统概论

untitled

Microsoft Word - (web)_F.1_Notes_&_Application_Form(Chi)(non-SPCCPS)_16-17.doc

天 主 教 輔 仁 大 學 社 會 學 系 學 士 論 文 小 別 勝 新 婚? 久 別 要 離 婚? 影 響 遠 距 家 庭 婚 姻 感 情 因 素 之 探 討 Separate marital relations are getting better or getting worse? -Exp

ENGG1410-F Tutorial 6

untitled

Microsoft Word - TIP006SCH Uni-edit Writing Tip - Presentperfecttenseandpasttenseinyourintroduction readytopublish

untitled

Untitled-3

<4D F736F F D205F FB942A5CEA668B443C5E9BB73A740B5D8A4E5B8C9A552B1D0A7F75FA6BFB1A4ACFC2E646F63>

1 * 1 *

Ted Codd s Relational Model Derivability, Redundancy, and Consistency of Relations Stored in Large Data Banks, IBM Research Report RJ 599 ( August 19t

Microsoft Word - A doc

13 A DSS B DSS C DSS D DSS A. B. C. CPU D. 15 A B Cache C Cache D L0 L1 L2 Cache 16 SMP A B. C D 17 A B. C D A B - C - D

习题1

2015年4月11日雅思阅读预测机经(新东方版)


ebook 165-6

Microsoft Word - ChineseSATII .doc

4. 每 组 学 生 将 写 有 习 语 和 含 义 的 两 组 卡 片 分 别 洗 牌, 将 顺 序 打 乱, 然 后 将 两 组 卡 片 反 面 朝 上 置 于 课 桌 上 5. 学 生 依 次 从 两 组 卡 片 中 各 抽 取 一 张, 展 示 给 小 组 成 员, 并 大 声 朗 读 卡

Microsoft Word - Final Exam Review Packet.docx

Untitiled

Improved Preimage Attacks on AES-like Hash Functions: Applications to Whirlpool and Grøstl

國立中山大學學位論文典藏.PDF

1對外華語文詞彙教學的策略研究_第三次印).doc

Microsoft Word doc

WWW PHP

A VALIDATION STUDY OF THE ACHIEVEMENT TEST OF TEACHING CHINESE AS THE SECOND LANGUAGE by Chen Wei A Thesis Submitted to the Graduate School and Colleg

東莞工商總會劉百樂中學

PowerPoint Presentation

The Development of Color Constancy and Calibration System

Microsoft Word - 18-p0402-c3.doc

I

<4D F736F F D2035B171AB73B6CBA8ECAB73A6D3A4A3B6CBA158B3AFA46CA9F9BB50B169A445C4D6AABAB750B94AB8D6B9EFA4F1ACE3A873>


參 加 第 二 次 pesta 的 我, 在 是 次 交 流 營 上 除 了, 與 兩 年 沒 有 見 面 的 朋 友 再 次 相 聚, 加 深 友 誼 外, 更 獲 得 與 上 屆 不 同 的 體 驗 和 經 歴 比 較 起 香 港 和 馬 來 西 亞 的 活 動 模 式, 確 是 有 不 同 特

Microsoft PowerPoint - STU_EC_Ch08.ppt

高中英文科教師甄試心得

% % % % % % ~


Microsoft Word - 口試本封面.doc

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO


100學年度大學推甄申請面試題庫



Microsoft Word 李若鶯.doc

Microsoft PowerPoint - CH 04 Techniques of Circuit Analysis


穨control.PDF

untitled

Microsoft PowerPoint - Lecture7II.ppt

UDC Empirical Researches on Pricing of Corporate Bonds with Macro Factors 厦门大学博硕士论文摘要库

Computer Architecture

Outline Speech Signals Processing Dual-Tone Multifrequency Signal Detection 云南大学滇池学院课程 : 数字信号处理 Applications of Digital Signal Processing 2

第三章 国内外小组合作学习的应用情况

ch_code_infoaccess

untitled

Logitech Wireless Combo MK45 English

Microsoft Word - 論文封面 修.doc

國 立 政 治 大 學 教 育 學 系 2016 新 生 入 學 手 冊 目 錄 表 11 國 立 政 治 大 學 教 育 學 系 博 士 班 資 格 考 試 抵 免 申 請 表 論 文 題 目 申 報 暨 指 導 教 授 表 12 國 立 政 治 大 學 碩 博 士 班 論

Oracle Database 10g: SQL (OCE) 的第一堂課

Chn 116 Neh.d.01.nis


BC04 Module_antenna__ doc

國家圖書館典藏電子全文

ebook 165-5

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

Microsoft Word - CX VMCO 3 easy step v1.doc

從詩歌的鑒賞談生命價值的建構

教務會議議程

C o n t e n t s Acceptance Allow Love Apologize Archangel Metatron Archangel Michael Ask for

VASP应用运行优化

QQGQ2.E Power Supplies, Information Technology Equipment Including Ele... 1/10

Microsoft PowerPoint - 協商談判(成大 ) [相容模式]

HKG_ICSS_FTO_sogobrilingual_100_19Feb2016_31837_tnc

Microsoft Word - 11月電子報1130.doc

DB2 (join) SQL DB2 11 SQL DB2 SQL 9.1 DB2 DB2 ( ) SQL ( ) DB2 SQL DB2 DB2 SQL DB2 DB2 SQL DB2 ( DB2 ) DB2 DB2 DB2 SQL DB2 (1) SQL (2) S

Thesis for the Master degree in Engineering Research on Negative Pressure Wave Simulation and Signal Processing of Fluid-Conveying Pipeline Leak Candi

<4D F736F F D C4EAC0EDB9A4C0E04142BCB6D4C4B6C1C5D0B6CFC0FDCCE2BEABD1A15F325F2E646F63>


PowerPoint 簡報

运动员治疗用药豁免申报审批办法


Transcription:

第 2 章 关 系 数 据 库 孟 小 峰 xfmeng@ruc.edu.cn 信 息 学 院 2014/3/4

上 节 课 关 系 完 整 性 实 体 完 整 性 / 主 码 完 整 性 参 照 完 整 性 / 外 码 完 整 性 函 数 依 赖 用 户 定 义 完 整 性 关 系 代 数 基 本 运 算 : 选 择, 投 影, 笛 卡 尔 积, 并, 差 导 出 运 算 : 交, 连 接, 除

Additional Relational Algebra Operators The new operators are all derivable from the six operators we have introduced previously. 6. --- 交 set intersection Format: R1 R2 Semantics: Return all tuples that belong to both R1 and R2. Formally: R1 R2 = { t t R1 and t R2 } Derivation from existing operators: R1 R2 = R1 - (R1 - R2) = R2 - (R2 - R1)

交 set intersection (2) Example: R1 R2 R1 - R2 A B C A B C A B C a1 b1 c1 a0 b0 c0 a3 b3 c3 a2 b2 c2 a1 b1 c1 A B C R1 R2 a3 b3 c3 a2 b2 c2 a1 b1 c1 a4 b4 c4 a2 b2 c2 R1 R2 = R1 - (R1 - R2)

7. --- join Format: R1 连 接 Join (1) join-condition R2 Semantics: Return all tuples in R1 R2 which satisfy the join condition. Derivation from existing operators: R1 join-condition R2 = σ join-condition (R1 R2) Format of join condition: R1.A op R2.B R1.A1 op R2.B1 and R1.A2 op R2.B2...

连 接 Join (2) Example: Find the names of all employees and their department locations. Employees: SSN Name Age Dept-Name 123456789 John 34 Sales 234567891 Mary 42 Service 345678912 Bill 39 null Departments: Name Location Manager Sales Binghamton Bill Inventory Endicott Charles Service Vestal Maria

连 接 Join (3) π Employees.Name, Location ( Employees Dept-Name = Departments. Name Departments) Result Name Location John Binghamton Mary Vestal

连 接 Join (4) Example: Find the names of all employees who earn more than his/her manager. Employees: SSN Name Salary Manager-SSN 123456789 John 34k 234567891 234567891 Bill 40k null 345678912 Mary 38k null 456789123 Mike 41k 345678912 π Employees.Name (Employees Employees.Manager-SSN = EMP2.SSN and Employees.Salary > EMP2.Salary ρ EMP2 (Employees))

等 值 连 接 Equijoin Definition: A join is called an equijoin if only equality operator is used in all join conditions. R1 R2 R1 R1.B = R2.B R2 A B B C A R1.B R2.B C a b b c a b b c d b c d d b b c b c a d b c c d Most joins in practice are equijoins.

自 然 连 接 Natural Join (1) Definition: A join between R1 and R2 is a natural join if There is an equality comparison between every pair of identically named attributes from the two relations. Among each pair of identically named attributes from the two relations, only one remains in the result. Natural join is denoted by with no join conditions explicitly specified.

自 然 连 接 Natural Join (2) Example: R1(A, B, C) R2(A, C, D) has attributes (A, B, C, D) in the result. Questions: (1) How to express natural join in terms of equijoin and other relational operator? R1 R2 = π R1.A, B, R2.C, D(R1 R1.A = R2.A and R1.C = R2.C R2)

自 然 连 接 Natural Join (3) (2) If there are no common attributes between R1 and R2, what is R1 R2? R1(A, B, C), R2(D, E) R1 R2 = π A, B, C, D, E (R1 φ R2) = π A, B, C, D, E (R1 R2) = R1 R2

A motivating example: 除 Division (1) Emp# Proj# Proj# 1 10 10 1 20 20 1 30 30 2 20 3 10 3 20 Which employee participates in all projects? {1}

8. --- division Format: R1 R2 除 Division (2) Restriction: Every attribute in R2 is in R1. Consider: R1(A1,..., An, B1,..., Bm) R2(B1,..., Bm) Let T = π A1,..., An (R1). Semantics: Return those tuples in T such that for every tuple t returned, the concatenation of t with every tuple in R2 is in R1.

除 Division (3) E.g.: R1 R2 T R1 R2 A B C D C D A B A B a b c d c d a b a b a b e f e f b c e d b c e f e d e d c d e d e f a b d e T = π A1,..., An (R1) Derivation from existing operators: R1 R2 = T - π A1,..., An (T R2 - R1).

除 (Division) 除 运 算 的 含 义 给 定 关 系 R (X,Y) 和 S (Y,Z), 其 中 X,Y,Z 为 属 性 组 R 中 的 Y 与 S 中 的 Y 可 以 有 不 同 的 属 性 名, 但 必 须 出 自 相 同 的 域 集 R 与 S 的 除 运 算 得 到 一 个 新 的 关 系 P(X),P 是 R 中 满 足 下 列 条 件 的 元 组 在 X 属 性 列 上 的 投 影 : 元 组 在 X 上 分 量 值 x 的 象 集 Y x 包 含 S 在 Y 上 投 影 的 集 合 R S = {t r [X] t r R π Y (S) Y x } Y x :x 在 R 中 的 象 集,x = t r [X]

除 (Division)( 续 ) 除 操 作 是 同 时 从 行 和 列 角 度 进 行 运 算 举 例 [ 例 6] R: 图 2.7(a);S: 图 2.7(b); R S: 图 2.7(c) R A B C a1 b1 c2 S B C D b1 c2 d1 a2 b3 c7 b2 c1 d1 a3 b4 c6 b2 c3 d2 a1 b2 c3 a4 b6 c6 a2 b2 c3 a1 b2 c1

除 (Division)( 续 ) 分 析 : 在 关 系 R 中,A 可 以 取 四 个 值 {a 1,a 2,a 3,a 4 } 其 中 : a 1 的 象 集 为 {(b 1,c 2 ),(b 2,c 3 ),(b 2,c 1 )} a 2 的 象 集 为 {(b 3,c 7 ),(b 2,c 3 )} a 3 的 象 集 为 {(b 4,c 6 )} a 4 的 象 集 为 {(b 6,c 6 )} S 在 (B,C) 上 的 投 影 为 {(b 1,c 2 ),(b 2,c 1 ),(b 2,c 3 ) } 显 然 只 有 a 1 的 象 集 (B,C) a1 包 含 了 S 在 (B,C) 属 性 组 上 的 投 影, 所 以 R S ={a 1 }

除 (Division)( 续 ) A sno s1 s1 s1 s1 s2 s2 s3 s4 s4 pno p1 p2 p3 p4 p1 p2 P2 p2 p4 pno p2 pno P2 p4 pno P1 p2 p4 B1 B2 B3 sno s1 s2 s3 s4 sno s1 s4 sno s1 A B1 A B2 A B3

除 (Division)( 续 ) 除 的 实 际 含 义 : 有 一 个 现 实 意 义 的 集 合, 希 望 在 另 一 个 集 合 中 找 出 包 含 该 集 合 的 元 组 集 例, 找 出 选 修 了 所 有 课 程 的 学 生 所 有 课 程 学 生 学 生 所 有 课 程 例, 找 出 选 修 了 所 有 张 三 所 选 课 的 学 生 张 三 所 选 课 学 生 学 生 张 三 所 选 课

Division (4) Example: Find the names and GPAs of all students who take all courses taken by a student with SSN = 123456789. Students (SSN, Name, GPA) Takes (SSN, Course#) Step 1: Find all courses that are taken by the student with SSN = 123456789. TEMP1 := π Course# (σ SSN = `123456789 (Takes))

Division (5) Step 2: Find the SSNs of those students who take all courses in TEMP1. TEMP2 := Takes TEMP1 Step 3: Obtain the final result. RESULT := π Name, GPA (Students TEMP2)

Division (5) Find the names of employees who participate in every project. Employees(SSN, Name, Department) Projects(Proj#, Name, Budget) Participation(SSN, Proj#) π Name (Employees Proj# (Projects))) (Participation π

Relational Algebra Example (1) Many relational algebra queries can be expressed using selection, projection and join operators by following steps: (1) Determine necessary relations to answer the query. If R1,..., Rn are all the relations needed, P are all conditions and T are all (target) attributes to be output, then form the initial query: π T (σ P (R1... Rn))

Relational Algebra Example (2) (2) If P contains a condition, say Ci, that involves only attributes in Ri, replace Ri by σ Ci (Ri) and remove Ci from P. (3) If P contains a condition, say C, that involves attributes from both Ri and Rj, replace Ri Rj by Ri C Rj (or a natural join) and remove C from P. Be careful when there are two or more joins.

Relational Algebra Example (3) Consider the following database schema: Students(SSN, Name, GPA, Age, Dept-Name) Enrollment(SSN, Course#, Grade) Courses(Course#, Title, Dept-Name) Departments(Name, Location, Phone) Query: Find the SSNs and names of all students who are CS major and who take CS532.

Relational Algebra Example (4) (1) Relations Students and Enrollment are needed. T = { Students.SSN, Students.Name } P = { Students.Dept-Name = CS, Enrollment.Course# = CS532, Students.SSN = Enrollment.SSN } The initial relational algebra query is: π Students.SSN, Name (σ Dept-Name = `CS and Course# = `CS532 and Students.SSN = Enrollment.SSN (Students Enrollment))

Relational Algebra Example (5) (2) Replace Students by σ Dept-Name = `CS (Students) and Enrollment by σ Course# = `CS532 (Enrollment). Remove the two conditions from the initial expression. (3) Replace Students Enrollment by Students Enrollment and remove Students.SSN = Enrollment.SSN from the initial expression. The final expression: π Students.SSN, Name (σ Dept-Name = `CS (Students) σ Course# = `CS532 (Enrollment))

Relational Algebra Example (6) Cartesian product is most expensive, followed by non-equijoin, followed by equijoin and natural join, followed by selection and projection. Query: Find the SSN and name of each student who is CS major together with the titles of the courses taken by the student.

Relational Algebra Example (7) π Students.SSN, Name, Title (σ Students.Dept-Name = `CS and Students.SSN = Enrollment.SSN and Enrollment.Course# = Courses.Course# (Students Enrollment Courses)) = π Students.SSN, Name, Title ((σ Students.Dept-Name = `CS (Students) Enrollment) Enrollment.Course# = Courses.Course# Courses) = π Students.SSN, Name, Title (σ Students.Dept-Name = `CS (Students) (Enrollment Courses) ) Students.SSN = Enrollment.SSN

Outerjoin (1) R1 R2 R1 R2 A B C C D E A B C D E a1 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.

Outerjoin (2) 10. o --- outer join Format: 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.

Relational Algebra Summary (1) Relational algebra operators: Fundamental operators: σ C (R), π A (R), ρ S (R), R1 R2, R1 - R2, R1 R2 Other traditional operators: R1 R2, R1 C R2, R1 R2 New operators: R1 o R2, R1 R2, R1 R2

Relational Algebra Summary (2) Some identities: σ C1 (σ C2 (R)) = σ C2 (σ C1 (R)) = σ C1 and C2 (R) π L1 (π L2 (R)) = π L1 (R), if L1 L2 R1 R2 = R2 R1 R1 (R2 R3) = (R1 R2) R3 R1 R2 = R2 R1 R1 (R2 R3) = (R1 R2) R3 Either both joins are natural or are non-natural

Relational Algebra Operations 27

Relational Algebra Operations 28

第 二 章 关 系 数 据 库 2.1 关 系 模 型 概 述 2.2 关 系 数 据 结 构 2.3 关 系 的 完 整 性 约 束 2.4 关 系 代 数 2.5 关 系 演 算 2.6 小 结 关 系 的 数 据 操 作

关 系 演 算 Relational Calculus Relational calculus query specifies what is to be retrieved rather than how to retrieve it. No description of how to evaluate a query. Based on a branch of symbolic logic called predicate calculus( 谓 词 演 算 ). When applied to databases, relational calculus is in two forms: tuple-oriented ( 元 组 )and domainoriented( 域 ). 54

关 系 演 算 Relational Calculus In first-order logic or predicate calculus, a predicate is a truth-valued function with arguments. When we substitute values for the arguments, the function yields an expression, called a proposition, which can be either true or false. 55

关 系 演 算 Relational Calculus If a predicate contains a variable, as in x is a member of students, there must be a range for x. When we substitute some values of this range for x, the proposition may be true; for other values, it may be false. If P is a predicate, then we write the set of all x such that P is true for x, as {x P(x)} Predicates can be connected using (AND), (OR), and (NOT) 56

元 组 关 系 演 算 Tuple-oriented Relational Calculus Interested in finding tuples for which a predicate is true. Based on use of tuple variables. Tuple variable is a variable that ranges over a named relation: that is, a variable whose only permitted values are tuples of the relation. 57

元 组 关 系 演 算 Tuple-oriented Relational Calculus To specify the range of a tuple variable S as the Student relation. RANGE OF S IS Student To find the set of all tuples S such that P(S) is true. {S P(S)} 58

元 组 关 系 演 算 Tuple-oriented Relational Calculus To find the Sno, Sname, Ssex, Sage, Sdept of all student with age more than 20, we write RANGE OF S IS Students {S S.sage > 20} S.sage means the value of the Sage attribute for the tuple S. 59

元 组 关 系 演 算 Tuple-oriented Relational Calculus To find a particular attribute, such as name, we write. RANGE OF S IS Students {S.sname S.age > 20} 60

元 组 关 系 演 算 Tuple-oriented Relational Calculus We can use two quantifiers to tell how many instances the predicate applies to. Existential quantifier ( there exists ) Universal quantifier ( for all ) 61

举 例 用 存 在 量 词 的 检 索 [ 例 8] 查 询 选 修 2 号 课 程 的 学 生 名 字 RANGE OF X is SC RANGE OF Y is STUDENT {Y.Sname X(X.Sno=Y.Sno X.Cno='2 } [ 例 9] 查 询 选 修 了 其 直 接 先 行 课 是 6 号 课 程 的 这 样 课 程 的 学 生 号 RANGE OF X is Course RANGE OF Y is SC {Y.Sno X (X.Cno=Y.Cno X.Pcno='6 }

举 例 [ 例 10] 查 询 至 少 选 修 一 门 其 先 行 课 为 6 号 课 程 的 学 生 名 字 RANGE OF CX is Course RANGE OF SCX is SC RANGE OF Y is Student {Y.Sname SCX (SCX.Sno=Y.Sno CX (CX.Cno=SCX.Cno CX.Pcno='6')} 可 将 元 组 关 系 演 算 公 式 变 换 为 前 束 范 式 的 形 式 : {Y.Sname SCX CX(SCX.Sno=Y.Sno CX.Cno=SCX.Cno CX.Pcno='6 }

举 例 带 有 多 个 关 系 的 表 达 式 的 检 索 [ 例 11] 查 询 成 绩 为 90 分 以 上 的 学 生 名 字 与 课 程 名 字 RANGE OF SCX is SC RANGE OF Y is Student RANGE OF Z is Course {(Y.Sname,Z.Cname) SCX (SCX.Grade 90 SCX.Sno=Y.Sno Z.Cno=SCX.Cno}

举 例 用 两 种 量 词 的 检 索 [ 例 13] 查 询 选 修 了 全 部 课 程 的 学 生 姓 名 RANGE OF CX is Course RANGE OF SCX is SC RANGE OF SX is Student {XS.Sname CX SCX (SCX.Sno=XS.Sno SCX.Cno=CX.Cno) }

除 (Division) R S: 另 一 种 定 义 t 属 于 R S, 当 且 仅 当 满 足 以 下 两 个 条 件 同 时 成 立 : 1)t 在 π R-S (r) 中 2) 对 s 中 的 每 一 个 元 组 t s, 在 r 中 都 有 元 组 t r 同 时 满 足 以 下 条 件 : t r [S] = t s [S] and t r [R-S] = t R1 R2 = T - π A1,..., An (T R2 - R1).

Relational algebra vs. Tuple-oriented Relational Calculus P66-67

2.5.1 元 组 关 系 演 算 语 言 ALPHA 一 种 典 型 的 元 组 关 系 演 算 语 言, 但 没 有 实 际 实 现 由 E.F.Codd 提 出 INGRES 所 用 的 QUEL 语 言 是 参 照 ALPHA 语 言 研 制 的 语 句 检 索 语 句 :GET 更 新 语 句 :PUT,HOLD,UPDATE,DELETE,DROP

检 索 操 作 语 句 格 式 : GET 工 作 空 间 名 [( 定 额 )]( 表 达 式 1) [: 操 作 条 件 ] [DOWN/UP 表 达 式 2] 定 额 : 规 定 检 索 的 元 组 个 数 格 式 : 数 字 表 达 式 1: 指 定 语 句 的 操 作 对 象 格 式 : 关 系 名 关 系 名. 属 性 名 元 组 变 量. 属 性 名 集 函 数 [, ] 操 作 条 件 : 将 操 作 结 果 限 定 在 满 足 条 件 的 元 组 中 格 式 : 逻 辑 表 达 式 表 达 式 2: 指 定 排 序 方 式 格 式 : 关 系 名. 属 性 名 元 组 变 量. 属 性 名 [, ]

检 索 操 作 ( 续 ) [ 例 7] 查 询 信 息 系 学 生 的 名 字 RANGE Student X GET W (X.Sname): X.Sdept='IS'

Domain-oriented Relational Calculus Uses variables that take values from domains instead of tuples of relations. If P(d 1, d 2,..., d n ) stands for a predicate with variables d 1, d 2,..., d n, then {d 1, d 2,..., d n P(d 1, d 2,..., d n )} Means the set of all domain variables d 1, d 2,..., d n for which the predicate, or formula, P(d 1, d 2,..., d n ) is true. 71

Domain-oriented Relational Calculus We often test for a membership condition, to determine whether values belong to a relation. The expression R(x, y) evaluates to true if and only if there is a tuple in relation R with values x, y for its two attributes. 72

Example - Domain-oriented Relational Calculus Find the names of all managers who earn more than 25,000. {fname, lname position, salary (Staff (lname, position, salary) position = Manager salary > 25000)} Each attribute has a (variable) name. Condition Staff (lname, position, salary) ensures domain variables are restricted to attributes of same tuple. 73

域 关 系 演 算 语 言 QBE 一 种 典 型 的 域 关 系 演 算 语 言 由 M.M.Zloof 提 出 的 QBE 1978 年 在 IBM370 上 得 以 实 现 QBE 也 指 此 关 系 数 据 库 管 理 系 统 QBE:Query By Example 基 于 屏 幕 表 格 的 查 询 语 言 查 询 要 求 : 以 填 写 表 格 的 方 式 构 造 查 询 即 : 用 示 例 元 素 ( 域 变 量 ) 来 表 示 查 询 结 果 可 能 的 情 况 查 询 结 果 : 以 表 格 形 式 显 示

域 关 系 演 算 语 言 QBE ( 续 ) QBE 操 作 框 架 关 系 名 属 性 名 操 作 条 件 域 值 或 查 询 条 件

一 检 索 操 作 操 作 步 骤 为 : (1) 用 户 提 出 要 求 ; (2) 屏 幕 显 示 空 白 表 格 ; (3) 用 户 在 最 左 边 一 栏 输 入 要 查 询 的 关 系 名, 例 如 Student; student

检 索 操 作 ( 续 ) (4) 系 统 显 示 该 关 系 的 属 性 名 ; Student Sno Sname Ssex Sage Sdept (5) 用 户 在 上 面 构 造 查 询 要 求 Student Sno Sname Ssex Sage Sdept P. T AO. C

检 索 操 作 ( 续 ) 构 造 查 询 的 几 个 要 素 : T: 示 例 元 素, 即 域 变 量 一 定 要 加 下 划 线 示 例 元 素 是 这 个 域 中 可 能 的 一 个 值, 它 不 必 是 查 询 结 果 中 的 元 素 例 如 要 求 信 息 系 的 学 生, 只 要 给 出 任 意 的 一 个 学 生 名 即 可, 而 不 必 真 是 信 息 系 的 某 个 学 生 名 C: 查 询 条 件, 不 用 加 下 划 线 可 使 用 比 较 运 算 符 >,,<,,= 和 其 中 = 可 以 省 略 P.: 打 印 操 作 符, 指 定 查 询 结 果 所 含 属 性 列 AO./DO.: 排 序 要 求

检 索 操 作 ( 续 ) (6) 屏 幕 显 示 查 询 结 果 Student Sno Sname Ssex Sage Sdept 李 勇 张 立 C

关 系 演 算 与 关 系 代 数 关 系 演 算 是 描 述 性 (descriptive) 形 式 的, 而 关 系 代 数 是 说 明 性 (prescriptive) 形 式 的 关 系 演 算 描 述 了 问 题 是 什 么, 而 关 系 代 数 说 明 了 解 决 问 题 的 过 程 或 者, 可 以 说, 关 系 代 数 是 过 程 化 的 ( 诚 然, 是 高 级 的, 但 仍 然 是 过 程 化 的 ); 而 关 系 演 算 是 非 过 程 化 的 上 述 区 别 仅 仅 是 表 面 上 的 实 际 上, 关 系 代 数 和 关 系 演 算 在 逻 辑 上 是 等 价 的 即 每 一 个 代 数 表 达 式 都 有 一 个 等 价 的 演 算 表 达 式, 每 一 个 演 算 表 达 式 都 有 一 个 等 价 的 代 数 表 达 式 两 者 是 一 一 对 应 关 系 所 以 两 者 的 区 别 仅 仅 是 形 式 上 的 可 以 证 明, 关 系 演 算 更 接 近 自 然 语 言, 而 关 系 代 数 更 像 程 序 语 言 特 别 地, 没 有 哪 一 种 方 法 真 正 地 比 另 外 一 种 更 加 非 过 程 化

比 较 举 例 元 组 关 系 演 算 域 关 系 演 算 查 询 选 修 了 全 部 课 程 的 学 生 号 码 和 姓 名 RANGE OF CX is Course RANGE OF SCX is SC RANGE OF SX is Student {XS.Sname, XS.Sno CX SCX (SCX.Sno=XS.Sno SCX.Cno=CX.Cno) } {Sname, Sno sno,sname(student(sno,sname) ) Cno( Course(cname, cno) =>SC(sno,cno) } 关 系 代 数 π Sno,Cno (SC) π Cno (Course) π Sno,Sname (Student) 74

关 系 演 算 与 关 系 代 数 Codd 认 为, 至 少 关 系 代 数 跟 关 系 演 算 一 样, 具 有 强 大 的 表 达 能 力 ( 参 看 [6.1]) 他 给 出 了 一 算 法 证 明 了 这 一 点 这 一 算 法 叫 Codd 简 约 算 法 通 过 这 个 算 法, 任 意 一 个 演 算 表 达 式 都 可 以 简 约 为 在 语 法 上 等 价 的 代 数 表 达 式 如 果 一 门 语 言 具 备 了 关 系 演 算 这 样 的 功 能, 那 么 可 以 说 是 满 足 关 系 完 备 性 (relational complete) 的 由 于 关 系 代 数 具 备 关 系 完 备 性, 故 为 了 表 现 某 一 给 定 语 言 L 具 备 此 特 征, 就 必 须 充 分 表 现 : (a)l 包 括 类 似 八 个 代 数 操 作 符 的 操 作 ( 实 际 上, 要 充 分 表 现 关 系 代 数 的 五 个 基 本 的 操 作 ), (b)l 语 言 的 任 一 操 作 符 的 操 作 数 可 能 是 L 的 任 一 表 达 SQL 是 用 这 种 方 式 表 示 关 系 完 备 的 语 言 的 一 个 例 子

Other Languages Transform-oriented languages are non-procedural languages that use relations to transform input data into required outputs (e.g. SQL). Graphical languages provide the user with a picture or illustration of the structure of the relation. The user fills in an example of what is wanted and the system returns the required data in that format (e.g QBE). Natural Language Interface (NChiql) 76

第 二 章 作 业 3/1 习 题 1,2,3 3/4 习 题 4,5,6,7 思 考 题 : 补 充 作 业 dbhw2 交 作 业 时 间 :3/11

Book Time 阅 读 : 流 失 的 岁 月, 李 新 作 者 敢 说 真 话 实 话, 文 中 揭 露 的 某 些 史 实 在 今 人 看 来 简 直 是 匪 夷 所 思