我正在重建一个最初使用自然键的大型仓库数据库,现在我想切换到代理键。
因此,我正在考虑将数据库拆分为物理层和逻辑层。
在物理层,每个表基本上都有两个这样的字段:(简化情况)
tbl产品:
ProductKey bigint identity (1,1) not null primary key,
ProductID nvarchar(100) not null unique
tbl销售:
RowKey bigint identity (1,1) not null primary key,
ProductKey bigint not null references tblProducts(ProductKey),
DateSold date,
UnitsSold decimal,
UNIQUE (ProductKey, DateSold)
- “Key”字段始终是主键,也用于所有外键关系。
- “ID”字段是唯一的 nvarchar。用户将始终使用“ID”而永远不会看到“密钥”。
在逻辑层,想法是为每个表创建视图,这些视图将“隐藏”代理键并允许我使用 ID。即使是数据库管理员最终也应该只使用视图!例如:
查看大众销售:
select
prd.ProductID,
sls.DateSold,
sls.UnitsSold
from tblSales sls inner join tblProducts prd on sls.ProductKey = prd.ProductKey
现在,如果我想使用这个视图,我需要创建处理所有更新/删除/插入的触发器并将其“翻译”到物理层。创建所有这些视图需要大量工作。所以:
- 有没有办法“自动”创建这样的视图?
- 将数据库拆分为物理层和逻辑层是一种好方法吗?