开发者

Check if only single record exists

开发者 https://www.devze.com 2022-12-21 07:00 出处:网络
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.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消