Being that they must be unique, what should I name FK's in a MySQL DB?
By default, MySQL encloses column names and table names in quotation marks. Table names can use any character that is allowed in a file name except for a period or a forward slash. Table names must be 32 characters or less because SAS does not truncate a longer name.
Each Primary Key name should have a “PK_” prefix. The first letter of a table name should be capitalized. The last word of a table name should be end with the character "s" (or "es") to indicate plural. If a table name contains more than one word then the first letter of each word should be capitalized.
Database names must only consist of the letters a to z (both lower and upper case allowed), the numbers 0 to 9 , and the underscore ( _ ) or dash ( - ) symbols. This also means that any non-ASCII database names are not allowed. Database names must always start with a letter.
Foreign Key Constraint Foreign key is a field in the database table that is a primary key in other tables. The naming conventions for a foreign key constraint should have an "FK_" prefix, followed by the target table name, followed by the source table name. The syntax should be "FK_<TargetTable>_<SourceTable>".
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.
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:
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With