[MDEV-7546] Different behavior of foreign keys in CREATE TABLE statements Created: 2015-02-05  Updated: 2015-02-06  Resolved: 2015-02-06

Status: Closed
Project: MariaDB Server
Component/s: Parser
Affects Version/s: 5.5.33a
Fix Version/s: N/A

Type: Bug Priority: Critical
Reporter: Marco Bakera Assignee: Unassigned
Resolution: Not a Bug Votes: 0
Labels: upstream
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]> 



 Comments   
Comment by Elena Stepanova [ 2015-02-06 ]

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.

Comment by Marco Bakera [ 2015-02-06 ]

Thanks for clarification of this 'strange' behavior.

Generated at Thu Feb 08 07:20:25 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.