Drop foreign key only if it exists

前端 未结 7 2062
清歌不尽
清歌不尽 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:27

    In the current version of Mariadb 10.1.26 (new Mysql), your query works:

    Key: MUL

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

    Key: <NULL>

    0 讨论(0)
  • 2021-02-05 02:29

    Here is a workaround for the DROP FOREIGN KEY IF EXISTS, that is missing in MySQL and MariaDB versions before v10.1.4. You can also use it for every other statement you want, that should be depend on the existence of an FOREIGN KEY (e.g. for SELECT "info: foreign key exists." like in the example below).

    -- DROP FOREIGN KEY IF EXISTS
    SELECT
        COUNT(*)
    INTO
        @FOREIGN_KEY_my_foreign_key_ON_TABLE_my_table_EXISTS
    FROM
        `information_schema`.`table_constraints`
    WHERE
        `table_schema` = 'my_database'
        AND `table_name` = 'my_table'
        AND `constraint_name` = 'my_foreign_key'
        AND `constraint_type` = 'FOREIGN KEY'
    ;
    -- SELECT @FOREIGN_KEY_my_foreign_key_ON_TABLE_my_table_EXISTS;
    SET @statement := IF(
        @FOREIGN_KEY_my_foreign_key_ON_TABLE_my_table_EXISTS > 0,
        -- 'SELECT "info: foreign key exists."',
        'ALTER TABLE my_table DROP FOREIGN KEY my_foreign_key',
        'SELECT "info: foreign key does not exist."'
    );
    PREPARE statement FROM @statement;
    EXECUTE statement;
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-02-05 02:36

    If you want to drop foreign key if it exists and do not want to use procedures you can do it this way (for MySQL) :

    set @var=if((SELECT true FROM information_schema.TABLE_CONSTRAINTS WHERE
                CONSTRAINT_SCHEMA = DATABASE() AND
                TABLE_NAME        = 'table_name' AND
                CONSTRAINT_NAME   = 'fk_name' AND
                CONSTRAINT_TYPE   = 'FOREIGN KEY') = true,'ALTER TABLE table_name
                drop foreign key fk_name','select 1');
    
    prepare stmt from @var;
    execute stmt;
    deallocate prepare stmt;
    

    If there is foreign key we put alter table statement in variable and if there isn't we put a dummy statement. And then we execute it.

    0 讨论(0)
  • 2021-02-05 02:36

    Similar discussion: How do I drop a foreign key constraint only if it exists in sql server?

    IF (OBJECT_ID('FK_ConstraintName', 'F') IS NOT NULL)
    

    is very useful and not mentioned yet here.

    0 讨论(0)
  • 2021-02-05 02:38

    For greater re-usability, you would indeed want to use a stored procedure. Run this code once on your desired DB:

       DROP PROCEDURE IF EXISTS PROC_DROP_FOREIGN_KEY;
        DELIMITER $$
        CREATE PROCEDURE PROC_DROP_FOREIGN_KEY(IN tableName VARCHAR(64), IN constraintName VARCHAR(64))
        BEGIN
            IF EXISTS(
                SELECT * FROM information_schema.table_constraints
                WHERE 
                    table_schema    = DATABASE()     AND
                    table_name      = tableName      AND
                    constraint_name = constraintName AND
                    constraint_type = 'FOREIGN KEY')
            THEN
                SET @query = CONCAT('ALTER TABLE ', tableName, ' DROP FOREIGN KEY ', constraintName, ';');
                PREPARE stmt FROM @query; 
                EXECUTE stmt; 
                DEALLOCATE PREPARE stmt; 
            END IF; 
        END$$
        DELIMITER ;
    

    Thereafter, you can always replace this:

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

    with this:

    CALL PROC_DROP_FOREIGN_KEY('object', 'object_ibfk_1');
    

    Your script should then run smoothly whether object_ibfk_1 actually exists or not.

    A lot of credit due to: http://simpcode.blogspot.com.ng/2015/03/mysql-drop-foreign-key-if-exists.html

    0 讨论(0)
提交回复
热议问题