Sql naming best practice

前端 未结 12 2735
春和景丽
春和景丽 2021-02-14 09:48

I\'m not entirely sure if there\'s a standard in the industry or otherwise, so I\'m asking here.

I\'m naming a Users table, and I\'m not entire

相关标签:
12条回答
  • 2021-02-14 10:22

    For the table names, I usually use pluralized nouns (or noun phrases), like you.

    For column names I'd not use the table name as prefix. The table itself specifies the context of the column.

    0 讨论(0)
  • 2021-02-14 10:24

    Just go with name and age, the table should provide the necessary context when you're wondering what kind of name you're working with.

    0 讨论(0)
  • 2021-02-14 10:26

    First of all, I would suggest using the singular noun, i.e. user instead of users, although this is more of a personal preference.

    Second, there are some who prefer to always name the primary key column id, instead of user_id (i.e. table name + id), and similar with for example name instead of employee_name. I think this is a bad idea for the following reason:

    -- when every table has an "id" (or "name") column, you get duplicate column names in the output:
    select e.id, e.name, d.id, d.name
    from employee e, department d
    where e.department_id = d.id
    
    -- to avoid this, you need to specify column aliases every time you query:
    select e.id employee_id, e.name employee_name, d.id department_id, d.name department_name
    from employee e, department d
    where e.department_id = d.id
    
    -- if the column name includes the table, there are no conflicts, and the join condition is very clear
    select e.employee_id, e.employee_name, d.department_id, d.department_name
    from employee e, department d
    where e.department_id = d.department_id
    

    I'm not saying you should include the table name in every column in the table, but do it for the key (id) column and other "generic" columns such as name, description, remarks, etc. that are likely to be included in queries.

    0 讨论(0)
  • 2021-02-14 10:31

    I think table and column names must be like that.

    Table Name :
    User --> Capitalize Each Word and not plural.

    Column Names :
    Id --> If i see "Id" I understand this is PK column.
    GroupId --> I understanding there is an table which named Group and this column is relation column for Group table.
    Name --> If there is a column which named "Name" in User table, this means name of user. It's enaughly clear.

    Especially if you are using Entity Framework I suppose this more.

    Note: Sorry for my bad English. If somebody will correct my bad English i will be happy.

    0 讨论(0)
  • 2021-02-14 10:34

    I explicitly named my columns using a prefix that was related to the table

    i.e. table = USERS, column name = user_id, user_name, user_address_street, etc.
    

    before that, when i started using JOINS I had to alias the crap out of the column names to avoid conflict in the query results, and then when accessed from templates in a MVC View, if the query result field name didn't match the published db schema, the template designers would get all confused and have to ask for the SQL VIEW to determine the correct field name to use.

    So it looks messy to use a prefix in a column name, but in practice it works better for us.

    0 讨论(0)
  • 2021-02-14 10:34

    I'm not entirely sure if there's a standard in the industry

    Yes: ISO 11179-5: Naming and identification principles, available here.

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