What is a proper naming convention for MySQL FKs?

前端 未结 4 2072
醉酒成梦
醉酒成梦 2020-12-12 11:52

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

4条回答
  •  青春惊慌失措
    2020-12-12 12:15

    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.

提交回复
热议问题