数据库原理与安全笔记:关系数据模型
该部分为本科期间数据库原理与安全笔记备份。
关系数据模型三要素:Data structure、Operation on the data、Constraint on the data
Note: 关系的操作:两大类,查询和更新操作。
Structure of Relational Database
关系就是Table。

严格的定义表
用数学的形式说清楚多少列,行,列的值从哪里来。列取什么值不知道,但是列取哪些值是清楚的。列取值的集合就是域(Domain)
实体完整性:客户id能够唯一表示已各人,id不能为空。
参照完整性: 客户拥有account,客户id一定要按照coustomer里面有的id来填写这个表。一定要参照另一个表,来填写这个表。
用户定义完整性:balance必须是数字,
行:代表一个实体
列:代表这个实体的一个特征
从关系来说:关系的形。关系的框架,account_number ,branch_name…
关系的值:这样的一个表有形和值之分。
Basic Structure
Domain:legal type and range of values of an attribute 从数学集合论的角度,相同类型的值的集合。
cardinality of a table T: the number of tuples in T, denoted by |T|.
The arity, or degree, of a table : the number of columns or attributes.
关系模型中的表必须规范化。
null value
-
Not applicable (phone number for a client that has no phone)
-
Missing values (there is a phone number but we do not know it yet)
-
Not known (we do not know whether there is a phone number or not)
Relation
relation r is a subset of \(D_1×D_2×...×D_n\)
笛卡尔积可以表示为一个二维表,表中的每一行对应一个元组,每一列对应一个域
\(D_1×D_2×...×D_n={(d_1,d_2,⋯,d_n)|d_i∈D_i, i=1,2,⋯,n}\)
Relation Definition
属性名与域名的区别(属性所有值得集合的名字):列有可能来自同一个域,但是每个列有不同的含义,每个都需要有名字
Relation Schema
\(R=(A_1)(D_1)×(A_2)(D_2)×...×(A_n)(D_n)\)
关系模式要指出元组集合的结构。
关系模式: 对关系的描述,对关系结构的结构进行描述,通常用关系指代关系实例。
r(R) denotes a relation r on the relation schema R.
Relations are Unordered
Order of tuples and attributes are irrelevant(不相干) (tuples may be stored in an arbitrary order)
Properties of Tables
- A table is finite.
- Atomic data values: the data type of a column must be a single value (1NF)
- There are no duplicate rows in a table.
- The order of the rows in a table is not important.
- The order of the columns in a row is not important
关系与表的差别:表允许重复行,不满足原子性。
Relational Database
- consists of multiple relations
- Information about an enterprise is broken up into parts,with each relation storing one part of the information
- Storing all information as a single relation
- Normalization theory deals with how to design relational schemas
Keys
K is a set of attributes,\(K⊆R\)
Superkey、candidate key、Primary key
K is a superkey of R if values for K are sufficient to identify a unique tuple of each possible relation r(R).
Superkey K is a candidate key if K is minimal
Note: Minimal means no subset of it is superkey
Primary key: a candidate key chosen as the principal means of identifying tuples within a relation
Foreign key
A relation r1 may have an attribute that corresponds to the primary key or candidate key of another relation r2. The attribute is called a foreign key
外码可以为NULL。
联系也表示为关系。
与主码区别
-
外码一定是另外一个关系的主码。
-
外码是连接两个关系的一个纽带
-
外码和他参照的主码的域必须相同,但是名称不一定一样。但是如何是两个表的话,为了能够方便识别,通常来讲名字相同。也可以在同一关系中存在外码的情况。例如员工 id lid(领导的id)

Referencing Constraint
参照性约束。
Only values occurring in the primary key attribute of the referenced relation may occur in the foreign key attribute of the referencing relation
参照的属性:可以取两个值:Null或被参照属性的子集
foreign key取值
- a value of an existing primary key value of the corresponding primary key PK in the referenced relation R2
- a null (此时:the FK in R1 should not be a part of its own primary key)
Integrity constraint
完整性约束
- Entity Integrity:The values of primary key are not null
- Referential Integrity
- User-defined Integrity
空值说明存在某个不可标识的实体,即不可区分的实体。
用户定义完整性:由具体的应用而定。例如,成绩,大学成绩0-100,而高考成绩就是0-150,由系统支持。
DBMS自动完整性的支持

浙公网安备 33010602011771号