数据库复习(三)关系模型

本文详细介绍了关系模型的组成部分,包括关系模式、关系数据语言、域、笛卡儿积、元组、属性、主键、候选键和外键等基本概念。关系模型由关系模式、关系和关系数据库构成,强调数据完整性的重要性,如域完整性、实体完整性、参照完整性和用户定义完整性。ER图用于直观展示实体间的关系,可以转换为关系表。文章还举例说明了ER图到关系模式的转换过程,强调了数据库设计中的逻辑模型构建。
摘要由CSDN通过智能技术生成

目录

关系模型的组成部分

关系数据语言

域、笛卡儿积、关系、元组、属性

主键、候选键、外键

关系模式、关系、关系数据库

关系模式的完整性

ER图与关系表

Acknowledgments


关系模型的组成部分

关系的描述称为关系模式(relation schema),它可以形式化地表示为
R(U,D,DOM, F)

,其中R为关系名,U为组成该关系的属性名集合,D为U中属性所来自的域,DOM为属性向域的映像集合,F为属性间数据的依赖关系集合。如果不考虑数据之间的依赖(如*NF中的依赖),关系模式主要由属性名集合、属性所来自的域、属性向域的映像集合组成,即
R(U,D,DOM)

关系数据语言

关系数据语言包括 relational calculus and relational algebra, 以及混合产物sql等。

The relational calculus consists of two calculi, the tuple relational calculus and the domain relational calculus, that are part of the relational model for databases and provide a declarative way to specify database queries.

relational algebra is a theory that uses algebraic structures with a well-founded semantics for modeling data, and defining queries on it.

通过关系演算和关系代数,能够对关系进行操作。

域、笛卡儿积、关系、元组、属性

  • 域:a data domain is the collection of values that a data element may contain.域限定了属性的范围

  • 笛卡儿积:In mathematics, specifically set theory, the Cartesian product of two sets A and B, denoted A × B, is the set of all ordered pairs (a, b) where a is in A and b is in B. 集合的有序偶

  • 关系:A relation is a table structure definition (a set of column definitions) along with the data appearing in that structure. by the way, an n-ary relation being a subset of the Cartesian product of n domains. 可以认为是定义实际数据列的一组结构,也可以认为是笛卡儿积的子集

  • 元组:A tuple is basically the same thing as a row, except in an SQL DBMS, where the column values in a row are ordered

  • 属性:Attribute is the term used in the theory for what is commonly referred to as a column.An attribute *value* is the entry in a specific column and row, such as "John Doe" or "35".

主键、候选键、外键

  • 候选键:A candidate key, or simply a key, of a relational database is a minimal superkey.若关系中的某一属性组的值能唯一地标识一个元组,而其子集不能,则称该属性组为候选键(candidate key)。

  • 主键 :More formally, a primary key is a choice of candidate key (a minimal superkey);从候选键中选定一个 as primary key。

  • 外键:A foreign key is a set of attributes in a table that refers to the primary key of another table. 外键揭示了不同表之间的联系

关系模式、关系、关系数据库

The relational model (RM) for database management is an approach to managing data using a structure and language consistent with first-order predicate logic, first described in 1969 by English computer scientist Edgar F. Codd

Relationships are a logical connection between different tables, established on the basis of interaction among these tables.

A relational database is a digital database based on the relational model of data, as proposed by E. F. Codd in 1970. 应用关系模式的数据库就是关系数据库 —— E. F. Codd

关系模式的完整性

  • Data integrity is the maintenance of, and the assurance of, data accuracy and consistency over its entire life-cycle[1] and is a critical aspect to the design, implementation, and usage of any system that stores, processes, or retrieves data.

  • 域完整性:Domain integrity specifies that all columns in a relational database must be declared upon a defined domain.

  • 实体完整性:Entity integrity is concerned with ensuring that each row of a table has a unique and non-null primary key value;

  • 参照完整性:Referential integrity is a property of data stating that all its references are valid.

  • 用户定义的完整性:User-defined integrity refers to a set of rules specified by a user, which do not belong to the entity, domain and referential integrity categories.比如课程名字唯一等

ER图与关系表

ER图能够直观地表现实体集之间的联系

然而,ER图和关系表之间的转换不是完全一一对应的。例如,有一个ER图,包含3个实体集,2个联系,该ER图最多可以转换成5个关系表:关系表包含3个实体集 和两个 M:N联系;最少可以转换成1个关系表,该关系表包含3个实体集,均为 1:1联系。ER图和关系表之间的转换取决于关系的结构。

下面通过一个具体的例子来进一步理解ER图与关系模式的联系。

现有ER图如下:

将ER图转换为关系模式,可以得到实体与关系表,其中主键下划线表示,外键 使用md标出

实体集

商品(商品编号,名称、类别、单位、单价)

供应商(供应商编号,名称,账号,地址)

仓库(仓库编号,地址,负责人)

门店(门店编号,名称,地址)

采购员(采购员编号,姓名,业绩)

管理员(管理员编号,姓名,业绩,仓库编号

营业员(营业员编号,姓名,业绩,门店编号

关系

采购(采购单号,数量,日期,采购员编号供应商编号

进货(进货单号,数量,日期,商品编号供应商编号仓库编号

配送(配送单号,数量,日期,仓库编号商品编号门店编号

销售(销售单号,数量,日期,商品编号营业员编号门店编号

存储(库存量,日期,安全库存量,仓库编号商品编号

关系可视化(By Navicat & Mysql)

如下图所示,录入所有表,进行数据库设计,可以得到

如下图,继续编辑实体集与关系表,可以得到

 

为数据库中的表添加相应外键约束,并导出得到逻辑模型图(逻辑模型图包含在附件中)

Acknowledgments

数据库系统概论(第五版),王珊,萨师煊等

数据库系统概论学习指导与习题解答,王珊等

数据库系统概念(中文第六版)

Wikipedia & reddit/rSQL

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值