[MDEV-26690] Confusing highlighted block on the page describing FK in KB Created: 2021-09-26  Updated: 2021-11-15  Resolved: 2021-11-15

Status: Closed
Project: MariaDB Server
Component/s: Documentation
Affects Version/s: N/A
Fix Version/s: N/A

Type: Bug Priority: Minor
Reporter: Elena Stepanova Assignee: Ian Gilfillan
Resolution: Fixed Votes: 0
Labels: None


 Description   

The KB page about foreign keys https://mariadb.com/kb/en/foreign-keys/ has this text block at the beginning of Syntax section:

Note: MariaDB accepts the REFERENCES clause in ALTER TABLE and CREATE TABLE statements, but that syntax does nothing. MariaDB simply parses it without returning any error or warning, for compatibility with other DBMS's. However, only the syntax described below creates foreign keys.

Maybe it is severely outdated or unfortunately phrased, I can't figure out from the text or from the history what it was supposed to mean at the time it was added. Either way it's confusing and needs to be updated, as clearly the REFERENCES clause is an essential part of FK definition and it's not ignored.



 Comments   
Comment by Ian Gilfillan [ 2021-10-18 ]

I think it means that until 10.5.0, REFERENCES in the following case would be accepted, but do nothing:

CREATE TABLE a(a_key INT primary key, not_key INT);
 
CREATE TABLE b(for_key INT REFERENCES a(not_key));
 
SHOW CREATE TABLE b;
+-------+--------------------------------------------------------------------------------------------+
| Table | Create Table                                                                               |
+-------+--------------------------------------------------------------------------------------------+
| b     | CREATE TABLE `b` (
  `for_key` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------+
 
INSERT INTO a VALUES (1,10);
 
INSERT INTO b VALUES (10);
 
INSERT INTO b VALUES (1);

From 10.5.0 however (which doesn't seem to have been documented anywhere):

CREATE TABLE a(a_key INT primary key, not_key INT);
 
CREATE TABLE b(for_key INT REFERENCES a(not_key));
ERROR 1005 (HY000): Can't create table `test`.`b` (errno: 150 "Foreign key constraint is incorrectly formed")
Warning (Code 150): Create  table `test`.`b` with foreign key (for_key) constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns.
Error (Code 1005): Can't create table `test`.`b` (errno: 150 "Foreign key constraint is incorrectly formed")
Warning (Code 1215): Cannot add foreign key constraint for `b`
 
CREATE TABLE c(for_key INT REFERENCES a(a_key));
 
 
INSERT INTO c VALUES (22);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`c`, CONSTRAINT `c_ibfk_1` FOREIGN KEY (`for_key`) REFERENCES `a` (`a_key`))
INSERT INTO c VALUES (1);
 
 
INSERT INTO c VALUES (2);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`c`, CONSTRAINT `c_ibfk_1` FOREIGN KEY (`for_key`) REFERENCES `a` (`a_key`))
 
SHOW CREATE TABLE c;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                      |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| c     | CREATE TABLE `c` (
  `for_key` int(11) DEFAULT NULL,
  KEY `for_key` (`for_key`),
  CONSTRAINT `c_ibfk_1` FOREIGN KEY (`for_key`) REFERENCES `a` (`a_key`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 
INSERT INTO a VALUES (1,10);
 
INSERT INTO c VALUES (10);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`c`, CONSTRAINT `c_ibfk_1` FOREIGN KEY (`for_key`) REFERENCES `a` (`a_key`))
 
INSERT INTO c VALUES (1);
Query OK, 1 row affected (0.002 sec)

Will the above examples be clear enough to clarify the highlighted text? The 10.5 change slipped through without notice, and needs to be documented elsewhere of course.

Generated at Thu Feb 08 09:47:13 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.