Bug #112007 FOREIGN KEY (reference the hidden part of key) should not have the UK's name.
Submitted: 9 Aug 2023 9:32 Modified: 9 Aug 2023 9:39
Reporter: Shun Yi (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: DDL Severity:S3 (Non-critical)
Version:Any, 5.7, 8.0 OS:Any
Assigned to: CPU Architecture:Any

[9 Aug 2023 9:32] Shun Yi
Description:
Engine considers hidden part of key (columns from primary key which are implicitly added to secondary keys) when determines if it can serve as parent. Example: InnoDB.

If the FOREIGN KEY of the child table which references the hidden part of parent table's UK should not have the same name as the parent table's UK.

Because the number of columns of child table's foreign key is greater than the number of columns of parent table's key.

They are different.

```
typedef struct Table_share_foreign_key_info {
  LEX_CSTRING referenced_table_db;
  LEX_CSTRING referenced_table_name;
  /**
    Name of unique key matching FK in parent table, "" if there is no
    unique key.
  */
  LEX_CSTRING unique_constraint_name;
  dd::Foreign_key::enum_rule update_rule, delete_rule;
  uint columns;
  /**
    Arrays with names of referencing columns of the FK.
  */
  LEX_CSTRING *column_name;
} TABLE_SHARE_FOREIGN_KEY_INFO;
```

How to repeat:
CREATE TABLE t1 (a INT, c INT, UNIQUE KEY(c), PRIMARY KEY (a));

CREATE TABLE t2 (fk1 INT, fk2 INT, FOREIGN KEY (fk1, fk2) REFERENCES t1 (c, a));

SELECT * from information_schema.referential_constraints\G
*************************** 1. row ***************************
       CONSTRAINT_CATALOG: def
        CONSTRAINT_SCHEMA: test
          CONSTRAINT_NAME: t2_ibfk_1
UNIQUE_CONSTRAINT_CATALOG: def
 UNIQUE_CONSTRAINT_SCHEMA: test
   UNIQUE_CONSTRAINT_NAME: c
             MATCH_OPTION: NONE
              UPDATE_RULE: NO ACTION
              DELETE_RULE: NO ACTION
               TABLE_NAME: t2
    REFERENCED_TABLE_NAME: t1
1 row in set (0.01 sec)

The unique_constraint_name of child table's FOREIGN KEY in table_share is 'c'. However, the FOREIGN KEY has two columns.

Suggested fix:
When not all unique key columns are referenced, set unique_constraint_name to "".
[9 Aug 2023 9:39] MySQL Verification Team
Hello Shun Yi,

Thank you for the report and test case.

regards,
Umesh