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

Different behavior of foreign keys in CREATE TABLE statements

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical
    • Resolution: Not a Bug
    • Affects Version/s: 5.5.33a
    • Fix Version/s: N/A
    • Component/s: Parser
    • Labels:
    • Environment:
      Suse Linux 13.1

      Description

      Inconsistent behavior in CREATE TABLE statements: When foreign key statements are listed directly behind attributes they do not get a constraint while the get one if the foreign keys statements are listed separately after all attributes. The following examples will demonstrate this:

      Welcome to the MariaDB monitor.  Commands end with ; or \g.
      Your MariaDB connection id is 1
      Server version: 5.5.33-MariaDB openSUSE package
      
      Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
      
      MariaDB [(none)]> create database testdb;
      Query OK, 1 row affected (0.00 sec)
      
      MariaDB [(none)]> use testdb;
      Database changed
      MariaDB [testdb]> create table t(id int primary key);
      Query OK, 0 rows affected (0.01 sec)
      
      MariaDB [testdb]> show create table t;
      +-------+-------------------------------------------------------------------------------------------------------+
      | Table | Create Table                                                                                          |
      +-------+-------------------------------------------------------------------------------------------------------+
      | t     | CREATE TABLE `t` (
        `id` int(11) NOT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
      +-------+-------------------------------------------------------------------------------------------------------+
      1 row in set (0.00 sec)
      
      MariaDB [testdb]> create table t2(id int, t_id int, foreign key (t_id) references t(id));
      Query OK, 0 rows affected (0.02 sec)
      
      MariaDB [testdb]> show create table t2;
      +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
      | Table | Create Table                                                                                                                                                                                                      |
      +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
      | t2    | CREATE TABLE `t2` (
        `id` int(11) DEFAULT NULL,
        `t_id` int(11) DEFAULT NULL,
        KEY `t_id` (`t_id`),
        CONSTRAINT `t2b_ibfk_1` FOREIGN KEY (`t_id`) REFERENCES `t` (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
      +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
      1 row in set (0.00 sec)
      
      MariaDB [testdb]> create table t3(id int, t_id int references t(id));
      Query OK, 0 rows affected (0.02 sec)
      
      MariaDB [testdb]> show create table t3;
      +-------+---------------------------------------------------------------------------------------------------------------------+
      | Table | Create Table                                                                                                        |
      +-------+---------------------------------------------------------------------------------------------------------------------+
      | t3    | CREATE TABLE `t3` (
        `id` int(11) DEFAULT NULL,
        `t_id` int(11) DEFAULT NULL
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
      +-------+---------------------------------------------------------------------------------------------------------------------+
      1 row in set (0.00 sec)
      
      MariaDB [testdb]> 
      

        Gliffy Diagrams

          Attachments

            Activity

            Hide
            elenst Elena Stepanova added a comment -

            See MySQL manual (the note applies to MariaDB as well):
            http://dev.mysql.com/doc/refman/5.6/en/create-table.html

            MySQL parses but ignores “inline REFERENCES specifications” (as defined in the SQL standard) where the references are defined as part of the column specification. MySQL accepts REFERENCES clauses only when specified as part of a separate FOREIGN KEY specification.

            Show
            elenst Elena Stepanova added a comment - See MySQL manual (the note applies to MariaDB as well): http://dev.mysql.com/doc/refman/5.6/en/create-table.html MySQL parses but ignores “inline REFERENCES specifications” (as defined in the SQL standard) where the references are defined as part of the column specification. MySQL accepts REFERENCES clauses only when specified as part of a separate FOREIGN KEY specification.
            Hide
            pintman Marco Bakera added a comment -

            Thanks for clarification of this 'strange' behavior.

            Show
            pintman Marco Bakera added a comment - Thanks for clarification of this 'strange' behavior.

              People

              • Assignee:
                Unassigned
                Reporter:
                pintman Marco Bakera
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: