Download presentation
Presentation is loading. Please wait.
Published by钲玦 弘 Modified over 7 years ago
1
Part2 The Relational Database Chapter 02- The Relational Model
Database Systems Lu Wei College of Software and Microelectronics Northwestern Polytechnical University
2
Outline Data Models The Relational Model Relational Algebra
Exercises in Class Decomposing complex operations Relational Calculus Other languages QBE (Query By Example) Relational DBMS Lu Wei
3
Outline Data Models The Relational Model Relational Algebra
Exercises in Class Decomposing complex operations Relational Calculus Other languages QBE (Query By Example) Relational DBMS Lu Wei
4
Data Models Model Data model
The abstraction of the real world that simulate the real world. A map, the sand table of a building, … Data model The abstraction about data characteristics of the real world. An integrated collection of concepts for describing and manipulating data, relationships between data, and constraints on the data in an organization. Lu Wei
5
Data Models Requirements for abstracting data model
Represent the “real world” really Make the data understandable for variety of users Easy to implement on computer The three components of data model Data structure Data manipulation Constraints of data integrity Lu Wei
6
Data Models The Three-Level ANSI-SPARC Architecture
Figure 2.1 The ANSI-SPARC three-level architecture ANSI—American National Standards Institute SPARC—Standards Planning and Requirements Committee Lu Wei
7
Data Models The objective of the three-level architecture is to separate each user’s view of the database from the way the database is physically represented. The reasons for separation Each user should be able to access the same data in different view. Each user should be able to change the way he or she views the data, and this change should not affect other users. A user’s interaction with the database should be independent of storage considerations. Lu Wei
8
Data Models The DBA should be able to change the database storage structures without affecting the user’s views. The internal structure of the database should be unaffected by changes to the physical aspects of storage. The DBA should be able to change the conceptual structure of the database without affecting all users. Lu Wei
9
Data Models External Level Conceptual Level
The users’ view of the database. This level describes that part of the database that is relevant to each user. The external level consists of a number of different external views of the database. Conceptual Level The community view of the database. This level describes what data is stored in the database and relationships among data. Lu Wei
10
Data Models It is the middle level in the three-level architecture and is the most important level. This level contains the logical structure of the entire database as seen by the DBA. It supports each external view. It must not contain any storage-dependent details. Lu Wei
11
Data Models Internal Level
The physical representation of the database on the computer. This level describes how the data is stored in the database. This level covers the physical implementation of the database to achieve optimal runtime performance and storage space utilization. It covers the data structures and file organizations used to store data on storage devices. It interfaces with the operating system access methods to place the data on storage devices. Lu Wei
12
Data Models Schemas The overall description of the database is called the database schema. External schemas (subschemas) Correspond to different views of the data Conceptual schema Describes all the entities, attributes, and relationships together with integrity constraints. Internal schema Describes internal model. Contains the definitions of stored records, the method of representation, the data fields,... Lu Wei
13
Data Models Mapping The three types of schema of database must be consistent. The DBMS is responsible for mapping between these three types of schema. The conceptual schema is related to the internal schema through a conceptual/internal mapping. Each external schema is related to the conceptual schema by the external/conceptual mapping. Lu Wei
14
Data Models Figure 2.2 differences between the three levels Lu Wei
15
Data Models Data independence
A major objective for the three-level architecture is to provide data independence, which means that upper levels are unaffected by changes to lower levels. Logical data independence Refers to the immunity of the external schemas to changes in the conceptual schema. Physical data independence Refers to the immunity of the conceptual schema to changes in the internal schema Lu Wei
16
Data Models Figure 2.3 Data independence and the ANSI-SPARC three-level architecture Lu Wei
17
Data Models The three-level ANSI-SPARC architecture and two-stage mapping may be inefficient, but provides greater data independence. For more efficient mapping, the ANSI-SPARC model allows the direct mapping of external schema on to the internal schema, thus by_passing the conceptual schema. But … Lu Wei
18
Data Models Models suit for describing external and conceptual levels (These models are object-based) Entity-Relationship Semantic Functional Object-Oriented Refer to 数据库系统概论,萨师煊,第三版 page 18 for Entity-Relationship model Lu Wei
19
Data Models Models suit for describing logical data model (These models are record-based) Hierarchical data model Network data model Relational data model Object-Oriented model Models suit for describing internal level Unifying model Frame memory Refer to 数据库系统概论,萨师煊,第三版 page 21 and database systems, Thomas, third edition page 36 for Hierarchical data model, Network data model and Relational data model Lu Wei
20
Data Models The procedure of modeling
Real world—information world—computer world Conceptual schema is the ‘heart’ of the database. It supports all the external views and is supported by the internal schema. The conceptual schema should be a complete and accurate representation of the data requirements of the enterprise. Lu Wei
21
Outline Data Models The Relational Model Relational Algebra
Exercises in Class Decomposing complex operations Relational Calculus Other languages QBE (Query By Example) Relational DBMS Lu Wei
22
The Relational Model The Relational Database System (RDBMS) has become the dominant data-processing software in use today. The RDBMS is based on the relational model, so we discuss the relational model in this section. We will discuss the relational model through it’s three components as the same as data model. Lu Wei
23
The Relational Model Brief History of the Relational Model
E.F.Codd – ‘A relational model of data for large shared data banks’ (1970) RDBMS System R – IBM San Jose Research Laboratory in California INGERS (Interactive Graphics Retrieval System) – University of California at Berkeley Peterlee Relational Test Vehicle – IBM UK Scientific Centre (Todd,1976) Lu Wei
24
The Relational Model In the late 1970s and early 1980s, commercial system based on the relational model started to appear. Some extensions to the relational model Capture more closely the meaning of data (Codd, 1979) Support object-oriented concepts (Stonebraker and Rowe, 1986) Support deductive capabilities (Gardarin and Valduriez, 1989) Lu Wei
25
The Relational Model Relational Data Structure
The relational model is based on the mathematical concept of a relation, which is physically represented as a table. The relational data structure are principally based on set theory and predicate logic from mathematics. Lu Wei
26
The Relational Model Key concepts and terminologies Χ Relation
A relation is a table with columns and rows. Mathematical definition -- Any subset of n-tuples from the Cartesian product of n sets is a relation on the n sets. Cartesian product Let D1, D2, …, Dn be n sets. Their Cartesian product is defined as: Usually written as: D1× D2× …× Dn={(d1,d2,…,dn) | d1∈D1, d2∈D2, …, dn∈Dn} Χ i=1 n Di Lu Wei
27
The Relational Model Cardinality – The number of elements in a set. Let the cardinality of set Di be mi (i=1,2,…n). The cardinality of Cartesian product D1×D2×…×Dn is m1×m2×…×mn. Usually written as: Examples about relation and Cartesian product (write in black board) Π i=1 n mi Lu Wei
28
The Relational Model Attribute (field) Domain Tuple (record)
An attribute is a named column of a relation. Domain A domain is the set of allowable values for one or more attributes. Tuple (record) A tuple is a row of a relation. Degree or arity (维) The degree of a relation is the number of attributes it contains. Lu Wei
29
The Relational Model Cardinality (基数) Relation schema
The cardinality of a relation is the number of tuples it contains. Relation schema A named relation defined by a set of attribute and domain name pairs. (describe the type of a relation) Relation instance (value) Relational database A collection of normalized relations with distinct relation names. Relational database schema A set of relation schemas, each with a distinct names. Lu Wei
30
The Relational Model R e3 d3 c3 b3 a3 e2 d2 c2 b2 a2 e1 d1 c1 b1 a1 E
属性值 字段值 元组3 记录3 元组2 记录2 元组1 记录1 关系模式 记录类型 属性 字段、数据项 关系模型术语 一般术语 文件 关系 (或实例) 维数 基数 Figure 2.4 terminologies Lu Wei
31
The Relational Model Intension (内涵) Extension or state (外延)
The structure of a relation, together with a specification of the domains and any other restrictions on possible values. It is usually fixed unless the meaning of a relation is changed to include additional attributes. Extension or state (外延) The tuples are called extension (or state) of a relation. It changes over time. Lu Wei
32
The Relational Model Properties of Relations
A relation must be a finite set. Otherwise, it could not be stored in a computer The order of attributes has no significance The values of an attribute are homogeneous that is the values of an attribute are all from the same domain The order of tuples has no significance, theoretically. (In practice, the order may affect the efficiency of accessing tuples) Each cell of the relation contains exactly one atomic (single) values Each tuple is distinct; there are no duplicate tuples Lu Wei
33
The Relational Model Relational keys Superkey Candidate key
An attribute, or set of attributes, that uniquely identifies a tuple within a relation Candidate key A superkey such that no proper subset is a superkey within a relation Primary key The candidate key that is selected to identify tuples uniquely within a relation The selection of primary key – semantic, pragmatic Lu Wei
34
The Relational Model Alternate key (辅关键字) Foreign key
A candidate key that is not selected to be the primary key Foreign key An attribute, or set of attributes, within one relation that matches the candidate key of some (possibly the same) relation Examples about keys (write in black board) Lu Wei
35
The Relational Model Representing Relational Database Schemas
Give the name of the relation followed by the attribute names in parentheses. Underline the primary key Examples Student (sNo, sName, sSex, sAge, sDept) Course (cNo, cName, cPNo, cCredit) SC (sNo, cNo, score) Lu Wei
36
The Relational Model Branch (branchNo, street, city, postcode)
Staff (staffNo, fName, lName, position, sex, DOB, salary, branchNo) PropertyForRent (propertyNo, street, city, postcode, type, rooms, rent, ownerNo, staffNo, branchNo) Client (clientNo, fName, lName, telNo, prefType, maxRent) PrivateOwner (ownerNo, fName, lName, address, telNo) Viewing (clientNo, propertyNo, viewDate, comment) Registration (clientNo, branchNo, staffNo, dateJoined) Lu Wei
37
The Relational Model Relational Integrity Null
Represents a value for an attribute that is currently unknown or is not applicable for this tuple. Null can cause implementation problems because the relational model is based on predicate calculus, which is a two-valued or Boolean logic. The incorporation of nulls in the relational model is a contentious issue. Lu Wei
38
The Relational Model Entity integrity Referential integrity
In a base relation, no attribute of a primary key can be null. Referential integrity If a foreign key exists in a relation, either the foreign value must match a candidate key value of some tuple in its home relation or the foreign key value must be wholly null. Enterprise constraints Additional rules specified by the users or database administrators of a database Examples about integrity Lu Wei
39
The Relational Model Relational manipulation
The data manipulation on relational model is in fact the manipulation on relation or set. The relational algebra and relational calculus are two formal, non-user-friendly languages but they have been used as the basis for other higher-level Data Manipulation Languages (DMLs) for relational database. The relational algebra and relational calculus are equivalent to one another. Lu Wei
40
Outline Data Models The Relational Model Relational Algebra
Exercises in Class Decomposing complex operations Relational Calculus Other languages QBE (Query By Example) Relational DBMS Lu Wei
41
Relational Algebra The relational algebra is a theoretical language with operations that work on one or more relations to define another relation without changing the original relation(s). Relations are closed under the algebra. Both the operands and the results are relations Informally, we may describe the relational algebra as a (high-level) procedural language. It can be used to tell the DBMS how to build a new relation from one or more relations in the database. Lu Wei
42
Relational Algebra RS Set Operations Union (并) R∪S={t|t∈R∨t∈S}
The union to two relations R and S defines a relation that contains all the tuples of R, or S, or both R and S, duplicate tuples being eliminated. R and S must be union-compatible. – the arities (degree) of the two relations must be equivalent. R∪S={t|t∈R∨t∈S} RS Lu Wei
43
Relational Algebra S R R∪S A B C 3 6 7 2 5 4 A B C 3 4 5 7 2 A B C 3 6
Lu Wei
44
Relational Algebra RS Set difference (差) R-S={t|t∈R∧tS}
The set difference operation defines a relation consisting of the tuples that are in relation R, but not in S. R and S must be union-compatible. R-S={t|t∈R∧tS} RS Lu Wei
45
Relational Algebra R S R-S S-R A B C 3 6 7 2 5 4 A B C 3 4 5 7 2 A B C
Lu Wei
46
Relational Algebra RS Intersection (交) R∩S={t|t∈R∧t∈S}
The intersection operation defines a relation consisting of the set of all tuples that are in both R and S. R and S must be union-compatible. R∩S={t|t∈R∧t∈S} We can express the intersection operation in terms of the set difference operation: R∩S=R-(R-S) RS Lu Wei
47
Relational Algebra S R R∩S A B C 3 6 7 2 5 4 A B C 3 4 5 7 2 A B C 7 2
Lu Wei
48
Relational Algebra Cartesian product RS = {t|t=﹤tr,ts﹥∧trRtsS}
The Cartesian product operation defines a relation that is the concatenation of every tuple of relation R with every tuple of relation S. Let the arities of R and S be m and n. Let cardinalities of R and S be k1 and k2 The arity (degree) of the new relation will be m+n The cardinality of the new relation will be k1×k2 RS = {t|t=﹤tr,ts﹥∧trRtsS} Lu Wei
49
Relational Algebra A B 1 2 C D 10 19 20 E a b R x S C D 10
Lu Wei
50
Relational Algebra Unary Operations Selection (Restriction) 选择
The selection operation works on a single relation R and defines a relation that contains only those tuples of R that satisfy the specified condition (predicate). σF(R) = {t|t∈R ∧ F(t)=‘true’} The selection operate the relation from the view of rows. Lu Wei
51
Relational Algebra R A<5(R) A<5 C=7(R) A B C 3 6 7 2 5 4 A B
Lu Wei
52
Relational Algebra Projection (投影) ΠA(R) = { t[A] | t∈R }
The projection operation works on a single relation R and defines a relation that contains a vertical subset of R, extracting the values of specified attributes and eliminating duplicates. ΠA(R) = { t[A] | t∈R } The projection operate the relation from the view of columns. Lu Wei
53
Relational Algebra B , C(R) R c b f e d a C B A B C b c e f Lu Wei
54
Relational Algebra Join Operations Theta join (θ-join)
The theta join operation defines a relation that contains tuples satisfying the predicate F from the Cartesian product of R and S. The predicate F is of the form R.aiθS.bi where θ may be one of the comparison operators (<, ≤, >, ≥, =,≠) We can rewrite the Theta join in terms of basic selection and Cartesian product operation R S={trts|trR∧tsS∧tr[A]θts[B]} AθB R S = r[A]S[B]( R×S) A B Lu Wei
55
Relational Algebra R S R S B < D A B C D E 1 2 3 6 4 5 9 8 7 6 5 4
Lu Wei
56
Relational Algebra Equijoin (相等连接) R S={trts|trR∧tsS∧tr[A]=ts[B]}
When θ is ‘=’ in theta join A particular type of Theta join R S={trts|trR∧tsS∧tr[A]=ts[B]} A=B Lu Wei
57
Relational Algebra Natural join R S={trts|trR∧tsS∧tr[X]=ts[X]}
The natural join is an equijoin of two relations R and S over all common attributes x. One occurrence of each common attribute is eliminated from the result. A particular type of equijoin When there are no common attributes in R and S, the equijoin is equal to Cartesian product. R S={trts|trR∧tsS∧tr[X]=ts[X]} R S = R×S Lu Wei
58
Relational Algebra S R R S 9 8 7 6 5 4 3 2 1 C B A C D 3 1 6 2 A B C D
Lu Wei
59
Relational Algebra B 1 3 2 D a b E S R S A B 1 2 4 C
Lu Wei
60
Relational Algebra A question in natural join T C 500 李三 P04 600 700
800 salary 孙立 P03 钱广 P02 赵明 P01 tName tNo cNo cName tNo C01 DS P01 C02 DB P02 C03 OS P04 How to produce a relation consisting of all teachers’ information with the courses they teach including the teachers who have no course. Lu Wei
61
Relational Algebra T C 500 李三 P04 600 700 800 salary 孙立 P03 钱广 P02 赵明
tName tNo cNo cName tNo C01 DS P01 C02 DB P02 C03 OS P03 tNo tName salary cNo cName P01 赵明 800 C01 DS P02 钱广 700 C02 DB P03 孙立 600 C03 OS Lu Wei
62
Relational Algebra Outer join
Often in joining two relations, a tuple in one relation does not have a matching tuple in the other relation. We may want a tuple from one of the relation to appear in the result even when there is no matching value in the other relation. The (left) Outer join is a join in which tuples from R that do not have matching values in the common attributes of S are also included in the result relation. Missing values in the second relation are set to null. Left outer join, right outer join, full outer join R S, R S, R S Lu Wei
63
Relational Algebra T C 500 李三 P04 600 700 800 salary 孙立 P03 钱广 P02 赵明
tName tNo cNo cName tNo C01 DS P01 C02 DB P02 C03 OS P03 tNo tName salary cNo cName P01 赵明 800 C01 DS P02 钱广 700 C02 DB P03 孙立 600 C03 OS P04 李三 500 null Lu Wei
64
Relational Algebra Semijoin F F
The semijoin operation defines a relation that contains the tuples of R that participate in the join of R with S. = A( ) A is the set of all attributes for R R S R S F F Lu Wei
65
Relational Algebra An example for semijoin B 1 3 2 D a b E S
4 C D a b R A B C D 1 2 a b Lu Wei
66
Relational Algebra X Z Division Operation Zx x=张军 Images set (象集)
Relation R(X,Z), X and Z are sets of attributes of R. For each t[X] = x, we can define the images set of x Zx in R : Zx={t[Z]|t∈R,t[X]=x} 它表示R中属性组X上值为x的诸元组在Z上分量的集合。 X Z Zx x=张军 张军同学所选修的全部课程 姓名 课程 张军 物理 王红 数学 课程 数学 物理 Lu Wei
67
Relational Algebra Division R÷S={tr[X]|trR∧ΠY(S)Yx}
给定关系R(X,Y)和S(Y,Z),其中X, Y, Z为属性组。 R中的Y与S中的Y可以有不同的属性名,但必须出自相同的域集。R与S的除运算得到一个新的关系P(X),P是R中满足下列条件的元组在X属性列上的投影:元组在X上分量值x的象集Yx包含S在Y上投影的集合。记作: We can rewrite the division expression using difference and Cartesian operation as follow: R÷S={tr[X]|trR∧ΠY(S)Yx} RS=X(R)X(X(R)Y(S)R) Lu Wei
68
Relational Algebra R S A B C D a b c d e f C D c d e f R÷S=? Lu Wei
69
Relational Algebra CD(a,b) = {(c,d),(e,f),(d,e)} CD(b,c) = {(e,f)}
CD(e,d) = {(c,d),(e,f)} {(c,d),(e,f)} CD(a,b) {(c,d),(e,f)} CD(e,d) => R÷S = {(a,b),(e,d)} Lu Wei
70
Relational Algebra R S AB (R) AB (R) CD (S) A B C D a b c d e f A
AB (R) CD (S)-R A B a b c e d A B a b e d A B C D b c d A B b c R S= - = Lu Wei
71
Relational Algebra Try : Given relations R and S as following, compute the result of R÷S R S A B C a1 b1 c2 b2 c3 c1 a2 b3 c7 a3 b4 c6 a4 b6 B C D b1 c2 d1 b2 c3 d2 c1 d3 d4 Lu Wei
72
Relational Algebra 在关系R中,A可以取四个值{a1, a2, a3, a4}。其中: a1的象集为{(b1,c2), (b2,c3), (b2,c1)} a2的象集为{(b3,c7), (b2,c3)} a3的象集为{(b4,c6)} a4的象集为{(b6,c6)} S在(B,C)上的投影为{(b1,c2),(b2,c3),(b2,c1)} 显然只有a1的象集(B,C)a1包含S在(B,C)属性组上的投影,所以R÷S={a1} Lu Wei
73
= = Relational Algebra The indication of division 选修了全部 课程的学生
姓名 课程 张军 物理 王红 数学 课程 数学 物理 姓名 张军 王红 = 选修了全部课 程并且成绩都 相同的学生 姓名 课程 成绩 张军 物理 93 王红 数学 86 92 课程 数学 物理 = 姓名 成绩 张军 93 Lu Wei
74
Outline Data Models The Relational Model Relational Algebra
Exercises in Class Decomposing complex operations Relational Calculus Other languages QBE (Query By Example) Relational DBMS Lu Wei
75
Exercises in Class Note: The following exercises are all based on two relation schemas Student (sNo, sName, sSex, sAge, sDept) Course (cNo, cName, cPNo, cCredit) SC (sNo, cNo, score) Lu Wei
76
Exercises in Class Branch (branchNo, street, city, postcode)
Staff (staffNo, fName, lName, position, sex, DOB, salary, branchNo) PropertyForRent (propertyNo, street, city, postcode, type, rooms, rent, ownerNo, staffNo, branchNo) Client (clientNo, fName, lName, telNo, prefType, maxRent) PrivateOwner (ownerNo, fName, lName, address, telNo) Viewing (clientNo, propertyNo, viewDate, comment) Registration (clientNo, branchNo, staffNo, dateJoined) Lu Wei
77
Exercises in Class 1. List all employees whose salary more than $10000
List all students in information system department List all male students whose age no less than 20 σsalary>10000(Staff) σsDept=‘IS’(Student) σsAge≥20 ∧ sSex=‘M’(Student) Lu Wei
78
Exercises in Class 2. List all students with name and department attributes 3. List the course numbers which elected by student whose number is ‘070001’ ΠsName,sDept(Student) cNo( sNo=‘070001’ (SC)) Course Lu Wei
79
Exercises in Class 4. List the students who have elected course ‘001’ or ‘002’ ΠsNo(cNo=‘001’ ∨ cNo=‘002’(SC)) Student (ΠsNo(cNo=‘001’(SC) )∪ ΠsNo(cNo=‘002’(SC))) Student Lu Wei
80
× × √ √ Exercises in Class
5. List the students who have elected course ‘001’ and ‘002’ × ΠsNo(cNo=‘001’ cNo=‘002’(SC)) Student × (ΠsNo(SC÷ cNo=‘001’ ∨ cNo=‘002’(C))) Student √ (ΠsNo,cNo(SC)÷ cNo=‘001’ ∨ cNo=‘002’(C)) Student √ (ΠsNo( cNo=‘001’ (SC)) ∩ΠsNo( cNo=‘002’ (SC))) Student Lu Wei
81
Exercises in Class 6. List the students who have elected course ‘001’ but have not elected course ‘002’ × (ΠsNo(cNo=‘001’(SC) - cNo=‘002’(SC))) Student √ (ΠsNo(cNo=‘001’(SC)) - ΠsNo(cNo=‘002’(SC))) Student Lu Wei
82
Exercises in Class 7. List the students who have not elected course ‘001’ √ (ΠsNo(Student) - ΠsNo(cNo=‘001’(SC))) Student × ΠsNo(cNo≠‘001’(SC)) Student sNo sName sAge s1 … s2 s3 sNo cNo score s1 001 90 s2 002 95 96 Lu Wei
83
Exercises in Class 8. List the student who have only elected course ‘001’ (ΠsNo(SC) - ΠsNo(cNo≠‘001’(SC))) Student || (ΠsNo( cNo=‘001’( SC)) - ΠsNo(SC - cNo=‘001’(SC))) Student Lu Wei
84
Exercises in Class sNo cNo score s03 002 88 s01 003 92 sNo s01 s02 sNo
Lu Wei
85
Exercises in Class 9. Produce a status report on property viewings.
Note: In this case, we want to produce a relation consisting of the properties that have been viewed with comments and those that have not been viewed. ΠpropertyNo,street,city(PropertyForRent) Viewing Lu Wei
86
Exercises in Class 10. Identify all clients who have viewed all properties with three rooms. (ΠclientNo,propertyNo(Viewing)÷ΠpropertyNo(rooms=3(PropertyForRent))) Client Lu Wei
87
Outline Data Models The Relational Model Relational Algebra
Exercises in Class Decomposing complex operations Relational Calculus Other languages QBE (Query By Example) Relational DBMS Lu Wei
88
Decomposing complex operations
The relational algebra operations can be of arbitrary complexity. We can decompose such operations into a series of smaller relational algebra operations. The operations we often use to decompose complex algebra expression: assignment operation () and rename operation (ρ). Lu Wei
89
Decomposing complex operations
Assignment operation () Used to give a name to the results of intermediate expressions RS = X(R) X(X(R) Y(S) R) We can use assignment operation to rewrite it as follow: temp1 X(R) temp2 X(temp1 Y(S) R) result temp1 temp2 Lu Wei
90
Decomposing complex operations
Rename operation (ρ) Used to give a name to the result of a relational algebra operation and allows an optional name for each of the attributes of the new relation to be specified. ρS(E) or ρS(a1,a2,...,an)(E) The rename operation provides a new name S for the expression E, and optionally names the attributes as a1,a2,…,an. Lu Wei
91
Outline Data Models The Relational Model Relational Algebra
Exercises in Class Decomposing complex operations Relational Calculus Other languages QBE (Query By Example) Relational DBMS Lu Wei
92
Relational Calculus A certain order is always explicitly specified in a relational algebra expression and a strategy for evaluating the query is implied. A relational calculus query specifies what is to be retrieved rather than how to retrieve it. Informally, we may describe the relational calculus as a (high-level) non-procedural language. It can be used to formulate the definition of a relation in terms of one or more database relations. Lu Wei
93
Relational Calculus Relational calculus is based on predicate calculus in symbolic logic. Two forms of relational calculus: Tuple relational calculus (Codd, 1972) Domain relational calculus (Lacroix and Pirotte, 1977) Lu Wei
94
Relational Calculus Tuple relational calculus
In the tuple relational calculus we are interested in finding tuples for which a predicate is true. It is based on the use of tuple variables. A tuple variable is a variable that ‘range over’ a named relation. Stuff(S) – specify the range of a tuple variable S as the Stuff relation. General form of the domain relational calculus: {S | F(S)} Lu Wei
95
Relational Calculus We can use the existential and universal quantifiers (存在量词和全称量词)in formulate of relational calculus. (┐) {S.fName, S.lName | Staff(S) S.position=‘Manager’ S.salary>25000} List the staff who manage properties for rent in Glasgow {S | Staff(S) (P)(PropertyForRent(P) (P.staffNo=S.staffNo) P.city=‘Glasgow’)} Lu Wei
96
Relational Calculus Domain Relational Calculus
In the tuple relational calculus, we use variables that range over tuples in a relation. In the domain relational calculus, we also use variables but in this case the variables take their values from domains of attributes rather than tuples of relations. General form of the domain relational calculus: {d1,d2,…,dn | F(d1,d2,…,dm )} m≥n Lu Wei
97
Relational Calculus {fN,lN | (sN,posn,sex,DOB,sal,bN)
(Staff(sN,fN,lN,posn,sex,DOB,sal,bN) posn=‘Manager’sal>25000)} Lu Wei
98
Outline Data Models The Relational Model Relational Algebra
Exercises in Class Decomposing complex operations Relational Calculus Other languages QBE (Query By Example) Relational DBMS Lu Wei
99
Other languages Although the relational calculus is hard to understand and use, it was recognized that its non-procedural property is exceedingly desirable. This resulted in a search for easy-to-use non-procedural techniques and led to easy-to-use non-procedural relational languages: Lu Wei
100
Other languages Transform-oriented languages Graphical languages
Use relations to transform input data into required output Graphical languages Provide the user with a picture or illustration of the structure of the relation Fourth-generation languages (4GLs) Fifth-generation languages (5GLs) Lu Wei
101
Outline Data Models The Relational Model Relational Algebra
Exercises in Class Decomposing complex operations Relational Calculus Other languages QBE (Query By Example) Relational DBMS Lu Wei
102
QBE (Query By Example) Readings:
iCarnegie contents online, section 1.2.3 Connolly and Begg, sections 7.1 and 7.2 Lu Wei
103
Outline Data Models The Relational Model Relational Algebra
Exercises in Class Decomposing complex operations Relational Calculus Other languages QBE (Query By Example) Relational DBMS Lu Wei
104
Relational DBMS 关系数据库管理系统简称为关系系统,是指支持关系模型的系统。由于关系模型中并非每一部分都是同等重要的,所以我们并不苛求一个实际的关系系统必须完全支持关系模型。 Lu Wei
105
Relational DBMS 一个数据库管理系统可定义为关系系统,当且仅当它至少支持:
关系数据库(即关系数据结构)。也就是说,从用户观点看,数据库是由表构成的,并且系统中只有表这种结构。 支持选择、投影和(自然)连接运算。对这些运算不要求用户定义任何物理存取路径。 Lu Wei
106
上面关于系统的定义实际上是对关系系统的最低要求,许多实际系统都不同程度地超过了这些要求。按照E. F
上面关于系统的定义实际上是对关系系统的最低要求,许多实际系统都不同程度地超过了这些要求。按照E.F.Codd的思想,依据关系系统支持关系模型的程度不同,可以把关系系统分为四类: 表式系统。 这类系统仅支持关系数据结构(即表),不支持集合级的操作。表式系统实际上不能算关系系统。倒排表列(Inverted list)系统就属于这一类。 Lu Wei
107
即上面定义的关系系统,它支持关系数据结构和选择、投影、连接三种关系操作。许多微机关系系统如FoxBASE、FoxPro等就属于这一类。
(最小)关系系统。 即上面定义的关系系统,它支持关系数据结构和选择、投影、连接三种关系操作。许多微机关系系统如FoxBASE、FoxPro等就属于这一类。 关系上完备的系统。 这类系统支持关系数据结构和所有的关系代数操作(功能上与关系代数等价)。目前许多中大型关系系统如DB2、 ORACLE等就属于这一类。 Lu Wei
108
全关系系统。 这类系统支持关系模型的所有特征,特别是数据结构中域的概念,实体完整性和参照完整性。虽然DB2、 ORACLE等系统已经接近这个目标,但到目前为止尚没有一个系统是全关系系统。 尽管不同的关系系统对关系模型的支持程度不同,但它们的体系结构都符合三级模式结构,提供了模式、外模式、内模式以及模式与外模式之间的映象、模式与内模式之间的映象。 Lu Wei
109
Summary In this chapter you should have learned:
The concept of data model and it’s three components The Three-Stage architecture and Two-Level mapping of database Concepts in relational model The three components and their complementation of relational model The five fundamental operations in relational algebra, Selection, Projection, Cartesian product, Union, and set difference. In addition, Join, Intersection, and Division operations which can be expressed in terms of the five basic operations. Lu Wei
110
Summary Relation calculus is a formal non-procedural language that uses predicates. Two forms of the relational calculus : tuple relational calculus and domain relational calculus. Lu Wei
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.