We have a table that needs to be locked down in a specific way and we are looking for the best (reliability is first priority, performance is second) way to accomplish it. The rules involving this table are that new records can be added freely but once they have been then, with one exception, they should never be changed. There is going to be one valid update command allowed for records in this table, all other updates or deletes should be blocked. Here is a rough example of the approach we have worked out so far, are there any good ways to improve it?
Table looks like this:
CREATE TABLE [dbo].[Table_1](
ID int IDENTITY(1,1) PRIMARY KEY NOT NULL
,data1 varchar(64) NULL
,data2 varchar(64) NULL
,data3 int NULL
,data4 bit NULL
,ModifiedBy VARCHAR(32)
,ModifiedDtTm DATETIME
)
The only allowable update will be via a proc which resembles this:
UPDATE dbo.Table_1
SET
data4 = 1
,ModifiedBy = @User
,ModifiedDtTm = GETDATE()
WHERE ID = @ID
What we have in mind to lock the table is to create these two triggers:
CREATE TRIGGER [dbo].[Table_1_UpdtLock]
ON [dbo].[Table_1]
INSTEAD OF UPDATE
AS
BEGIN
IF COLUMNS_UPDATED() = 0x70
UPDATE dbo.Table_1
SET
data4 = I.data4
,ModifiedBy = I.ModifiedBy
,ModifiedDtTm = I.ModifiedDtTm
FROM dbo.Table_1 AS T
INNER JOIN INSERTED AS I
ON T.ID = I.ID
WHERE I.data4 = 1
ELSE
BEGIN
RAISERROR ('Table is locked.', 16, 0)
ROLLBACK
END
END
and
CREATE TRIGGER [dbo].[Table_1_DelLock]
ON [dbo].[Table_1]
INSTEAD OF DELETE
AS
BEGIN
ROLLBACK
RAISERROR ('Table is locked.', 16, 0)
END
This has not been implemented yet and we are trying to make sure we have it right the first time since there are potential legal ramifications if this data gets corrupted or lost.
Anyone able to see any holes that would allow someone to edit the data in this table? The only one I am aware of at this point is that the table can be truncated without activating the delete trigger. The best answer I have to that at the moment is to establish a FK reference to this table.
Any potential performance problems that stand out?
Suggestions for a better approach?
Thanks for the assist.
EDIT:
I should note that access to the database will be restricted as tightly as we can make it. However there is always an account with DBO and our SAs generally don't know much about what we are doing with any given database. I know that people with these permissions can circumvent anything we put in place, the goal at this level is to prevent accidents. I should also note that we want it to throw an error if someone attempts an illegal update, it should not fail silently or perform a partial update on the allowed fields.