I have created a foreign key without specifying the name so sql server has created it with auto generated name. Now I want to drop the column which has this foreign key. The problem is that i don't know the name of this foreign key. Is there any way to drop all the foreign keys for particular column in particular table?
So far I've found this script which drops all default constraints for column
DECLARE @tableName VARCHAR(MAX)
DECLARE @ConstraintName nvarchar(200)
DECLARE @columnName VARCHAR(MAX)
SET @tableName = 'tablename'
SET @columnName = 'columnname'
SELECT @ConstraintName = Name
FROM SYS.DEFAULT_CONSTRAINTS
WHERE PARENT_OBJECT_ID = OBJECT_ID(@tableName)
AND PARENT_COLUMN_ID = (
SELECT column_id FROM sys.columns
WHERE NAME = @columnName AND object_id = OBJECT_ID(@tableName))
IF @ConstraintName IS NOT NULL
BEGIN
EXEC('ALTER TABLE '+@tableName+' DROP CONSTRAINT ' + @ConstraintName)
END
ALTER TABLE [tablename] DROP COLUMN columnname
GO
But it doesn't help with foreign key constraints.
EXEC sp_help 'TABLE_NAME';From the result set copy the FK constraint name and then drop it using theDROP CONSTRAINTcommand!