Foreign Key naming scheme

前端 未结 10 1786
悲&欢浪女
悲&欢浪女 2020-12-12 10:55

I\'m just getting started working with foreign keys for the first time and I\'m wondering if there\'s a standard naming scheme to use for them?

Given these tables:

10条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-12 11:29

    My usual approach is

    FK_ColumnNameOfForeignKey_TableNameOfReference_ColumnNameOfReference
    

    Or in other terms

    FK_ChildColumnName_ParentTableName_ParentColumnName
    

    This way I can name two foreign keys that reference the same table like a history_info table with column actionBy and actionTo from users_info table

    It will be like

    FK_actionBy_usersInfo_name - For actionBy
    FK_actionTo_usersInfo_name - For actionTo
    

    Note that:

    I didn't include the child table name because it seems common sense to me, I am in the table of the child so I can easily assume the child's table name. The total character of it is 26 and fits well to the 30 character limit of oracle which was stated by Charles Burns on a comment here

    Note for readers: Many of the best practices listed below do not work in Oracle because of its 30 character name limit. A table name or column name may already be close to 30 characters, so a convention combining the two into a single name requires a truncation standard or other tricks. – Charles Burns

提交回复
热议问题