分布式数据库的四分结构设计 BCDE

时间:2023-03-09 06:57:57
分布式数据库的四分结构设计 BCDE

首先,对关系型数据库的表进行四种分类定义:

Basis 根基,Content 内容, Description 说明, Extension 扩展。

Basis:Baisis 表是唯一的,为了实现标准而得到方便,名称可以就定义为 Basis。这个表是分布式数据库的基础,以极少量的必要信息记录所有表以及表名,在设计的角度所有对表的访问都从这张表开始。

Content:Content 表就是数据库的实际内容,根据需求进行设计。应该为每一类的表提供一个前缀的分类命名,并且将定义记录在 Description 里。

Description:Description 表是用来对表的结构设计进行说明的,可以通过查询的方式获取对整个表的描述,方便引导其他设计人员以及未来的自己迅速的了解表的设计结构。它也是唯一的,为了实现标准而得到方便,名称可以就定义为 Description。

Extension:鉴于关系型数据库数据结构的简单性,Extension 表可以作为对主要内容表提供辅助的扩展,命名应该定义为 表名_Extension,它的结构和定义应该记录在 Description 表里。 通常以键值对作为列。

SQLite 代码示例 共 2 种:

第 1 种:编号索引示例

--《编号索引示例》

-- #1 创建主表和说明表
--
-- 步骤 1/2 创建表

--create table Basis
--(id integer primary key autoincrement,
--category text not null default'暂未分类',
--name text not null unique,
--version integer not null unique,
--tableName text not null unique);
--
--create table Basis_Extension
--(keyName text not null,
--keyValue text not null,
--unique(keyName,keyValue));
--
--create table Description
--(tableType text not null unique,
--description text not null);
--
--create table Description_Extension
--(keyName text not null,
--keyValue text not null,
--unique(keyName,keyValue));

-- 步骤 2/2 插入值

--insert into Description values('Description','<tableType>(唯一):记录所有类型的表,多表通过TableName_[1]或TableName_[a]来分别表示通过数字或字符区分的表。 <description>:对该表的各列进行描述');
--insert into Description values('Description_Extension','通过键值对记录进行任意的补充说明');
--insert into Description values('Basis','记录所有项目的编号(唯一、自增长)、分类、名称(唯一)、版本(唯一)、表名(唯一)');
--insert into Description values('Basis_Extension','通过键值对记录。<recoveryId>:回收的ID');

----
-- #2 创建内容表 

-- 实际编程中,这里必须使用编码的方式获取下一个ID,并手写内容表的名称。
--
-- 步骤 1/2 创建表
--
--insert into Basis values (1, '小乘', '无量寿经',1, 'FoJing_1' );
----
--create table FoJing_1
--(chapterNumber integer not null default'1' unique,
--chapterName text,
--content text not null);
--
--create table FoJing_1_Extension
--(KeyName text not null,
--KeyValue text not null,
--unique(keyName,keyValue));

--insert into Description values('FoJing_[1]','记录佛经的章节数(唯一)、章节名(可空)、名称、内容');
--insert into Description values('FoJing_[1]_Extension','通过键值对记录佛经的版本、更新时间、编辑者');

-- 步骤 2/2 插入值

--insert into FoJing_1 values (1,null,'经文内容');
--insert into FoJing_1_Extension values('updateTime','codeForCurrentTime'),('editor','一星');

-- #3 更新内容表

--update FoJing_1 set content = '新的经文内容' where chapterNumber = '1';
--update Basis set version = version + 1 where id = 1;
--update FoJing_1_Extension set keyValue = 'codeForCurrentTime' where keyName = 'updateTime';

-- #4 删除内容表

--delete from Basis where id = '1';
--insert into Basis_Extension values ('recoveryId','1');
--drop table if exists FoJing_1;
--drop table if exists Fojing_1_Extension;

第 2 种:数字索引示例

--《数字索引示例》
--
-- #1 创建主表和说明表
--
-- 步骤 1/2 创建表

--create table Basis
--(id integer primary key autoincrement,
--category text not null default'暂未分类',
--indexStart integer not null unique,
--tableName text not null unique);
--
--create table Basis_Extension
--(keyName text not null,
--keyValue text not null,
--unique(keyName,keyValue));
--
--create table Description
--(tableType text not null unique,
--description text not null);
--
--create table Description_Extension
--(keyName text not null,
--KeyValue text not null,
--unique(keyName,keyValue));

-- 步骤 2/2 插入值

--insert into Basis_Extension values ('indexLength','10000');
--insert into Description values('Description','<tableType>(唯一):记录所有类型的表,多表通过TableName_[1]或TableName_[a]来分别表示通过数字或字符区分的表。 <description>:对该表的各列进行描述');
--insert into Description values('Description_Extension','通过键值对记录进行任意的补充说明');
--insert into Description values('Basis','记录所有项目的编号(唯一、自增长)、分类、索引起始数(唯一)、表名(唯一)');
--insert into Description values('Basis_Extension','通过键值对记录。<startLength>:索引步长');

-- #2 创建内容表
--
-- 步骤 1/4 创建表
--
--insert into Basis values (1, 'store','1', 'Account_1' ),(2, 'store','10001','Account_2');
--
--create table Account_1
--(accountNumber integer primary key,
--accountName text not null,
--accountPassword text not null,
--email text,
--otherInfo text);
--
--create table Account_1_Extension
--(KeyName text not null,
--KeyValue text not null,
--unique(keyName,keyValue));
--
--create table Account_2
--(accountNumber integer not null unique,
--accountName text not null,
--accountPassword text not null,
--email text,
--otherInfo text);
--
--create table Account_2_Extension
--(KeyName text not null,
--KeyValue text not null,
--unique(keyName,keyValue));
--
--insert into Account_1_Extension values ('accountCount','0');
--insert into Account_2_Extension values ('accountCount','0');
--
--insert into Description values('Account_[1]','记录数字帐号(唯一)、帐号名、帐号密码等等信息');
--insert into Description values('Account_[1]_Extension','通过键值对记录该区域账户的实际数量,以及其他辅助信息');

-- 说明:

-- 步骤 2/4 查询表信息
--
--select keyValue from Basis_Extension where keyName = 'indexLength';
--
--select tableName from Basis where indexStart < 18546 and indexStart > (18546 - 10000);

-- 步骤 3/4 插入值

--insert into Account_2 values (18546,'一星','md5-pw',null,null);

-- 步骤 4/4 更新扩展表

--update Account_2_Extension set keyValue = keyValue + 1 where keyName = 'accountCount';

-- #3 更新内容表

--update Account_2 set email = 'new@email.com' where accountNumber = '18546';

-- #4 删除内容表的值

--delete from Account_2 where accountNumber = '18546';
--update Account_2_Extension set keyValue = keyValue - 1 where keyName = 'accountCount';

#