MySQL naming conventions, should field name include the table name?

前端 未结 9 2335
梦谈多话
梦谈多话 2021-02-07 08:59

A friend told me that I should include the table name in the field name of the same table, and I\'m wondering why? And should it be like this? Example:

(Table)         


        
9条回答
  •  遇见更好的自我
    2021-02-07 09:34

    Prefixing the column name with the table name is a way of guaranteeing unique column names, which makes joining easier.

    But it is a tiresome practice, especially if when we have long table names. It's generally easier to just use aliases when appropriate. Besides, it doesn't help when we are self-joining.

    As a data modeller I do find it hard to be consistent all the time. With ID columns I theoretically prefer to have just ID but I usually find I have tables with columns called USER_ID, ORDER_ID, etc.

    There are scenarios where it can be positively beneficial to use a common column name across multiple tables. For instance, when a logical super-type/sub-type relationship has been rendered as just the child tables it is useful to retain the super-type's column on all the sub-type tables (e.g. ITEM_STATUS) instead of renaming it for each sub-type (ORDER_ITEM_STATUS, INVOICE_ITEM_STATUS, etc). This is particularly true when they are enums with a common set of values.

提交回复
热议问题