数据库设计相关知识

-- TOC --

本文汇总一些在设计数据库的时候,可能需要参考的背景知识。

为什么要用数据库

用文件就可以存放数据,但写过真正系统的人都应该有体会,当系统内部的数据存储不再那么单一单纯的时候,完全使用文件来存放数据,读写接口会存在很大的问题。此时,自然而然就会开始过度到对数据库的使用上来。如果还不使用数据库,系统中对多个文件的读写接口,实际上也已经开始朝着数据库的方向在发展了。

数据库设计之范式

范式这个词是翻译过来的,原文是 Normalization Form。

数据库设计时,有一些共性的设计规则,称之为范式。学习这些设计范式,可以让我们设计出结构良好的表结构(数据库里面全是Table)。比如,最基本的设计考虑之一就是,不要在不同的位置存放相同的数据,这样的设计会出现数据同步的问题。

反范式(Denormalization)有时也是刻意为之的,设计良好的表结构,并不意味着执行效率就是最高的,如果对执行效率有更高的要求,我们就要考虑反范式的设计,一般都是通过临时表。

良好的数据库表结构设计,带来的好处,可能远远超过执行速度提升带来的,毕竟保证数据的可靠一致完整,应该是最重要的!

1NF

First Normal Form,是最优先要考虑的设计范式,1NF内含3个需要满足的条件:

1NF中,可能就第3个条件,需要好好考虑,比如存放时间,是否要把年月日单独存放?

2NF

2NF是针对一个表级约束,primary key是多column的场景。

2NF的内容是:

比如我们用 build_id 和 room_id 组合在一起唯一确定一个 meeting room,如果有一列address,当某个building内有很多间会议室的时候,你就会发现address的内容在重复,此时就属于违反的2NF设计范式。

3NF

3NF的内容:

从数据关系上分析,A依赖B,B依赖C,这就是有传递依赖的关系。在设计表结构的时候,中间的那个B,就要想一想,是否能用。用的话,可能会出现冗余数据。

1NF,2NF和3NF,都与数据不冗余,不重复出现有关!

更高阶的范式

4NF,也叫BCNF。还有5NF,6NF。一般都不怎么用得上,你设计的表格满足3NF的时候,基本上就是良好设计。

数据库设计之表间的三种关系

数据库设计就是设计表,Table!每一张Table都是二维的,可以有各种约束关系(表级约束,外键约束),以及相互之间的对应关系。

表与表之间有三种关系:

one-to-one

这是一种比较严格的对应关系,表A中的一行row,只对应表B中的一行row。

在设计的时候,foreign key 加上 unique 约束就是这样的关系了!

还有个专业的说法,detailed table,想想其实就是这样,对应的row,就是自然延伸,包含更多数据。

one-to-many

一对多的关系可能是最常见的。

一般的foreign key设计,都是一对多。当表中的某row,包含或延伸的数据是 list 或 array 这样的场景式,自然就设计出一对多的关系表。

many-to-many

最经典的例子,人和分组之间的关系,就是many-to-many。一个人可以在多个组中,一个组也包含了多个人。

这种数据关系,需要一个 bridge table,或 link table,来将两个表的数据专门对用起来,如下图:

many-to-many

DDL,DML,TCL,DCL

DDL

Data Definition Language,数据定义语言。

The first language is the Data Definition Language, or DDL, which refers to commands that define the structure of tables, views, indexes, and other data containers and objects within the database. CREATE TABLE (used to define a new table) and DROP VIEW (used to delete a view) are examples of DDL commands.

DDL用来管理container,比如table,index,view。table是SQL数据库的基本数据结构,还有 临时table (create temp table ...),和 virtual table。

Temporary tables have two specific features. First, temporary tables can only be seen by the database connection that created them. This allows the simultaneous re-use of table names without any worry of conflict between different clients. Second, all associated temporary tables are automatically dropped and deleted whenever a data-base connection is closed. 临时表的两个特性:(1)只能创建者能看到临时表;(2)数据库连接关闭,临时表自动删除。

管理方式有:create,alter,drop。

A view, on the other hand, is fully dynamic. Every time the view is referenced or queried, the underlying SELECT statement is run to regenerate the view. This means the data seen in a view automatically updates as the data changes in the underlying tables. In a sense,views are almost like named queries. view创建后不会自动删除,每一次访问view,用于创建view的select语句就会被执行一次,因此每次得到的都是最新的select结果。

约束(constraint)可以应用于一个或多个column,后者有时也被称为 table constraint,表级约束。

Index可以单独创建,也可以在create table的时候,设置unique约束,SQLite3会自动为有unique约束的column,创建一个index。单独创建index,就可以选择是否要加上unique约束,有一些column的数据也很散列,但是不满足unique,也可以考虑增加index。

DML

Data Manipulation Language,数据操作语言。

The second category of commands is known as Data Manipulation Language, or DML. These are all of the commands that insert, update, delete, and query actual data values from the data structures defined by the DDL. INSERT (used to insert new values into a table) and SELECT (used to query or look up data from tables) are examples of DML commands.

就是 select,insert,update,delete。

python3的sqlite3模块,默认在使用DML语句的时候,需要显示调用commit哦!除非自己设置了合适的isolation_level,或者显示的使用transaction。

insert一般一次插入一行,如果insert后面跟select语句,就可以实现同时插入多行。update和delete,只要满足条件,都是多行模式!

select语句是SQL中最复杂,最多变的。(select的结果一般是一个table,此table可用于insert,或者create temp table或者view等操作)

TCL

Transaction Control language,事务控制语言。

Related to the DML and DDL is the Transaction Control Language, or TCL. TCL commands can be used to control transactions of DML and DDL commands. BEGIN (used to begin a multistatement transaction) and COMMIT (used to end and accept a transaction) are examples of TCL commands.

有 begin,commit,rollback。

事务要满足ACID(Atomic,Consistent,Isolated,Durable)要求。

DCL

Data Control Language,数据控制语言,用于权限控制。

The last category is the Data Control Language, or DCL. The main purpose of the DCL is to grant or revoke access control. Much like file permissions, DCL commands are used to allow (or deny) specific database users (or groups of users) permission to utilize or access specific resources within a database. These permissions can apply to both the DDL and the DML. DDL permissions might include the ability to create a real or temporary table, while DML permissions might include the ability to read, update, or delete the records of a specific table. GRANT (used to assign a permission) and REVOKE (used to delete an existing permission) are the primary DCL commands.

SQLite supports the majority of standardized DDL, DML, and TCL commands but lacks any DCL commands. Because SQLite does not have user names or logins, it does not have any concept of assigned permissions. Rather, SQLite depends on datatype permissions to define who can open and access a database.

SQLite没有DCL部分,因为它是基于文件的数据库,文件访问控制权限,是Linux操作系统的事儿。

本文链接:https://cs.pynote.net/sf/sqlite/202203191/

-- EOF --

-- MORE --