Constrain a table to have only one row
Posted
by finnw
on Stack Overflow
See other posts from Stack Overflow
or by finnw
Published on 2010-05-18T11:49:15Z
Indexed on
2010/05/18
12:00 UTC
Read the original article
Hit count: 227
sql
|sql-server
What's the cleanest way to constrain a SQL table to allow it to have no more than one row?
This related question discusses why such a table might exist, but not how the constraint should be implemented.
So far I have only found hacks involving a unique key column that is constrained to have a specific value, e.g. ALWAYS_0 TINYINT NOT NULL PRIMARY KEY DEFAULT (0) CONSTRAINT CHECK_ALWAYS_0 CHECK (ALWAYS_0 = 0)
. I am guessing there is probably a cleaner way to do it.
The ideal solution would be portable SQL, but a solution specific to MS SQL Server or postgres would also be useful
© Stack Overflow or respective owner