Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-32270

ALTER TABLE: DROP CONSTRAINT is ignored when ADD CONSTRAINT is also present

Details

    Description

      When an `ALTER TABLE` statement tries to drop an constraint while also adding one, the drop operation is not performed:

       
      MariaDB [test]> create table a (id int primary key);
      Query OK, 0 rows affected (0.016 sec)
       
      MariaDB [test]> create table b (id int primary key references a(id));
      Query OK, 0 rows affected (0.015 sec)
       
      MariaDB [test]> show create table b\G
      *************************** 1. row ***************************
             Table: b
      Create Table: CREATE TABLE `b` (
        `id` int(11) NOT NULL,
        PRIMARY KEY (`id`),
        CONSTRAINT `b_ibfk_1` FOREIGN KEY (`id`) REFERENCES `a` (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
      1 row in set (0.001 sec)
       
      MariaDB [test]> alter table b drop constraint b_ibfk_1, add constraint other foreign key (id) references a(id) on update cascade;
      Query OK, 0 rows affected (0.023 sec)              
      Records: 0  Duplicates: 0  Warnings: 0
       
      MariaDB [test]> show create table b\G
      *************************** 1. row ***************************
             Table: b
      Create Table: CREATE TABLE `b` (
        `id` int(11) NOT NULL,
        PRIMARY KEY (`id`),
        CONSTRAINT `b_ibfk_1` FOREIGN KEY (`id`) REFERENCES `a` (`id`),
        CONSTRAINT `other` FOREIGN KEY (`id`) REFERENCES `a` (`id`) ON UPDATE CASCADE
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
      1 row in set (0.010 sec)
      

      Attachments

        Issue Links

          Activity

            dotdash Björn Steinbrink created issue -
            dotdash Björn Steinbrink made changes -
            Field Original Value New Value
            Description When an `ALTER TABLE` statement tries to drop an constraint while also adding one, the drop operation is not performed:

            ```
            MariaDB [test]> create table a (id int primary key);
            Query OK, 0 rows affected (0.016 sec)

            MariaDB [test]> create table b (id int primary key references a(id));
            Query OK, 0 rows affected (0.015 sec)

            MariaDB [test]> show create table b\G
            *************************** 1. row ***************************
                   Table: b
            Create Table: CREATE TABLE `b` (
              `id` int(11) NOT NULL,
              PRIMARY KEY (`id`),
              CONSTRAINT `b_ibfk_1` FOREIGN KEY (`id`) REFERENCES `a` (`id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
            1 row in set (0.001 sec)

            MariaDB [test]> alter table b drop constraint b_ibfk_1, add constraint other foreign key (id) references a(id) on update cascade;
            Query OK, 0 rows affected (0.023 sec)
            Records: 0 Duplicates: 0 Warnings: 0

            MariaDB [test]> show create table b\G
            *************************** 1. row ***************************
                   Table: b
            Create Table: CREATE TABLE `b` (
              `id` int(11) NOT NULL,
              PRIMARY KEY (`id`),
              CONSTRAINT `b_ibfk_1` FOREIGN KEY (`id`) REFERENCES `a` (`id`),
              CONSTRAINT `other` FOREIGN KEY (`id`) REFERENCES `a` (`id`) ON UPDATE CASCADE
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
            1 row in set (0.010 sec)
            ```
            When an `ALTER TABLE` statement tries to drop an constraint while also adding one, the drop operation is not performed:


            {code:sql}

            MariaDB [test]> create table a (id int primary key);
            Query OK, 0 rows affected (0.016 sec)

            MariaDB [test]> create table b (id int primary key references a(id));
            Query OK, 0 rows affected (0.015 sec)

            MariaDB [test]> show create table b\G
            *************************** 1. row ***************************
                   Table: b
            Create Table: CREATE TABLE `b` (
              `id` int(11) NOT NULL,
              PRIMARY KEY (`id`),
              CONSTRAINT `b_ibfk_1` FOREIGN KEY (`id`) REFERENCES `a` (`id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
            1 row in set (0.001 sec)

            MariaDB [test]> alter table b drop constraint b_ibfk_1, add constraint other foreign key (id) references a(id) on update cascade;
            Query OK, 0 rows affected (0.023 sec)
            Records: 0 Duplicates: 0 Warnings: 0

            MariaDB [test]> show create table b\G
            *************************** 1. row ***************************
                   Table: b
            Create Table: CREATE TABLE `b` (
              `id` int(11) NOT NULL,
              PRIMARY KEY (`id`),
              CONSTRAINT `b_ibfk_1` FOREIGN KEY (`id`) REFERENCES `a` (`id`),
              CONSTRAINT `other` FOREIGN KEY (`id`) REFERENCES `a` (`id`) ON UPDATE CASCADE
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
            1 row in set (0.010 sec)
            {code}

            alice Alice Sherepa made changes -
            Fix Version/s 10.4 [ 22408 ]
            Fix Version/s 10.5 [ 23123 ]
            Fix Version/s 10.6 [ 24028 ]
            Fix Version/s 10.9 [ 26905 ]
            Fix Version/s 10.10 [ 27530 ]
            Fix Version/s 10.11 [ 27614 ]
            Fix Version/s 11.0 [ 28320 ]
            alice Alice Sherepa made changes -
            Affects Version/s 10.4 [ 22408 ]
            Affects Version/s 10.5 [ 23123 ]
            Affects Version/s 10.6 [ 24028 ]
            Affects Version/s 10.9 [ 26905 ]
            Affects Version/s 10.10 [ 27530 ]
            Affects Version/s 10.11 [ 27614 ]
            alice Alice Sherepa made changes -
            Status Open [ 1 ] Confirmed [ 10101 ]
            alice Alice Sherepa made changes -
            Assignee Sergei Golubchik [ serg ]
            serg Sergei Golubchik made changes -
            Assignee Sergei Golubchik [ serg ] Marko Mäkelä [ marko ]
            marko Marko Mäkelä made changes -
            marko Marko Mäkelä made changes -
            Component/s Data Definition - Alter Table [ 10114 ]
            Component/s Storage Engine - InnoDB [ 10129 ]
            Labels foreign-keys
            marko Marko Mäkelä made changes -
            Assignee Marko Mäkelä [ marko ] Vladislav Lesin [ vlad.lesin ]
            julien.fritsch Julien Fritsch made changes -
            Fix Version/s 10.9 [ 26905 ]
            julien.fritsch Julien Fritsch made changes -
            Fix Version/s 10.10 [ 27530 ]
            julien.fritsch Julien Fritsch made changes -
            Fix Version/s 11.0 [ 28320 ]
            julien.fritsch Julien Fritsch made changes -
            Fix Version/s 10.4 [ 22408 ]

            People

              vlad.lesin Vladislav Lesin
              dotdash Björn Steinbrink
              Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.