Drop foreign key only if it exists

前端 未结 7 2063
清歌不尽
清歌不尽 2021-02-05 01:37

I\'m on a MySQL database.

I\'m doing this, but it doesn\'t work.

ALTER TABLE `object` DROP FOREIGN KEY IF EXISTS `object_ibfk_1`;

I\'ve

7条回答
  •  滥情空心
    2021-02-05 02:33

    Which Database you are using??

    If SQL Server

    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
    

提交回复
热议问题