What is a proper naming convention for MySQL FKs?
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 beuser_id
(and not justid
) 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).
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.
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.