Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The short answer is no - don't put "FK" in column names of foreign key columns. You can still signal the intent of the column though, here's how I do it:</p> <h2>Naming foreign key columns</h2> <p>It depends on your naming convention for the target of the FK. If you have <code>Id</code>, then I'd prepend the table name when creating FK columns.</p> <p><strong>Example 1:</strong><br> For table <code>User</code> with PK <code>Id</code> and table <code>Workitem</code> with user ID FK, I'd call the column <code>Workitem.UserId</code>.</p> <p>If there were more than one FK between the same tables, I'd make this clear in the column name:</p> <p><strong>Example 2:</strong><br> For table <code>User</code> with PK <code>Id</code> and table <code>Workitem</code> with "assigned to user ID" and "created by user ID" FKs, I'd call the columns <code>Workitem.CreatedByUserId</code> and <code>Workitem.AssignedToUserId</code>.</p> <p>If your naming convention for PKs is more like <code>UserId</code>, then you'd factor that into the above examples so as not to end up with <code>UserUserId</code>.</p> <h2>Naming foreign key constraints</h2> <p>This is mine:</p> <pre><code>FK_childtablename_[differentiator]parenttablename </code></pre> <p>The differentiator is used when there is more than one FK between the same two tables (e.g. <code>CreatedByUserId</code> and <code>AssignedToUserId</code>). Often I use the child table's column name for this.</p> <p><strong>Example 1:</strong><br> Given tables: <code>Workitem</code> and <code>User</code> Where <code>User</code> has <code>CreatedByUserId</code> and <code>AssignedToUserId</code> Foreign key names are <code>FK_Workitem_User_CreatedByUser</code> and <code>FK_Workitem_AssignedToUser</code></p> <p>I use double-underscores if tables/columns have underscores in the name:</p> <p><strong>Example 2:</strong><br> Given tables: <code>work_item</code> and <code>user</code> Where <code>user</code> has <code>created_by_user_id</code> and <code>assigned_to_user_id</code> Foreign key names are <code>FK_work_item__created_by_user</code> and <code>FK_work_item__assigned_to_user</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload