3

I have a table that has a foreign key relation with another table.

I want to check that if this relation exists, drop it.

How to I write this query.

Thanks.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
Tavousi
  • 14,848
  • 18
  • 51
  • 70

1 Answers1

9

If you are using SQL Server, this should works

if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FKName]') AND parent_object_id = OBJECT_ID('TableName'))
  alter table TableName drop constraint FKName
Iridio
  • 9,213
  • 4
  • 49
  • 71