Hi All, I wondered if you could clear something up for me.
I have a database that we are currently attempting to roll out as a web app. Large scale changes are happening to the schema and we are currently working on provision for recording history within the database concentrating on the 'main' table initially.
It has been decided that we will insert an updated copy of a record along with the date and time that this operation was completed instead of updating the current record. This has meant that we have expanded the primary key to include the datetime that the insert was completed.
Currently the primary key of this table is used as a foreign key in many other tables within the database which may or may not be updated at the same time.
I decided that the simplest way to maintain this is to create a table with an auto incrementing primary key that can then occur in the 'main' table as a foreign key that is also a composite part of the primary key. All foreign key relationships in the other tables link to this table rather than the main table.
It has been suggested by others within the team that we don't need this additional table and can achieve the same result by keeping the 'main' table with it's identity column and then simply turning identity insert on or off dependent on if it was a logical insert or update that we are performing.
My problem with this is that it could create many to many relationships between the 'main' and FK tables.
I would appreciate any feedback that you may wish to give on this - included below is some code fragments for my solution.
tblEvent used to have a PK of EventID with identity insert on
I have a database that we are currently attempting to roll out as a web app. Large scale changes are happening to the schema and we are currently working on provision for recording history within the database concentrating on the 'main' table initially.
It has been decided that we will insert an updated copy of a record along with the date and time that this operation was completed instead of updating the current record. This has meant that we have expanded the primary key to include the datetime that the insert was completed.
Currently the primary key of this table is used as a foreign key in many other tables within the database which may or may not be updated at the same time.
I decided that the simplest way to maintain this is to create a table with an auto incrementing primary key that can then occur in the 'main' table as a foreign key that is also a composite part of the primary key. All foreign key relationships in the other tables link to this table rather than the main table.
It has been suggested by others within the team that we don't need this additional table and can achieve the same result by keeping the 'main' table with it's identity column and then simply turning identity insert on or off dependent on if it was a logical insert or update that we are performing.
My problem with this is that it could create many to many relationships between the 'main' and FK tables.
I would appreciate any feedback that you may wish to give on this - included below is some code fragments for my solution.
tblEvent used to have a PK of EventID with identity insert on
Code:
CREATE TABLE [dbo].[ktblEvent](
[EventID] [bigint] IDENTITY(1,1) NOT NULL,
[CreatedDateTime] [datetime] NOT NULL,
CONSTRAINT [PK_ktblEvent] PRIMARY KEY NONCLUSTERED
(
[EventID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [tblEvent] (
[EventID] [bigint] NOT NULL,
..............................
[FromDatetime] [datetime] NOT NULL DEFAULT CURRENT_TIMESTAMP,
[ToDatetime] [datetime] NULL,
[UserId] [nvarchar] (256) NOT NULL DEFAULT USER_NAME(),
CONSTRAINT [PK_tblEvent] PRIMARY KEY NONCLUSTERED (
[EventID],
[FromDatetime])
)
ALTER TABLE [dbo].[tblEvent] WITH CHECK ADD CONSTRAINT [FK_tblEvent_EventID_ktblEvent] FOREIGN KEY([EventID])
REFERENCES [dbo].[ktblEvent] ([EventID])
ALTER TABLE [dbo].[tblEvent] CHECK CONSTRAINT [FK_tblEvent_EventID_ktblEvent]
CREATE TABLE [tblActivityCodes] (
[EventID] [bigint] NOT NULL,
[ActivityDate] [datetime] NOT NULL,
........................................
[FromDatetime] [datetime] NOT NULL DEFAULT CURRENT_TIMESTAMP,
[ToDatetime] [datetime] NULL,
[UserId] [nvarchar] (256) NOT NULL DEFAULT USER_NAME(),
CONSTRAINT [PK_tblActivityCodes] PRIMARY KEY NONCLUSTERED (
[EventID],
[ActivityDate],
[FromDatetime])
)
ALTER TABLE [tblActivityCodes] WITH CHECK ADD CONSTRAINT [FK_tblActivityCodes_EventID_ktblEvent] FOREIGN KEY ([EventID]) REFERENCES [ktblEvent]
ALTER TABLE [tblActivityCodes] CHECK CONSTRAINT [FK_tblActivityCodes_EventID_ktblEvent]