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

前端 未结 9 2332
梦谈多话
梦谈多话 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:33

    With generic fields like 'id' and 'name', it's good to put the table name in.

    The reason is it can be confusing when writing joins across multiple tables.

    It's personal preference, really, but that is the reasoning behind it (and I always do it this way).

    Whatever method you choose, make sure it is consistent within the project.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-07 09:38

    For example, your database has tables which store information about Sales and Human resource departments, you could name all your tables related to Sales department as shown below:

    SL_NewLeads SL_Territories SL_TerritoriesManagers

    You could name all your tables related to Human resources department as shown below:

    HR_Candidates HR_PremierInstitutes HR_InterviewSchedules

    This kind of naming convention makes sure, all the related tables are grouped together when you list all your tables in alphabetical order. However, if your database deals with only one logical group of tables, you need not use this naming convention.

    Note that, sometimes you end up vertically partitioning tables into two or more tables, though these partitions effectively represent the same entity. In this case, append a word that best identifies the partition, to the entity name

    0 讨论(0)
提交回复
热议问题