问题描述
我第一次开始使用外国钥匙,我想知道是否有标准的命名计划可以使用?
给定这些表:
task (id, userid, title) note (id, taskid, userid, note); user (id, name)
在任务有注释的地方,任务由用户所有,用户作者注释.
在这种情况下,三个外国钥匙将如何命名?或者, 是否完全重要?
update :这个问题是关于外键名,而不是字段名称!
推荐答案
SQL Server中的标准约定是:
FK_ForeignKeyTable_PrimaryKeyTable
所以,例如,笔记和任务之间的键是:
FK_note_task
,任务和用户之间的密钥是:
FK_task_user
这使您有一个"一眼"的视图,即钥匙中涉及哪些表,因此可以轻松查看哪个表(第一个名为第一个表)依赖于哪个表(第二个名称).在这种情况下,完整的键将是:
FK_task_user FK_note_task FK_note_user
因此,您可以看到任务取决于用户,注释取决于任务和用户.
其他推荐答案
我使用两个下划线字符作为定界符,即
fk__ForeignKeyTable__PrimaryKeyTable
这是因为表名为偶尔包含强度的字符本身.这是遵循约束的命名约定,因为数据元素的名称经常包含下划线字符,例如
CREATE TABLE NaturalPersons ( ... person_death_date DATETIME, person_death_reason VARCHAR(30) CONSTRAINT person_death_reason__not_zero_length CHECK (DATALENGTH(person_death_reason) > 0), CONSTRAINT person_death_date__person_death_reason__interaction CHECK ((person_death_date IS NULL AND person_death_reason IS NULL) OR (person_death_date IS NOT NULL AND person_death_reason IS NOT NULL)) ...
其他推荐答案
FK_TABLENAME_COLUMNNAME?
怎么样k
问题描述
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)
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!
推荐答案
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.
其他推荐答案
I use two underscore characters as delimiter i.e.
fk__ForeignKeyTable__PrimaryKeyTable
This is because table names will occasionally contain underscore characters themselves. This follows the naming convention for constraints generally because data elements' names will frequently contain underscore characters e.g.
CREATE TABLE NaturalPersons ( ... person_death_date DATETIME, person_death_reason VARCHAR(30) CONSTRAINT person_death_reason__not_zero_length CHECK (DATALENGTH(person_death_reason) > 0), CONSTRAINT person_death_date__person_death_reason__interaction CHECK ((person_death_date IS NULL AND person_death_reason IS NULL) OR (person_death_date IS NOT NULL AND person_death_reason IS NOT NULL)) ...
其他推荐答案
How about FK_TABLENAME_COLUMNNAME?
Keep It Simple Stupid whenever possible.