《数据库系统_relation(2)》由会员分享,可在线阅读,更多相关《数据库系统_relation(2)(77页珍藏版)》请在金锄头文库上搜索。
1、Chapter 2 The Relational Model of Data,第2章 关系数据模型,2 The Relational Model of Data,What is a data model? What is a relational data model? How to define a relation schema in SQL? Which operations can be in the relational model? What are the result of these operations? The operations in the relational m
2、odel can be expressed in either an algebra, called “relational algebra”. Relational algebra can express not only operations, but also constraints on relations.,关系数据模型,关系代数,约束,2.1 An Overview of Data Models,What is a data model? Mathematical representation of data. Examples: relational model = tables
3、; semistructured model = trees/graphs. Operations on data. Constraints.,2.1 An Overview of Data Models,Several data models The relational model, including object-relational extensions The semistructured-data model, including XML and related standards The hierarchical model 层次模型 The network model 网状模
4、型,2.2 Basics of the Relational Model,Relation 关系 A two-dimensional table called a relation.,2.2 Basics of the Relational Model,Why Relations? Very simple model. Often matches how we think about data. Abstract model that underlies SQL, the most important database language today.,2.2.1 Attributes,Attr
5、ibute 属性 Names for the columns of the relation, describe the meaning of entries in the column below. Such as length of Movies. An attribute have a name. Any two attributes of a relation cant have same name.,2.2.2 Schemas,Relation schema = relation name and attribute list. Optionally: types of attrib
6、utes.,Database = collection of relations. Database schema = set of all relation schemas in the database.,Example: Movies ( title, year, length, filmtype) or Movies ( title: string, year: int, length: int, filmtype: string),关系模式,2.2.3 Tuples,Tuples 元组 The rows of a relation, other than the header row
7、 containing the attributes, are called tuples. There may be no tuple in a relation. A tuple has one component for each attribute of the relation.,分量,2.2.3 Tuples,How to describe a tuple? Use commas to separate components, and use parentheses to surround the tuple. Example: (Star Wars, 1977, 124, col
8、or) We should always use the order in which the attributes were listed in the relation schema.,2.2.3 Tuples,The mapping of tuples and objects: A relation - a class A tuple - a object A component of a tuple - a property of a object,2.2.3 Tuples,The difference of tuples and objects: Objects have ident
9、ities, while tuples have not. A class could have two different objects with the same values in all attributes, but a tuple cant appear more than once in a relation.,2.2.4 Domains,Domains 域 A domain is an elementary type, such as integer, char(n), date, time. Each attribute of a relation is a domain,
10、 that is, a particular elementary type. Each component of any tuple must be atomic. Movies ( title: string, year: int, length: int, filmtype: string),2.2.5 Equivalent Representations of a Relation,We can reorder the attributes of a relation, without changing the relation. We can reorder the tuples o
11、f a relation, without changing the relation.,2.2.7 Keys of Relations,What is a key? A set of attributes forms a key for a relation if we do not allow two tuples in a relation instance to have the same values in all the attribute of the key. Movies ( title, year, length, genre ) employee-ID, Social-S
12、ecurity number, student-ID, drivers license numbers and automobile registration number,键,关键字,码,2.2.7 Keys of Relations,Key of the relation Movies ( title, year, length, genre, studioName, starName ): title, year ? title, year, starName ?,2.2.8 An Example Database Schema,Following is an example of da
13、tabase application:Well build a marketing database system for a sale company (supermarket). It will manage all the following information:1. Manage all departments information in the company (such as “Shanghai sales department“, “JiangSu sales department“). Also manage every salesman information in t
14、hose departments including exclusive employee number, ID card number, and some private information (such as name, gender, birthday and phone number). By the way one of salesmen will act as the department manager in his department.,2.2.8 An Example Database Schema,2. Manage a group of customers: name
15、, province, city, company name, phone number. 3.Manage all the merchandises information: manufacturers (e.g. Chunlan, Hailer ), types (e.g. motorcycle, air conditioner ), specifications (e.g. “MT125“, “RE1500“ ), prices, descriptions. 4. Manage sales order which record each deal has been done. Notes
16、: every sales order contains an unique order No. ,sign date, a corresponding customer, a salesman, and at least one kind of products. Each merchandise in the order should have its quantity and unit price which will be used to calculate the total prices.,2.2.8 An Example Database Schema,Customer (custid, name, prov, city, phone, company)Merchandise (merid, manufacturer, type, spec, price, desc)Salesman (empid, idno, name, gender, phone, deptid)Department (deptid, name, headerid)Salesorder (orderno, signdate, empid, custid)Salesitem (orderno, lineno, merid, unitprice, quantity),