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

No warning when trying to name a primary key constraint

Details

    Description

      Our documentation says:

      "For UNIQUE keys, PRIMARY KEYs and FOREIGN KEYs, you can specify a name for the constraint, using the CONSTRAINT keyword. That name will be used in error messages."

      <https://mariadb.com/kb/en/library/create-table/>

      But for primary keys this is not true, the constraint name is just silently ignored:

      This CREATE succeeds without warning:

      CREATE TABLE t1 (
        id1 INT,
        id2 INT,
        CONSTRAINT `foo` PRIMARY KEY (id1),
        CONSTRAINT `bar` UNIQUE KEY(id2)
      );
      

      When verifying with SHOW CREATE TABLE the constraint name
      for the UNIQUE key is shown, but not that of the PRIMARY key:

      MariaDB [test]> show create table t1\G
      *************************** 1. row ***************************
             Table: t1
      Create Table: CREATE TABLE `t1` (
        `id1` int(11) NOT NULL,
        `id2` int(11) DEFAULT NULL,
        PRIMARY KEY (`id1`),
        UNIQUE KEY `bar` (`id2`)
      ) ENGINE=InnoDB DEFAULT CHARSET=latin1
      

      INFORMATION_SCHEMA.TABLE_CONSTRAINTS also does not show the
      chosen name `foo` for the primary key:

      +--------------------+-------------------+-----------------+--------------+------------+-----------------+
      | CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_SCHEMA | TABLE_NAME | CONSTRAINT_TYPE |
      +--------------------+-------------------+-----------------+--------------+------------+-----------------+
      | def                | test              | PRIMARY         | test         | t1         | PRIMARY KEY     |
      | def                | test              | bar             | test         | t1         | UNIQUE          |
      +--------------------+-------------------+-----------------+--------------+------------+-----------------+
      

      And the name is not shown in primary key related error messages either, contrary to what the knowledge base claims:

      MariaDB [test]> insert into t1 values(1,1);
      Query OK, 1 row affected (0.004 sec)
       
      MariaDB [test]> insert into t1 values(2,1);
      ERROR 1062 (23000): Duplicate entry '1' for key 'bar'
       
      MariaDB [test]> insert into t1 values(1,2);
      ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
      

      My main concern though is that there is no warning on CREATE about an unsupported feature being used and the chosen name being discarded in favour of the hardcoded "PRIMARY" name string.

      Attachments

        Activity

          I edited the CREATE TABLE page in the documentation to clarify that for PRIMARY KEY the name is accepted but ignored. This should be also explained at CONSTRAINT page https://mariadb.com/kb/en/library/constraint/ where the syntax does not even mention index_name can be specified for PRIMARY KEY

          The server definitely should return warning so it is not documentation only problem.

          salle Alexander Keremidarski added a comment - I edited the CREATE TABLE page in the documentation to clarify that for PRIMARY KEY the name is accepted but ignored. This should be also explained at CONSTRAINT page https://mariadb.com/kb/en/library/constraint/ where the syntax does not even mention index_name can be specified for PRIMARY KEY The server definitely should return warning so it is not documentation only problem.

          Assigning to serg for re-distribution.

          elenst Elena Stepanova added a comment - Assigning to serg for re-distribution.
          holyfoot Alexey Botchkov added a comment - http://lists.askmonty.org/pipermail/commits/2019-July/013889.html

          This is good, just one detail — you cannot add new errors or warnings to 10.3, because 10.4 is already GA, and adding new warnings to 10.3 would cause 10.4 error numbers to change.

          So, just use, say,

                push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                                    ER_WRONG_NAME_FOR_INDEX,
                                    "Name '%-.100s' ignored for PRIMARY key.",
                                    key->name.str);
          

          serg Sergei Golubchik added a comment - This is good, just one detail — you cannot add new errors or warnings to 10.3, because 10.4 is already GA, and adding new warnings to 10.3 would cause 10.4 error numbers to change. So, just use, say, push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, ER_WRONG_NAME_FOR_INDEX, "Name '%-.100s' ignored for PRIMARY key." , key->name.str);
          holyfoot Alexey Botchkov added a comment - https://github.com/MariaDB/server/commit/c6efbc543d6fb3004f320ef9d02542a4692ca88a

          You forgot to update .result files for MyRocks tests. Now fixed.

          psergei Sergei Petrunia added a comment - You forgot to update .result files for MyRocks tests. Now fixed.

          People

            holyfoot Alexey Botchkov
            hholzgra Hartmut Holzgraefe
            Votes:
            1 Vote for this issue
            Watchers:
            9 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Git Integration

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