I have a table with
GID
ID
DefaultA
DefaultB
DefaultC
Def开发者_如何学PythonaultD
DefaultE
DefaultF
my requirement is that there should always be one record in the database always.
In the front end if the end user tries to update its ok since he will update the existing record.But if he tries to enter a new record I must stop him.
Create a primary key field, type tinyint, and set the constraint so that the value can only be "1" (oe you could use a unique index on the field). Set the field value to "1" for you single record. Then no other record can be entered.
Assuming GID is the primary key, here is sql code to add the constraint to your existing table:
ALTER TABLE dbo.address ADD CONSTRAINT
single_record CHECK (GID = 1)
Eliminate all Insert, delete permissions from all users...
or,
Add a trigger that rolls back any transaction that attempts to delete or insert a record.
Create Trigger MyTableEnsureSingleRowTrigger
On MyTable for Insert, Delete
As Rollback Transaction
精彩评论