How can I check if a SQL Server constraint exists?

sql, sql-server, sql-server-2008

Solution

 SELECT
    * 
    FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS 

or else try this

  SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName,
type_desc AS ConstraintType
FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT' 

or

IF EXISTS(SELECT 1 FROM sys.foreign_keys WHERE parent_object_id = OBJECT_ID(N'dbo.TableName'))
 BEGIN 
ALTER TABLE TableName DROP CONSTRAINT CONSTRAINTNAME 
END 

Problem

I have the following: ``` IF OBJECT_ID(N'[dbo].[webpages_Roles_UserProfiles_Target]', 'xxxxx') IS NOT NULL DROP CONSTRAINT [dbo].[webpages_Roles_UserProfiles_Target] ``` I want to be able to check if there is a constraint existing before I drop it. I use the code above with a type of 'U' for tables. How could I modify the code above (change the xxxx) to make it check for the existence of the constraint ?

Original source

Related problems