数据库设计列命名约定;主键和外键

时间:2012-01-26 22:18:31

标签: database-design

作为一种常见的惯例,我问,因为我并不完全确定;

Normaly在列命名中; id_ *引用pk和* id到fk的? 那么多个id *意味着id_ *会形成一个多列pk?

1 个答案:

答案 0 :(得分:1)

一个名为FOO_BAR_ID的字段会向我建议某种桥接或映射表,而不是与复合主键的关系,这是我认为你建议的,下面是我倾向于如何命名的演示字段,我个人避免使用复合主键,因为它们打破了第二范式而且我从未见过使用它们的逻辑理性

  + a normal table
-----------------------------------------------------
| DEPARTMENT_SID | NAME | DESCRIPTION | ADDRESS_SID |
-----------------------------------------------------
  ^ pk                                  ^ fk

  + another normal table
--------------------------------------------------
| ADDRESS_SID | NUMBER | STREET | TOWN | POSTCODE |
--------------------------------------------------
  ^ pk

  + yet another normal table
-----------------------------------------------------------------------
| EMPLOYEE_SID | FIRST_NAME | LAST_NAME | DATE_OF_BIRTH | ADDRESS_SID |
-----------------------------------------------------------------------
  ^ pk                                                    ^ fk

  + bridge table as an employee can belong to many departments
  + uses a composite key rather then defining a single primary key
---------------------------------
! EMPLOYEE_SID ! DEPARTMENT_SID |  
---------------------------------
  ^ cpk / fk     ^ cpk / fk

  + bridge table defining a single primary key
----------------------------------------------------------- 
| EMPLOYEE_DEPARTMENT_SID ! EMPLOYEE_SID | DEPARTMENT_SID |
-----------------------------------------------------------
  ^ pk                      ^ fk           ^ fk

  + a table with a foreign key to the bridge table
------------------------------------------------------
! SHIFT_SID | EMPLOYEE_DEPARTMENT_SID | HOURS_WORKED |
------------------------------------------------------
  ^ pk        ^ fk                 

pk = PRIMARY KEY, fk = FOREIGN KEY, cpk = COMPOSITE PRIMARY KEY