SQL Unique Record not column?

前端 未结 4 1739
[愿得一人]
[愿得一人] 2021-01-07 02:14

Is there a way to insert into an SQL database where the whole record is unique? I know you can make primary keys and unique columns, but that is not what I want.

Wha

4条回答
  •  一向
    一向 (楼主)
    2021-01-07 03:03

    You could add a unique constraint to the table definition and include all columns (except the primary key, I believe). Unique constraints create indexes, so I have no idea what sort of performance impact this might have, but I'd guess the fewer columns, the better.

    This will add such a constraint to an existing table:

    ALTER TABLE SampleTable
    ADD CONSTRAINT [uc_UniqueRow] UNIQUE (Column1, Column2, Column3)
    

    Note that there are certain restrictions on column types etc. so this may or may not work for your table. See Books Online for details: http://msdn.microsoft.com/en-us/library/ms177420(sql.90).aspx

提交回复
热议问题