跳至正文

【笔记】数据库基础入门Fundamentals of Database Systems(1)

  • Dec 17, 2023 阅读、整理
  • Dec 18, 2023 补充view部分及其诠释

前言:近期开始重新学习数据库内容,其一是C.J.Date的数据库英文图书“An Introduction to Database System”(4th Edition,1986),其二是Elmasri和Navathe的“Fundamentals of Database Systems”(3rd Edition,2000)。虽然前者作者名声更大,但由于后者内容更新,因此二者参考学习。再次被知识的深邃所吸引,更感慨于前辈科学家的严谨与细致。

本文一方面作为读书摘要,另一方面备注自己的学习体会。

XILEJUN

下文中,C.J.Date的图书简称C.D.J(正如他自己所简称),默认的章节则指Elmasri和Navathe的“Fundamentals of Database Systems”(或者简称Fundamentals)。

一、认识数据库及其功能

虽然号称“数据库基础”,但两本书的难度可是大学本科甚至研究生级别的,所以对我们像我一样的“业务爱好者”,只需要关注通识性的部分——那些对于大部分数据库而言普适性的内容,比如数据库的分类、架构设计、数据要素、概念模型、表等等,并有意识地跳过物理层的构建部分(internal level)(这些是专业数据库工程师的内容)。

Chapter 1 database and database users

1.1 Introduction 何为数据库?

相比Fundamentals的定义(P4 A database is a collection of related data.),我觉得C.J.D的描述更有趣(P3,A Database system is essentially nothing more than a computed record-keeping system),至少对我们分析师而言更能理解上的价值。不过,C.J.D把数据库称之为 a collection of computerized data files明显是因为他更“老”,数据库确实是为了更好地存储文件(file),甚至可以说替代我们纸质的数据本、记账本。不过,随着关系型数据库逐渐成为主流,我们一般不再称数据是file,而是table。表格table毫无疑问是数据库的核心——既是存储上的分类单位,也是数据查询的基本对象。

怎样更深刻的理解数据库是什么?这就深入到它从何而为、为何而去。

在Fundamentals一书中,作者用了几个关键词:represent、real world、logical。数据库的内容首先来源于现实(the real world),是对现实的反映和记录,它内部的构成是自洽、有关联的,它服务于特定的目的。

P4 A database is a collection of related data. …… A database has the following implicit properties:

  • A database represents some aspect of the real world, sometimes called the miniworld or the Universal of Discourse(UoD). Changes to the miniworld are reflected in the database.
  • A database if a logically coherent collection of data with some inherent meaning.
  • A database is designed , built, and populated with data for a specific purpose.

In other words, a database has some source from which data is derived, some degree of interaction with events in the real world, and an audience that is actively interested in the contents of the database.

Fundamentals of Database Systems

不得不说,这样的诠释略显生涩,虽然represent这个词至关重要,我们后续会反复的提及它对应的中文形态;不过,要从业务的角度理解数据库,我们需要理解所谓的“real world”的业务意义,represent是何种过程。

在这一方面,我还是更偏向于C.J.D的诠释。他简化了数据库的目的,同时强调了对于决策的价值。

前面说,数据库不过、仅仅是“计算机记录系统”(computerized record-keeping system),换句话说,”数据库的全部目的都是保存信息并确保其按需可用”(P5,that is, a system whose overall purpose is to maintenance information and to make that information available in demand).这里的信息可以是对于个人或机构而言任何有用的数据(of significance to the individual or organization),而目的则是服务决策过程(server anything ……to assist in the process of making decision).

在接下来,C.J.D更是新开一节(1.3 Operational Data),详细说明了数据库的集合是什么的集合,是业务运营数据的集合,而不是输入数据、数据数据、临时数据等的集合。

当然,Operation data更早来自于Engles,他把数据库的数据称之为“Operation data”,以此区别于输入数据(input data)输出数据(output data),以及其他类型的数据。而C.J.Date对此做了修改,于是将数据库定义为:“保存企事业单位中各种应用系统所用的运营数据的集合”。

A database is a collection of stored operational data used by the applications of some particular enterprise.

P9, An Introduction to Database System

这里的重点是operational data、enterprise,先说enterprise,书中指任何合理而独立的商业、科学、技术等组织(any reasonable self-contained commercial, scientific, technical ,or other organizations),基本可以对应我们常说的“企事业单位”;在此基础上,Operational data就容易理解了,一切企事业单位的运营过程,都会源源不断的产生数据(P9,Any enterprise must necessarily maintain a lot of data about its operation),从企业到政府,从医院到军队,莫不如此。这里的Operation概括一切抽象的运营行为,管理中有运营管理(operations management),数据中有“运营数据”(operational data)。

之所以如此强调operation的翻译,是因为这个简单的管理名词,在中文的技术领域存在严重的翻译错误,很多外文图书,甚至大学计算机系的教科书中,都把operational data称之为“操作性数据”,而把后面将提及的“operational database”称之为“操作性数据库”,简直就是天大的笑话。足以深刻地说明,我们的计算机学界距离一线的业务是多么遥远,计算机的象牙塔是多么的“高不可攀”。

数据库的管理:数据库管理系统DBMS

正如C.J.Date所言,“数据库仅仅是数据记录的集合”,它更像是静态“保管箱”,你如不动,它自不动。而诸如数据的创建、更新、修改等诸多工作,则是数据库管理系统(Database Management System,DBMS)的基本职责——虽然很多人常常把二者宽泛地地称之为“数据库系统”——一个包含数据库的管理系统。

A database management system(DBMS) is a collection of programs that enables users to create and maintain a databases. The DBMS is hence a general-purpose software system that facilitates the process of defining, constructing, and manipulating databases for various applications.

P5 Fundamentals of Database Systems

理解DBMS的重点是program(程序),这也塑造了不同的数据库工具之间显而易见的差别,即便同为关系型数据库的Mysql、PostgreSQL和SQL Server。DBMS可以定义define、构建construct,和操纵manipulate数据库,它们分别处理不同的内容:

  • Define定义:定义数据类型data type、关系relationship和约束constraints
  • Construct:数据存储的过程process of storing the data itself
  • Manipulate:包含数据的查询和更新,以及生产报表( retrieve specific data, updating the database to reflect changes in the miniworld , and generating reports from the data.)

对于分析而言,最重要的部分当然是了解如何查询数据库中的已有数据,极少涉及到数据库定义、更新等内容,常见的Select查询,可以视为上述manipulation操作的一小部分。

1.3 数据库的特性 characteristics of the database approach

这一部分是Fundamentals第一章的重点,也让我对数据库的理解更上一层楼,特别是1.3.2部分的Data Abstraction。如下的四个特征,是DBMS区别于传统的文件处理系统的关键(P11, The preceding characteristics are most important in distinguishing a DBMS from traditional file-processing software.)

1.3.1 自定义特性 self-describing nature of a database system

作者说,数据库系统不仅仅包含数据库本身,而且包含一个完全自定义或“自描述”的数据库架构及其约束(a complete definition or description of database structure and constrains),这些定义保存在系统的目录(catalog)之中,我们把目录中的信息称之为“元数据”(meta-data)。

A fundamental characteristic of the database approach is that the database system contains not only the database itself, but also a complete definition or description of database structure and constraints. This definition is stored in the system catalog…… The information stored in the catalog is called meta-data, and it describe the structure of the primary database.(figure 1.1)

P8 “Fundamentals of Database Systems”(3rd Edition,2000)

这里的重点是definition of database,catalog ,meta-data以及primary database。 当我们在自己的电脑上安装一个数据库时,我们既可以做自定义(self-defined),也可以保持默认配置从而获得“初始化数据库”(primary database);不管是自定义的还是默认的配置,都是“关于数据库架构及其参数的数据”,称之为Meta-data(元数据),这里的meta是“元”“初始”之一,如同道家所说“道生一,一生二,二生三,三生万物”中的“道”,英文把“道”音译为“Tao”,如果是意译可以考虑“meta”一词。

在Tableau Server安装过程中,系统也会自动安装一个内置的Postgresql数据库,其中默认的架构称之为workbooks 用以保存系统运行的关键数据,比如用户、内容、权限等等,相当于 tableau server应用平台的“元数据”(This database provides persistent storage for Tableau Server and is primarily intended to support that application.)更多了解参考:Workgroups Database

初看这里,还是蛮绕的。接下来一段作者解释说,一个数据库管理系统的软件包(DBMS software package)不是专用于某个应用的,而应该是适用于任何一种数据库应用的(The DBMS software must be equally well with any number of database applications),比如既能适用于学校,又能适用于银行或者企业。数据库管理平台会把特定的数据库架构信息,保存在自身的元数据之中。

也就是说,数据库平台的“自定义”特征,确保了它的初始设置,同时确保支持不同企事业单位的差异化需求。

1.3.2 数据抽象Insulation between programs and data, and data abstraction

关键内容)这一段是第一章的重中之重,它界定的“数据抽象”不仅适用于数据库,而且适用于数据库基础上的数据查询、数据处理、数据展现的过程。借助这里的分析框架,我们可以把数据库的“三层楼”框架再细细分解为诸如“三层楼12套30房间”这样的更细致的视角。

program-data independence

作为对比,作者提及了传统的文件处理程序,由于其架构是在程序中预先定义好的,没有实现定义与程序的分离,因此就只能适用于特定的数据库。(P9 Whereas file-processing software can access only specific database, DBMS software can access diverse databases by extracting the database definitions from the catalog and then using these definitions.)

这种数据构架和程序一体化的设计,缺乏灵活性,想要修改数据库架构,就意味着重写正常程序本身。相反,DBMS的“数据定义”(即元数据meta-data)和程序是独立的,作者把这个属性称之为“program-data independence”(程序-数据独立性)。

P9 The structure of data files is stored in the DBMS catalog separately from the access programs. We call this property program-data independence.

为了方便理解,作者举例说:假设学校的数据库中记录了学生的信息,包含姓名、编号、班级,如果我们想添加一个新属性“出生日期”,我们不需要修改整个程序,而仅需在数据表中增加新的一列,这会反映在数据库的catalog之中。下次访问,数据表就增加了新需求。

我们可以借助于书中图1-1的插图辅助说明程序与数据的独立性——或者说程序与meta-data的独立性。

P10 program-operation independence

在介绍了“程序与数据独立性”之后,作者更进一步地说,在面向对象和对象关系型数据库中(标准SQL中没有),用户可以在‘数据库定义’中自定义‘函数’,程序的用户可以引用函数,而如需关心内部的实现过程,这就是Program-operation independence(程序与“操作”独立性)。作者在文中使用了一些专业性词汇,比如operation可以称之为function(我们常说的函数),这些就深入到了近乎编程的领域。

In objected-oriented and object-relational databases, users can define operations on data as part of the database definitions. An operation (also called a function) is specified in two parts. The interface (or signature) of an operation includes the operation name and the data type of its arguments (or parameters). The implementation (or method) of the operation is specified separately and can be changed without affecting the interface. User application programs can operate on the data by invoking these operations through their names and arguments, regardless of how the operations are implemented. This may be termed program-operation independence.

PP9~10 “Fundamentals of Database Systems”(3rd Edition,2000)

我们知道Java中有自定义函数,在Python也很普遍,我们可以用类似的方式做个对照。如下图所示:为了简化类似的数据处理过程,实现定义一个函数(def就是definition的简称),函数包含interface接口(或者叫signature)和method方法(或者叫implementation)两部分,接口又包含函数名称和变量arguments(或叫parameter)。定义好的函数可以被反复引用(invoke)。对于引用这个函数的人而言,他可以忽略函数的内部执行过程,只需要关注它能实现什么。

这个功能在标准SQL中自然是没有的,但却是编程语言中的基本功能。为什么不能给SQL加上这个功能呢?这个可能与SQL的自身定位有关。SQL是面向集合的、声明式程序语言,而非命令式语言;为了实现类似的功能,标准SQL可以使用扩展实现类似的功能。一些数据库管理系统也自行扩展了类似功能,比较典型的是SQL Server对用户自定义函数(UDFs,user-defined functions)的支持。

SQL is a set-based, declarative programming language, not an imperative programming language like C or BASIC. However, extensions to Standard SQL add procedural programming language functionality, such as control-of-flow constructs.

en.wikipedia.org/wiki/SQL

这里有一个重点是Operation的翻译问题,前面我们把“operational data”翻译为“运营数据”,这里operation代表企事业单位的业务运营,是一个抽象而广义的用词。而在这里,program和operation的独立性,operation相对于程序program而言,就是非常具体的场景——比如查询学生明细数据并返回其聚合值是一次operation,查询学生的出生日期并计算和today的间隔也是一次operation。这里的operation就是一次次具体的“操作”,完成特定的功能(注意功能这个词很重要),如果我们把类似的操作定义在程序之中,并让它独立于程序本身,从而完成特定的功能,程序中称之为定义了一个“函数”(function)。可见,operation操作、function功能和functions函数都是具有相关性的用词,适用于不同的场合而已。

说会本书,作者这样就介绍了两种独立性:程序与数据的独立性、程序与操作的独立性。接下来,作者做了最重要的一段时间。

Data abstraction数据抽象

作者说,这种程序和数据独立性、程序与操作独立性,可以称之为“数据抽象”(data abstraction)。数据库管理系统可以为用户提供数据的“概念映射”(conceptual representation),而不需要让大家关心数据如何存储、如何执行——当我们借助Tableau查询数据库或者Prep写入数据库时,我们就无需关心数据库具体做了什么。数据模型(data model)就是数据抽象的一种方式,可以实现这样的数据映射!(注意,这里的数据模型不是我们常说的数据表关系模型,而是数据库定义)

The characteristic that allows program-data independence and program-operation independence is called data abstraction.

A DSMS provides user with a conceptual representation of data that does not include many of the details of how the data is restored or how the operation are implemented. Informally, a data model is a type of data abstraction that is used to provide the conceptual representation.

P10 “Fundamentals of Database Systems”(3rd Edition,2000)

到这里,如果有一种豁然开朗的感觉,数据库世界就算开始“登堂入室”了。

C.J.Date说数据库只是业务记录系统(record-keeping system),但是我们又不需要关心数据库如何保存,因为程序和数据分离、逻辑构建和物理实现分离,数据库的核心就是这个逻辑构建的过程。它对最终用户隐藏了数据存储和执行的细节。(The data model hides storage and implementation details that are not of interest to most database users. P10).

在图书第二章,就进一步展开data model的多个层次(level)。

1.3.3 Support of multiple views of the Data

view = perspective

即便是同一个数据库、同一份数据,不同用户使用它的视角(perspective or view)也会有所差别,如何解决这个问题?参考前面progrm-data independence的思路,数据库自然地支持每个人“按需查询”、自行处理,SQL就提供了这样的高级灵活性。

但是,如果公司的100个人都要完成类似的查询(比如每天查看“各分公司的MTD销售额),让每个人都自行完成显然就不是最佳选项,把通用查询预设在数据库系统的某个角落,用近似“调用程序”的方式完成,就能大幅提高查询效率、确保数据一致性。这个被保存下来的数据查询,就是数据库系统中常说的“view视图”。

A database typically has many users, each of whom may require a different perspective or view of the database. A view may be a subset of the database, or it may contains virtual data that is derived from the database files but is not explicitly stored. Some users may not be aware of whether the data they refer to is stored or derived.

P10 “Fundamentals of Database Systems”(3rd Edition,2000)

view视图可以视为一个简化版“program程序”,view视图和data数据之间的关系则可以视为“程序-数据独立性”的一个实现案例。对于最终用户而言,他不需要关心视图view的存储及执行原理,甚至意识不到对应的数据是存储在数据库中还是计算而来的(stored or derived),只需要引用它的名称即可获得想要的内容。

这是这一小节的关键。如果能理解Program-data independence,我们就能更深刻地理解view;反过来,我们也可以借助于view更深刻地理解程序数据独立性。

这里喜乐君举个例子,倘若我是学校学工部负责学生资助工作的老师(我大学期间就接受过此类资助,需要个人申请、学院和学工部审核),并期望借助于数据更人性化、智能化地开展学生资助工作。于是我使用Tableau连接了学校餐饮系统数据库。起初,我想每次都查看“本月的学生就餐明细”,于是使用Tableau连接数据表+数据源筛选器轻松完成这个功能;后来发现,我每次都想知道学生的就餐频次、金额、最贵消费金额等统计类的数据,受限于Tableau的能力水平,于是请教IT系的学生,在数据库中写了一段完成这个查询的代SQL代码,学生为了我使用方法,把它定义为一个view:

Create VIEW V_Eat_Student_Monthly (
…………
)

有了这个帮助,我就可以每次连接视图,甚至不需要关心学生写了什么(很可能也看不懂)。这样,我就可以每个月申请给符合特定条件的学生(对餐厅而言相当于高频单单价的穷学生)自动转账。

当然,随着敏捷BI的发展,越来越多的视图view从预设的视角转换为即时的视角,我不再需要学生预设view,Tableau可以随时帮助我完成查询——哪怕是包含聚合的查询。

因此,本质上,我们可以把可视化visualization视为“ad-hoc view”(即时视角),而把视图view视为是“priori view”(先验视角),而它们都可以视为是一个简化版的“程序”program,遵循program-data independence原则。数据分析技术的快速发展,正在让业务分析师成为分析主力,即时分析和业务决策得以紧密结合。

1.3.4 数据分享与多用户事务处理Sharing of data and Multiuser Transaction Processing

多用户的数据库(multiuser database)允许多个用户同时访问数据,这就要求DBMS必须包含Concurrency control(并发控制)程序,确保多用户同时更新数据时的准确性。作者举例说,比如多个人同时订购航班的机票,订座系统必须保证航班的每个座位在一个时间段内只能被一个人访问。这种程序称之为“On-line transaction processing(OLTP)”(联机事务处理)。

The DBMS must include concurrency control software to ensure that several users trying to update the same data do so in a controlled manner so that the result of updates is correct. …… A fundamental role of multiuser DBMS software is to ensure that concurrent transactions operate correctly .

P11 “Fundamentals of Database Systems”(3rd Edition,2000)

这里有一个重点词是transaction,它和operation一样,都既有狭义上、具体的应用场景,又被引申成广义的、抽象的宏观概念。Transaction早期专指银行的交易,比如一位顾客在ATM机器上存款100元是一次交易,取款200元也是一次交易。 随着计算机处理系统首先进入银行业,数据库工程师必须进一步拆分一笔交易的所有环节,才能确保数据一致性,于是交易就有了转入、查询、对账、执行等一系列操作。

  • 先查询子账户余额,计算转账计划(从哪些子账户转到哪些子账户)
  • 执行转账计划
  • 如果失败,尝试重新查询子账户余额,计算新的转账计划
  • 执行转账计划
  • 如果失败,放弃

当我们在完整地描述一次转账过程时,我们说这是一次“交易”;而当数据库工程师在分步骤理解一次转账过程时,我们说这是一次“事务”,一次包含很多步骤、依次执行、确保一致性的事务。

当计算机系统从银行业扩展到其他行业,transaction一词就被广泛地使用开来,抽象地指多个步骤的事务,也正因为此On-line transaction processing(OLTP)翻译为“联机事务处理”,而非“联机交易处理”。就像我们不能把企业的数据库称之为“操作数据库”,而应该称之为“运营数据库”(operation database)一样。

第一章后续的1.4介绍数据库日常使用的用户角色(Actors on the scene),而1.5介绍数据库背后的用户角色(Works behind the scene),包括DBMS设计和开发者、工具开发者、操作与运维人员等。1.6介绍数据库系统的优势(advantages of using a dDBMS),这里不再展开

  • Actors on the scene: the people whose jobs involve the day-to-day use of a large database
  • Works behind the scene:the persons are typically not interested in the database itself .

by 喜乐君 Dec, 2023


了解 喜乐君 的更多信息

订阅后即可通过电子邮件收到最新文章。

《【笔记】数据库基础入门Fundamentals of Database Systems(1)》有1个想法

  1. Pingback: [笔记]数据库基础入门 CH2-Database System Concepts and Architecture – 喜乐君

评论已关闭。