during the phase of collection and analysis of requirements, the users of the application told me that many concept should be historicized. Then I'm thinking to implement it in this way:

Suppose to have to historicized a table called user details,

create table user_details(

   id int,
   mail varchar(50),
   telephone varchar(50),
   fax varchar(50),
   dateFrom date,
   dateTo date,
   primary key(id,dateFrom)

);

with the last two fields I think to manage the historicization of this entity. Any suggestions about this? Is this the better way to manage it?

有帮助吗?

解决方案

It might work, but problem nowadays is that DBMS's do not generally enforce temporal primary key constraints or temporal referential integrity constraints.

I would do like this (in T-SQL syntax):

CREATE TABLE USER
(
id_user int not null identity (1,1),
natural_key varchar(50) not null
)
;

ALTER TABLE USER
ADD CONSTRAINT [XPK_user_iduser]
PRIMARY KEY CLUSTERED (id_user ASC)
GO

ALTER TABLE USER
ADD CONSTRAINT [XAK1_user_naturalkey]
UNIQUE (natural_key ASC)
GO

CREATE TABLE USER_DETAIL
(
id_user int not null,
mail varchar(50),
telephone varchar(50),
fax varchar(50),
dateFrom date not null,
dateTo date
) ;

ALTER TABLE USER_DETAIL
ADD CONSTRAINT [XPK_userdetail_1]
PRIMARY KEY CLUSTERED (id_user ASC, dateFrom ASC)
GO

And finally RI here:

ALTER TABLE USER_DETAIL
ADD CONSTRAINT [XFK_userdetail_user_1]
FOREIGN KEY (id_user) REFERENCES USER (id_user)
ON DELETE NO ACTION
ON UPDATE NO ACTION
GO

This construct does not help stopping all anomalities but at least there is no possibility for two tuples having same starting time.

Of course you could create a table USER_DETAIL and USER_DETAIL_HIST and latter would contain values from earlier periods. Your USER_DETAIL table could contain only current records.

I would then create a following view for end-user applications:

CREATE VIEW USER_DETAIL_TOT AS
SELECT id_user,mail,telephone,fax,dateFrom,dateTo,'Current' as rowStatus
FROM USER_DETAIL
UNION ALL
SELECT id_user,mail,telephone,fax,dateFrom,dateTo,'Historical' as rowStatus
FROM USER_DETAIL_HIST
GO

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top