I am rebuilding a big warehouse database which originally used natural keys, and now I want to switch to surrogate keys.
Therefore, I am considering to split the database into a physical layer and a logical layer.
In the physical layer, every table gets basically two fields like this: (simplified case)
tblProducts:
ProductKey bigint identity (1,1) not null primary key,
ProductID nvarchar(100) not null unique
tblSales:
RowKey bigint identity (1,1) not null primary key,
ProductKey bigint not null references tblProducts(ProductKey),
DateSold date,
UnitsSold decimal,
UNIQUE (ProductKey, DateSold)
- The "Key" field is always the primary key which is also used in all foreign key relationships.
- The "ID" field is a unique nvarchar. The users will always work with the "ID" and never see the "key".
In the logical layer, the idea is to create views for every table which will "hide" the surrogate key and will allow me to work with the IDs. Even the database admins should finally work only with the views! For example:
View vwSales:
select
prd.ProductID,
sls.DateSold,
sls.UnitsSold
from tblSales sls inner join tblProducts prd on sls.ProductKey = prd.ProductKey
Now if I want to work with this view, I would need to create triggers which are handling all update/delete/insert and "translate" it to the physical layer. To create all these views is a lot of work. So:
- Is there a way to "automatically" create such views?
- It is a good approach to split the database into physical and logical layer at all?