Limit the table to just one row

What is the cleanest way to limit an SQL table so that it has no more than one row?

This related question discusses why such a table may exist, but not how this restriction should be implemented.

So far, I have only discovered hacks with a unique key column that is limited to a specific value, for example. ALWAYS_0 TINYINT NOT NULL PRIMARY KEY DEFAULT (0) CONSTRAINT CHECK_ALWAYS_0 CHECK (ALWAYS_0 = 0). I guess there is probably a cleaner way to do this.

An ideal solution would be portable SQL, but a solution specific to MS SQL Server or postgres would also be useful.

+5
source share
3 answers

SQL Server 2008, :

CREATE TABLE MyOneRowTable (
    [id] AS (1) PERSISTED NOT NULL CONSTRAINT pk_MyOneRowTable PRIMARY KEY,
    -- rest of the columns go here
);
+1

( ) - ON INSERT, ( ). .

+4

Grant ,

dba , dba , ,

+1

All Articles