Naming of ID columns in database tables

后端 未结 23 730

I was wondering peoples opinions on the naming of ID columns in database tables.

If I have a table called Invoices with a primary key of an identity column I would c

23条回答
  •  自闭症患者
    2020-11-29 18:17

    What I do to keep things consistent for myself (where a table has a single column primary key used as the ID) is to name the primary key of the table Table_pk. Anywhere I have a foreign key pointing to that tables primary key, I call the column PrimaryKeyTable_fk. That way I know that if I have a Customer_pk in my Customer table and a Customer_fk in my Order table, I know that the Order table is referring to an entry in the Customer table.

    To me, this makes sense especially for joins where I think it reads easier.

    SELECT * 
    FROM Customer AS c
        INNER JOIN Order AS c ON c.Customer_pk = o.Customer_fk
    

提交回复
热议问题