Foreign Key naming scheme

前端 未结 10 1784
悲&欢浪女
悲&欢浪女 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条回答
  •  -上瘾入骨i
    2020-12-12 11:39

    The standard convention in SQL Server is:

    FK_ForeignKeyTable_PrimaryKeyTable
    

    So, for example, the key between notes and tasks would be:

    FK_note_task
    

    And the key between tasks and users would be:

    FK_task_user
    

    This gives you an 'at a glance' view of which tables are involved in the key, so it makes it easy to see which tables a particular one (the first one named) depends on (the second one named). In this scenario the complete set of keys would be:

    FK_task_user
    FK_note_task
    FK_note_user
    

    So you can see that tasks depend on users, and notes depend on both tasks and users.

提交回复
热议问题