Details
-
Task
-
Status: Closed (View Workflow)
-
Critical
-
Resolution: Fixed
-
None
Description
Add support of referential constraints directly in column defininions:
create table t1 (id1 int primary key); |
create table t2 (id2 int references t1(id1)); |
Referenced field name can be omitted if equal to foreign field name:
create table t1 (id int primary key); |
create table t2 (id int references t1); |
Until 10.5 this syntax was understood by the parser but was silently
ignored.
In case of generated columns this syntax is disabled at parser level
by ER_PARSE_ERROR. Note that separate FOREIGN KEY clause for generated
columns is disabled at storage engine level.
From ISO/IEC 9075-2 (2016):
11.4 <column definition> (p. 842)
<column definition> ::=
|
<column name> [ <data type or domain name> ]
|
[ <default clause> | <identity column specification> | <generation clause>
|
| <system time period start column specification>
|
| <system time period end column specification> ]
|
[ <column constraint definition>... ]
|
...
|
<column constraint definition> ::=
|
[ <constraint name definition> ] <column constraint> [ <constraint characteristics> ]
|
|
<column constraint> ::=
|
NOT NULL
|
| <unique specification>
|
| <references specification>
|
| <check constraint definition>
|
11.8 <referential constraint definition> (p. 857)
<references specification> ::=
|
REFERENCES <referenced table and columns>
|
[ MATCH <match type> ] [ <referential triggered action> ]
|
|
<referenced table and columns> ::=
|
<table name> [ <left paren> <referenced column list>
|
[ <comma> <referenced period specification> ] <right paren> ]
|
Note
Currently this syntax is accepted but silently ignored.
Reproduce
create or replace table t1 (x int primary key); |
create or replace table t2 (x int references t1(x)); |
show create table t2; |
Result
+-------+---------------------------------------------------------------------------------------+
|
| Table | Create Table |
|
+-------+---------------------------------------------------------------------------------------+
|
| t2 | CREATE TABLE `t2` (
|
`x` int(11) DEFAULT NULL
|
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
|
+-------+---------------------------------------------------------------------------------------+
|
Expected
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
| Table | Create Table |
|
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
| t2 | CREATE TABLE `t2` (
|
`x` int(11) DEFAULT NULL,
|
KEY `x` (`x`),
|
CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`x`) REFERENCES `t1` (`x`)
|
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
|
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
Attachments
Issue Links
- duplicates
-
MDEV-6420 REFERENCES syntax in CREATE TABLE
-
- Closed
-
- is blocked by
-
MDEV-20480 Obsolete internal parser for FK in InnoDB
-
- Closed
-
- relates to
-
MDEV-7284 INDEX: CREATE OR REPLACE
-
- Closed
-
-
MDEV-7801 Unexpected syntax error in ALTER TABLE t1 ADD INDEX TYPE BTREE
-
- Open
-
-
MDEV-35916 REFERENCES clause fails to infer multiple column names
-
- Open
-
Activity
Field | Original Value | New Value |
---|---|---|
Link |
This issue is blocked by |
Status | Open [ 1 ] | In Progress [ 3 ] |
Assignee | Aleksey Midenkov [ midenok ] | Alexander Barkov [ bar ] |
Status | In Progress [ 3 ] | In Review [ 10002 ] |
Description |
h3. From ISO/IEC 9075-2 (2016):
h4. 11.4 <column definition> (p. 842) {code} <column definition> ::= <column name> [ <data type or domain name> ] [ <default clause> | <identity column specification> | <generation clause> | <system time period start column specification> | <system time period end column specification> ] [ <column constraint definition>... ] ... <column constraint definition> ::= [ <constraint name definition> ] <column constraint> [ <constraint characteristics> ] <column constraint> ::= NOT NULL | <unique specification> | <references specification> | <check constraint definition> {code} h4. 11.8 <referential constraint definition> (p. 857) {code} <references specification> ::= REFERENCES <referenced table and columns> [ MATCH <match type> ] [ <referential triggered action> ] {code} h4. Note Currently this syntax is accepted but silently ignored. h3. h4. Reproduce {code:sql} create or replace table t1 (x int primary key); create or replace table t2 (x int references t1(x)); show create table t2; {code} h4. Result {code} +-------+---------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+---------------------------------------------------------------------------------------+ {code} h4. Expected {code} +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL, KEY `x` (`x`), CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`x`) REFERENCES `t1` (`x`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ {code} |
As of version 10.5, MariaDB supports referencial constraints as separate table elements:
{code:cql} CREATE OR REPLACE TABLE t1 ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL ) ENGINE = InnoDB; CREATE OR REPLACE TABLE t2 ( id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(200) NOT NULL, author_id SMALLINT UNSIGNED NOT NULL, CONSTRAINT `fk_book_author` FOREIGN KEY (author_id) REFERENCES t1 (id) ) ENGINE = InnoDB; {code} Under terms of this task we'll add support of referential constraints directly in column defininions: {code:sql} CREATE OR REPLACE TABLE t1 ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL ) ENGINE = InnoDB; CREATE OR REPLACE TABLE t2 ( id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(200) NOT NULL, author_id SMALLINT UNSIGNED NOT NULL REFERENCES t1 (id), ) ENGINE = InnoDB; {code} {quote} Note, as of 10.5 this syntax is understood by the parser, but ignored. After this task, this syntax will actually work. {quote} h3. From ISO/IEC 9075-2 (2016): h4. 11.4 <column definition> (p. 842) {code} <column definition> ::= <column name> [ <data type or domain name> ] [ <default clause> | <identity column specification> | <generation clause> | <system time period start column specification> | <system time period end column specification> ] [ <column constraint definition>... ] ... <column constraint definition> ::= [ <constraint name definition> ] <column constraint> [ <constraint characteristics> ] <column constraint> ::= NOT NULL | <unique specification> | <references specification> | <check constraint definition> {code} h4. 11.8 <referential constraint definition> (p. 857) {code} <references specification> ::= REFERENCES <referenced table and columns> [ MATCH <match type> ] [ <referential triggered action> ] {code} h4. Note Currently this syntax is accepted but silently ignored. h3. h4. Reproduce {code:sql} create or replace table t1 (x int primary key); create or replace table t2 (x int references t1(x)); show create table t2; {code} h4. Result {code} +-------+---------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+---------------------------------------------------------------------------------------+ {code} h4. Expected {code} +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL, KEY `x` (`x`), CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`x`) REFERENCES `t1` (`x`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ {code} |
Description |
As of version 10.5, MariaDB supports referencial constraints as separate table elements:
{code:cql} CREATE OR REPLACE TABLE t1 ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL ) ENGINE = InnoDB; CREATE OR REPLACE TABLE t2 ( id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(200) NOT NULL, author_id SMALLINT UNSIGNED NOT NULL, CONSTRAINT `fk_book_author` FOREIGN KEY (author_id) REFERENCES t1 (id) ) ENGINE = InnoDB; {code} Under terms of this task we'll add support of referential constraints directly in column defininions: {code:sql} CREATE OR REPLACE TABLE t1 ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL ) ENGINE = InnoDB; CREATE OR REPLACE TABLE t2 ( id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(200) NOT NULL, author_id SMALLINT UNSIGNED NOT NULL REFERENCES t1 (id), ) ENGINE = InnoDB; {code} {quote} Note, as of 10.5 this syntax is understood by the parser, but ignored. After this task, this syntax will actually work. {quote} h3. From ISO/IEC 9075-2 (2016): h4. 11.4 <column definition> (p. 842) {code} <column definition> ::= <column name> [ <data type or domain name> ] [ <default clause> | <identity column specification> | <generation clause> | <system time period start column specification> | <system time period end column specification> ] [ <column constraint definition>... ] ... <column constraint definition> ::= [ <constraint name definition> ] <column constraint> [ <constraint characteristics> ] <column constraint> ::= NOT NULL | <unique specification> | <references specification> | <check constraint definition> {code} h4. 11.8 <referential constraint definition> (p. 857) {code} <references specification> ::= REFERENCES <referenced table and columns> [ MATCH <match type> ] [ <referential triggered action> ] {code} h4. Note Currently this syntax is accepted but silently ignored. h3. h4. Reproduce {code:sql} create or replace table t1 (x int primary key); create or replace table t2 (x int references t1(x)); show create table t2; {code} h4. Result {code} +-------+---------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+---------------------------------------------------------------------------------------+ {code} h4. Expected {code} +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL, KEY `x` (`x`), CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`x`) REFERENCES `t1` (`x`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ {code} |
As of version 10.5, MariaDB supports referential constraints as separate table elements:
{code:cql} CREATE OR REPLACE TABLE t1 ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL ) ENGINE = InnoDB; CREATE OR REPLACE TABLE t2 ( id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(200) NOT NULL, author_id SMALLINT UNSIGNED NOT NULL, CONSTRAINT `fk_book_author` FOREIGN KEY (author_id) REFERENCES t1 (id) ) ENGINE = InnoDB; {code} Under terms of this task we'll add support of referential constraints directly in column defininions: {code:sql} CREATE OR REPLACE TABLE t1 ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL ) ENGINE = InnoDB; CREATE OR REPLACE TABLE t2 ( id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(200) NOT NULL, author_id SMALLINT UNSIGNED NOT NULL REFERENCES t1 (id), ) ENGINE = InnoDB; {code} {quote} Note, as of 10.5 this syntax is understood by the parser, but ignored. After this task, this syntax will actually work. {quote} h3. From ISO/IEC 9075-2 (2016): h4. 11.4 <column definition> (p. 842) {code} <column definition> ::= <column name> [ <data type or domain name> ] [ <default clause> | <identity column specification> | <generation clause> | <system time period start column specification> | <system time period end column specification> ] [ <column constraint definition>... ] ... <column constraint definition> ::= [ <constraint name definition> ] <column constraint> [ <constraint characteristics> ] <column constraint> ::= NOT NULL | <unique specification> | <references specification> | <check constraint definition> {code} h4. 11.8 <referential constraint definition> (p. 857) {code} <references specification> ::= REFERENCES <referenced table and columns> [ MATCH <match type> ] [ <referential triggered action> ] {code} h4. Note Currently this syntax is accepted but silently ignored. h3. h4. Reproduce {code:sql} create or replace table t1 (x int primary key); create or replace table t2 (x int references t1(x)); show create table t2; {code} h4. Result {code} +-------+---------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+---------------------------------------------------------------------------------------+ {code} h4. Expected {code} +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL, KEY `x` (`x`), CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`x`) REFERENCES `t1` (`x`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ {code} |
Description |
As of version 10.5, MariaDB supports referential constraints as separate table elements:
{code:cql} CREATE OR REPLACE TABLE t1 ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL ) ENGINE = InnoDB; CREATE OR REPLACE TABLE t2 ( id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(200) NOT NULL, author_id SMALLINT UNSIGNED NOT NULL, CONSTRAINT `fk_book_author` FOREIGN KEY (author_id) REFERENCES t1 (id) ) ENGINE = InnoDB; {code} Under terms of this task we'll add support of referential constraints directly in column defininions: {code:sql} CREATE OR REPLACE TABLE t1 ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL ) ENGINE = InnoDB; CREATE OR REPLACE TABLE t2 ( id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(200) NOT NULL, author_id SMALLINT UNSIGNED NOT NULL REFERENCES t1 (id), ) ENGINE = InnoDB; {code} {quote} Note, as of 10.5 this syntax is understood by the parser, but ignored. After this task, this syntax will actually work. {quote} h3. From ISO/IEC 9075-2 (2016): h4. 11.4 <column definition> (p. 842) {code} <column definition> ::= <column name> [ <data type or domain name> ] [ <default clause> | <identity column specification> | <generation clause> | <system time period start column specification> | <system time period end column specification> ] [ <column constraint definition>... ] ... <column constraint definition> ::= [ <constraint name definition> ] <column constraint> [ <constraint characteristics> ] <column constraint> ::= NOT NULL | <unique specification> | <references specification> | <check constraint definition> {code} h4. 11.8 <referential constraint definition> (p. 857) {code} <references specification> ::= REFERENCES <referenced table and columns> [ MATCH <match type> ] [ <referential triggered action> ] {code} h4. Note Currently this syntax is accepted but silently ignored. h3. h4. Reproduce {code:sql} create or replace table t1 (x int primary key); create or replace table t2 (x int references t1(x)); show create table t2; {code} h4. Result {code} +-------+---------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+---------------------------------------------------------------------------------------+ {code} h4. Expected {code} +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL, KEY `x` (`x`), CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`x`) REFERENCES `t1` (`x`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ {code} |
As of version 10.5, MariaDB supports referential constraints as separate table elements:
{code:cql} CREATE OR REPLACE TABLE t1 ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL ) ENGINE = InnoDB; CREATE OR REPLACE TABLE t2 ( id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(200) NOT NULL, author_id SMALLINT UNSIGNED NOT NULL, CONSTRAINT `fk_book_author` FOREIGN KEY (author_id) REFERENCES t1 (id) ) ENGINE = InnoDB; {code} Under terms of this task we'll add support of referential constraints directly in column defininions: {code:sql} CREATE OR REPLACE TABLE t1 ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL ) ENGINE = InnoDB; CREATE OR REPLACE TABLE t2 ( id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(200) NOT NULL, author_id SMALLINT UNSIGNED NOT NULL REFERENCES t1 (id), ) ENGINE = InnoDB; {code} {quote} Note, as of 10.5 this syntax is understood by the parser, but is silently ignored. After this task, this syntax will actually work. {quote} h3. From ISO/IEC 9075-2 (2016): h4. 11.4 <column definition> (p. 842) {code} <column definition> ::= <column name> [ <data type or domain name> ] [ <default clause> | <identity column specification> | <generation clause> | <system time period start column specification> | <system time period end column specification> ] [ <column constraint definition>... ] ... <column constraint definition> ::= [ <constraint name definition> ] <column constraint> [ <constraint characteristics> ] <column constraint> ::= NOT NULL | <unique specification> | <references specification> | <check constraint definition> {code} h4. 11.8 <referential constraint definition> (p. 857) {code} <references specification> ::= REFERENCES <referenced table and columns> [ MATCH <match type> ] [ <referential triggered action> ] {code} h4. Note Currently this syntax is accepted but silently ignored. h3. h4. Reproduce {code:sql} create or replace table t1 (x int primary key); create or replace table t2 (x int references t1(x)); show create table t2; {code} h4. Result {code} +-------+---------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+---------------------------------------------------------------------------------------+ {code} h4. Expected {code} +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL, KEY `x` (`x`), CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`x`) REFERENCES `t1` (`x`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ {code} |
Assignee | Alexander Barkov [ bar ] | Aleksey Midenkov [ midenok ] |
Status | In Review [ 10002 ] | Stalled [ 10000 ] |
Description |
As of version 10.5, MariaDB supports referential constraints as separate table elements:
{code:cql} CREATE OR REPLACE TABLE t1 ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL ) ENGINE = InnoDB; CREATE OR REPLACE TABLE t2 ( id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(200) NOT NULL, author_id SMALLINT UNSIGNED NOT NULL, CONSTRAINT `fk_book_author` FOREIGN KEY (author_id) REFERENCES t1 (id) ) ENGINE = InnoDB; {code} Under terms of this task we'll add support of referential constraints directly in column defininions: {code:sql} CREATE OR REPLACE TABLE t1 ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL ) ENGINE = InnoDB; CREATE OR REPLACE TABLE t2 ( id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(200) NOT NULL, author_id SMALLINT UNSIGNED NOT NULL REFERENCES t1 (id), ) ENGINE = InnoDB; {code} {quote} Note, as of 10.5 this syntax is understood by the parser, but is silently ignored. After this task, this syntax will actually work. {quote} h3. From ISO/IEC 9075-2 (2016): h4. 11.4 <column definition> (p. 842) {code} <column definition> ::= <column name> [ <data type or domain name> ] [ <default clause> | <identity column specification> | <generation clause> | <system time period start column specification> | <system time period end column specification> ] [ <column constraint definition>... ] ... <column constraint definition> ::= [ <constraint name definition> ] <column constraint> [ <constraint characteristics> ] <column constraint> ::= NOT NULL | <unique specification> | <references specification> | <check constraint definition> {code} h4. 11.8 <referential constraint definition> (p. 857) {code} <references specification> ::= REFERENCES <referenced table and columns> [ MATCH <match type> ] [ <referential triggered action> ] {code} h4. Note Currently this syntax is accepted but silently ignored. h3. h4. Reproduce {code:sql} create or replace table t1 (x int primary key); create or replace table t2 (x int references t1(x)); show create table t2; {code} h4. Result {code} +-------+---------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+---------------------------------------------------------------------------------------+ {code} h4. Expected {code} +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL, KEY `x` (`x`), CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`x`) REFERENCES `t1` (`x`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ {code} |
Add support of referential constraints directly in column defininions:
{code:sql} create table t1 (id1 int primary key); create table t2 (id2 int references t1(id1)); {code} Referenced field name can be omitted if equal to foreign field name: {code:sql} create table t1 (id int primary key); create table t2 (id int references t1); {code} Until 10.5 this syntax was understood by the parser but was silently ignored. In case of generated columns this syntax is disabled at parser level by ER_PARSE_ERROR. Note that separate FOREIGN KEY clause for generated columns is disabled at storage engine level. h3. From ISO/IEC 9075-2 (2016): h4. 11.4 <column definition> (p. 842) {code} <column definition> ::= <column name> [ <data type or domain name> ] [ <default clause> | <identity column specification> | <generation clause> | <system time period start column specification> | <system time period end column specification> ] [ <column constraint definition>... ] ... <column constraint definition> ::= [ <constraint name definition> ] <column constraint> [ <constraint characteristics> ] <column constraint> ::= NOT NULL | <unique specification> | <references specification> | <check constraint definition> {code} h4. 11.8 <referential constraint definition> (p. 857) {code} <references specification> ::= REFERENCES <referenced table and columns> [ MATCH <match type> ] [ <referential triggered action> ] {code} h4. Note Currently this syntax is accepted but silently ignored. h3. h4. Reproduce {code:sql} create or replace table t1 (x int primary key); create or replace table t2 (x int references t1(x)); show create table t2; {code} h4. Result {code} +-------+---------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+---------------------------------------------------------------------------------------+ {code} h4. Expected {code} +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL, KEY `x` (`x`), CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`x`) REFERENCES `t1` (`x`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ {code} |
Description |
Add support of referential constraints directly in column defininions:
{code:sql} create table t1 (id1 int primary key); create table t2 (id2 int references t1(id1)); {code} Referenced field name can be omitted if equal to foreign field name: {code:sql} create table t1 (id int primary key); create table t2 (id int references t1); {code} Until 10.5 this syntax was understood by the parser but was silently ignored. In case of generated columns this syntax is disabled at parser level by ER_PARSE_ERROR. Note that separate FOREIGN KEY clause for generated columns is disabled at storage engine level. h3. From ISO/IEC 9075-2 (2016): h4. 11.4 <column definition> (p. 842) {code} <column definition> ::= <column name> [ <data type or domain name> ] [ <default clause> | <identity column specification> | <generation clause> | <system time period start column specification> | <system time period end column specification> ] [ <column constraint definition>... ] ... <column constraint definition> ::= [ <constraint name definition> ] <column constraint> [ <constraint characteristics> ] <column constraint> ::= NOT NULL | <unique specification> | <references specification> | <check constraint definition> {code} h4. 11.8 <referential constraint definition> (p. 857) {code} <references specification> ::= REFERENCES <referenced table and columns> [ MATCH <match type> ] [ <referential triggered action> ] {code} h4. Note Currently this syntax is accepted but silently ignored. h3. h4. Reproduce {code:sql} create or replace table t1 (x int primary key); create or replace table t2 (x int references t1(x)); show create table t2; {code} h4. Result {code} +-------+---------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+---------------------------------------------------------------------------------------+ {code} h4. Expected {code} +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL, KEY `x` (`x`), CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`x`) REFERENCES `t1` (`x`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ {code} |
Add support of referential constraints directly in column defininions:
{code:sql} create table t1 (id1 int primary key); create table t2 (id2 int references t1(id1)); {code} Referenced field name can be omitted if equal to foreign field name: {code:sql} create table t1 (id int primary key); create table t2 (id int references t1); {code} Until 10.5 this syntax was understood by the parser but was silently ignored. In case of generated columns this syntax is disabled at parser level by ER_PARSE_ERROR. Note that separate FOREIGN KEY clause for generated columns is disabled at storage engine level. h3. From ISO/IEC 9075-2 (2016): h4. 11.4 <column definition> (p. 842) {code} <column definition> ::= <column name> [ <data type or domain name> ] [ <default clause> | <identity column specification> | <generation clause> | <system time period start column specification> | <system time period end column specification> ] [ <column constraint definition>... ] ... <column constraint definition> ::= [ <constraint name definition> ] <column constraint> [ <constraint characteristics> ] <column constraint> ::= NOT NULL | <unique specification> | <references specification> | <check constraint definition> {code} h4. 11.8 <referential constraint definition> (p. 857) {code} <references specification> ::= REFERENCES <referenced table and columns> [ MATCH <match type> ] [ <referential triggered action> ] <referenced table and columns> ::= <table name> [ <left paren> <referenced column list> [ <comma> <referenced period specification> ] <right paren> ] {code} h4. Note Currently this syntax is accepted but silently ignored. h3. h4. Reproduce {code:sql} create or replace table t1 (x int primary key); create or replace table t2 (x int references t1(x)); show create table t2; {code} h4. Result {code} +-------+---------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+---------------------------------------------------------------------------------------+ {code} h4. Expected {code} +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL, KEY `x` (`x`), CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`x`) REFERENCES `t1` (`x`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ {code} |
Fix Version/s | 10.5.0 [ 23709 ] | |
Fix Version/s | 10.5 [ 23123 ] | |
Resolution | Fixed [ 1 ] | |
Status | Stalled [ 10000 ] | Closed [ 6 ] |
Workflow | MariaDB v3 [ 100142 ] | MariaDB v4 [ 134106 ] |
Link | This issue relates to MDEV-35916 [ MDEV-35916 ] |
I guess that the documentation hasn't been updated yet in order to reflect the change: https://mariadb.com/kb/en/create-table/#column-definitions