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

(Column) CHECK constraints can cause CREATE TABLE (SELECT) queries to fail

    XMLWordPrintable

Details

    • Bug
    • Status: In Review (View Workflow)
    • Major
    • Resolution: Unresolved
    • 10.6.23
    • 10.11, 11.4, 11.8
    • None
    • None

    Description

      When do you do `CREATE TABLE ... (SELECT ... FROM)` and you don't manipulate the data in any way, any `CHECK` constraints in the column definitions of the new table are copied to from the old table. This can lead to problems when combined `AS` in the `SELECT` statement and there are column-level `CHECK` constraints:

      CREATE OR REPLACE TABLE `x`(
        `i` INT NOT NULL CHECK(`i` > 0),
        PRIMARY KEY(`i`)
      );
      CREATE OR REPLACE TABLE `x2` (SELECT `i` AS `a` FROM `x`);
      /* Errors with: Unknown column 'i' in 'CHECK' */ 
      DROP TABLE `x`;
      DROP TABLE `x2`;
      

      I am raising this as a bug, as while the documentation doesn't explicitly talk about constraints, it does state that column attributes (which I would consider the `CHECK` need to be specified manually:

      Remember that the query just returns data. If you want to use the same indexes, or the same columns attributes ([NOT] NULL, DEFAULT, AUTO_INCREMENT) in the new table, you need to specify them manually. Types and sizes are not automatically preserved if no data returned by the SELECT requires the full size, and VARCHAR could be converted into CHAR. The CAST() function can be used to forcee the new table to use certain types.

      Attachments

        Activity

          People

            sanja Oleksandr Byelkin
            rmhumphries Robert Humphries
            Votes:
            0 Vote for this issue
            Watchers:
            2 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.