外键命名方案 [英] Foreign Key naming scheme

查看:1304
本文介绍了外键命名方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用外键是第一次,我想知道是否有一个标准的命名方案为他们使用。

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:

task (id, userid, title)
note (id, taskid, userid, note);
user (id, name)

任务有Notes,任务由用户所有,

Where Tasks have Notes, Tasks are owned by Users, and Users author Notes.

在这种情况下如何命名三个外键?

How would the three foreign keys be named in this situation? Or alternatively, does it even matter at all?

更新:这个问题是关于外键名称,不是字段名!

Update: This question is about foreign key names, not field names!

推荐答案

SQL Server中的标准约定是:

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.

这篇关于外键命名方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆