Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you tell what method a constraint was created with in SQL Server 2008?

There are two ways to create a constraint,

Method A:

ALTER TABLE dbo.<tablename> ADD CONSTRAINT
<namingconventionconstraint> UNIQUE NONCLUSTERED
(
<columnname>

Method B:

CREATE UNIQUE NONCLUSTERED INDEX
<namingconventionconstraint> ON dbo.<tablename>
(
<columnname>
) ON [PRIMARY]

However, it appears that these constraints need to be dropped using a method that is dependent on how they were created (Drop constraint vs drop index). Is there a way to determine what method the constraint was created other than trying a method and seeing if it fails? I know you can have SQL Server create a drop script for you but I'm looking for a query of some sort.

like image 712
Kavet Kerek Avatar asked Feb 04 '26 04:02

Kavet Kerek


1 Answers

  • A unique constraint will appear in sys.objects with type = UQ
  • A unique index will not appear in sys.objects
  • Both appear in sys.indexes

This is why you can have the same index name for many table, but a table level unique constraint has to be database/schema unique

like image 81
gbn Avatar answered Feb 06 '26 01:02

gbn