What is a proper naming convention for MySQL FKs?

MysqlNaming ConventionsForeign Keys

Mysql Problem Overview


Being that they must be unique, what should I name FK's in a MySQL DB?

Mysql Solutions


Solution 1 - Mysql

In MySQL, there is no need to give a symbolic name to foreign key constraints. If a name is not given, InnoDB creates a unique name automatically.

In any case, this is the convention that I use:

fk_[referencing table name]_[referenced table name]_[referencing field name]

Example:

CREATE TABLE users(
    user_id    int,
    name       varchar(100)
);

CREATE TABLE messages(
    message_id int,
    user_id    int
);

ALTER TABLE messages ADD CONSTRAINT fk_messages_users_user_id 
    FOREIGN KEY (user_id) REFERENCES users(user_id);

I try to stick with the same field names in referencing and referenced tables, as in user_id in the above example. When this is not practical, I also append the referenced field name to the foreign key name.

This naming convention allows me to "guess" the symbolic name just by looking at the table definitions, and in addition it also guarantees unique names.

Solution 2 - Mysql

my choice is different. in my opinion, a table should have an id field, not a user_id one, because table is just called user, so:

CREATE TABLE users(
   id    int,
   name       varchar(100)
);

CREATE TABLE messages(
   id int,
   user_id    int
);

user_id in messages table is a fk field so it has to make clear which id is (user_id).

a fully-self-explaining naming convention, in my opinion, could be:

fk_[referencing table name]_[referencing field name]_[referenced table name]_[referenced field name]

i.e.: `fk_messages_user_id_users_id`

note:

  • you can, in some case, omit the second element ([referencing field name])

  • this fk could is unique, because if a messages_user table exists, the referencing field name should be user_id (and not just id) and the fk name should be:

    fk_messages_user_user_id_users_id

in other words, a foreign key naming convention make you sure about unique names if you also use a "referencing/referenced field" naming convention (and you can choose your own, of course).

Solution 3 - Mysql

If you don't find yourself referencing fk's that often after they are created, one option is to keep it simple and let MySQL do the naming for you (as Daniel Vassallo mentions in the beginning of his answer).

While you won't be able to uniquely "guess" the constraint names with this method - you can easily find the foreign key constraint name by running a query:

use information_schema;
select TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME from KEY_COLUMN_USAGE where REFERENCED_TABLE_SCHEMA = 'your_db_schema_name' ORDER BY TABLE_NAME;

For example you might receive the following from the query:

+------------+-------------+-----------------+-----------------------+------------------------+
| TABLE_NAME | COLUMN_NAME | CONSTRAINT_NAME | REFERENCED_TABLE_NAME | REFERENCED_COLUMN_NAME |
+------------+-------------+-----------------+-----------------------+------------------------+
| note       | taskid      | note_ibfk_2     | task                  | id                     |
| note       | userid      | note_ibfk_1     | user                  | id                     |
| task       | userid      | task_ibfk_1     | user                  | id                     |
+------------+-------------+-----------------+-----------------------+------------------------+

If this extra step isn't too much for you, then you should be able to easily find the fk you are looking for.

Solution 4 - Mysql

fk-[referencing_table]-[referencing_field]

The reason is the combination of referencing_table and referencing_field is unique in a database. This way make the foreign key name easy to read, for example:

table `user`:
    id
    name
    role

table `article`:
    id
    content
    created_user_id /* --> user.id */
    reviewed_user_id /* --> user.id */

So we have two foreign keys:

fk-article-created_user_id
fk-article-reviewed_user_id

Adding user table name to foreign key name is reduntdant.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZombiesView Question on Stackoverflow
Solution 1 - MysqlDaniel VassalloView Answer on Stackoverflow
Solution 2 - MysqllorenzoView Answer on Stackoverflow
Solution 3 - Mysqluser12345View Answer on Stackoverflow
Solution 4 - MysqlVăn QuyếtView Answer on Stackoverflow