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

Foreign key doesn't always require index on referenced columns

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Not a Bug
    • Affects Version/s: 5.5.33a
    • Fix Version/s: None
    • Component/s: None
    • Labels:

      Description

      I discovered a situation where a two-column foreign key constraint doesn't seem to require an index on the referenced columns in the referenced table. I am attaching a repro case that illustrates the scenario, but it is rather complex.

      Basically, there are three tables:

      • Table "A" has one column "a" which is a primary key
      • Table "B" has two columns, "a" which is a foreign key to table "A", and "b" which is a primary key
      • Table "C" has two columns, "a" and "b", which together are a foreign key to the columns in table "B"

      Based on whether table B's foreign key to A is declared inline or separately, table C cannot/can be created.

        Gliffy Diagrams

          Attachments

            Activity

            Hide
            elenst Elena Stepanova added a comment - - edited

            Hi,
            Sorry for the delay.

            What you observe is actually a documented behavior, although not quite obvious. Here is what happens here:

            • when you create table b1, you define FOREIGN KEY on b1.a_id. Since the column doesn't have an index, it's created automatically, as described here:
              http://dev.mysql.com/doc/refman/5.5/en/create-table-foreign-keys.html
              <quote>
              In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist.
              </quote>

            If you run SHOW CREATE TABLE b1, you'll see the index on the column.

            So, when you later create c1, it works all right since there is an index.

            • when you create b2, you define the constraint "inline". It does not work at all, as described here:
              http://dev.mysql.com/doc/refman/5.5/en/create-table.html
              <quote>
              MySQL does not recognize or support “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.
              </quote>

            If you run SHOW CREATE TABLE b2, you'll see there is no FK constraint on the table.
            Since the clause is ignored, the index is not created automatically, as it happened with b1, so when you later create c2, it complains and refuses to work.

            One thing that might be considered a documentation bug here is the vague wording in the second quote. It says that the inline references are not "recognized" or "accepted", while in fact they don't cause a syntax error, but are just ignored. If you wish, you could suggest a better wording to MySQL documentation team at bugs.mysql.com.

            Please comment if you have further concerns on the topic.

            Show
            elenst Elena Stepanova added a comment - - edited Hi, Sorry for the delay. What you observe is actually a documented behavior, although not quite obvious. Here is what happens here: when you create table b1, you define FOREIGN KEY on b1.a_id. Since the column doesn't have an index, it's created automatically, as described here: http://dev.mysql.com/doc/refman/5.5/en/create-table-foreign-keys.html <quote> In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. </quote> If you run SHOW CREATE TABLE b1, you'll see the index on the column. So, when you later create c1, it works all right since there is an index. when you create b2, you define the constraint "inline". It does not work at all, as described here: http://dev.mysql.com/doc/refman/5.5/en/create-table.html <quote> MySQL does not recognize or support “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. </quote> If you run SHOW CREATE TABLE b2, you'll see there is no FK constraint on the table. Since the clause is ignored, the index is not created automatically, as it happened with b1, so when you later create c2, it complains and refuses to work. One thing that might be considered a documentation bug here is the vague wording in the second quote. It says that the inline references are not "recognized" or "accepted", while in fact they don't cause a syntax error, but are just ignored. If you wish, you could suggest a better wording to MySQL documentation team at bugs.mysql.com. Please comment if you have further concerns on the topic.

              People

              • Assignee:
                elenst Elena Stepanova
                Reporter:
                pinkston3 Donnie Pinkston
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: