简体   繁体   中英

How does MySQL foreign key work?(Innodb)

I am using MySQL foreign key in Innodb, I wonder how Mysql enforce foreign key constraint when we insert into the child table. Innodb seems to build index on the foreign key column automatically, How is this feature useful in enforcing the foreign key constraint? In sum, for normal index, we can create index file, which use the B+ tree structure. What structure is used for a foreign key?

It's an ordinary INDEX . And, in particular, a BTree.

SHOW CREATE TABLE will demonstrate that it is such (and has a fabricated name for the index).

Q: How is this feature [the index on the foreign key column(s) in the child table] useful in enforcing the foreign key constraint?

A: It's useful when an attempt is made to delete a row in the parent table. Or when an attempt is made to update a value in a column that is referenced by a foreign key.

For example, consider an attempt to delete a row in the parent table. The foreign key is a constraint that says: if there are any rows in child table that reference the row in parent table, the delete operation will not succeed and will return an error. (We're assuming for the sake of this example that the foreign key constraint is declared as ON DELETE RESTRICT .)

When a row is deleted from the parent, the child table needs to be checked. Think of performing that check in terms of running a query on the child table, to find out: are there any rows in the child table that reference the row in the parent table. (That is, are they are any rows that have a particular value in the foreign key column?)

If the database wasn't enforcing the constraint, and we were doing the check in the application instead, we would need to run a query something like this:

  SELECT 1
    FROM child_table
   WHERE foreign_key_col = :referenced_key_value
   LIMIT 1

And that query would benefit from a suitable index, and index with foreign_key_col as the leading column. Using an index, MySQL can quickly eliminate vast swaths of rows that it knows can't have that value, narrowing in very quickly to the block(s) that would contain a matching row.

For non-trivial sets, using an appropriate index to locate a row is much more efficient than performing a full scan operation, examining every row in the table, to verify there are no rows that match.

In this case, the ideal index ... ON child_table (foreign_key_column) , ...)`

In addition to that performance benefit, the database can also use the index to prevent other sessions from inserting a row into child_table (rows that would violate the foreign key constraint), using a lock mechanism. Without an index, the database would need to lock the entire child_table. And that would kill concurrency.

(This is an overly simplified explanation. The actual mechanics are more involved. But this should explain why an index on foreign key columns is "useful". To define a foreign key constraint, InnoDB requires that a suitable index be defined. And if one doesn't exist, InnoDB will create one.


Q: What structure is used for a foreign key?

The same structure used for any other column in the table, or any other index on the table.

A FOREIGN KEY is a constraint . There's nothing "special" about a column used in a foreign key constraint. There is a requirement that the column(s) used in a foreign key constraint must be the leading columns in an index. As explained in the answer to your first question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM