Is prefixing each field name in a table with abbreviated table name a good practice?

后端 未结 14 1965
-上瘾入骨i
-上瘾入骨i 2020-12-16 19:26

Do you prefix each field in a table with abbreviated table name?

Example:

Table: User

Fields:
user_id
user_name
user_password

Or d

相关标签:
14条回答
  • 2020-12-16 20:18

    if you do it you will end up writing queries like:

    SELECT user.user_name, user.user_password, user.user_firstname ...
    

    instead of

    SELECT user.name, user.password, user.firstname
    

    so IMO the answer to your question is quite clear.

    0 讨论(0)
  • 2020-12-16 20:19

    We also don't use abbreviated table prefixes normally and I wouldn't advice it either.

    There's however one situation where we do: reserve fields.

     e.g. OH_Reserve_Field_Alpha3 in table ORDER_HEADER
    

    Short background: Our database has 250+ tables and we put in most of them reserve columns to use them for future feature implementations. As you can imagine, without prefixing you would end up having 50 Reserve_Field_Alpha3's with totally different meaning but same name throughout your code. It's already hard as it's now, but without prefixes it would be worse.

    0 讨论(0)
  • 2020-12-16 20:19

    For all the reasons given, I don't think this is a good idea. Besides, you don't prefix all the methods in your classes with the class names, do you? So why do it for database objects?

    0 讨论(0)
  • 2020-12-16 20:20

    Personally, on the 'user' table, my column would just be 'id'.

    But any foriegn key columns on different tables pointing to that column, I'd call the column 'user_id'.

    so you might end up with something like this :

    select  *
    from    order
            inner join user
                on user.id=order.user_id
    
    0 讨论(0)
  • 2020-12-16 20:20

    It's an awesome practise:

    1. You see what every field means, at least at what domain it is. You can't figure out what amount means (transactions, incomes) — unless they are xac_amount and inc_amount. Most query tools do not output alias along with the field name.
    2. You may use tables aliases, for sure. But SQL does not require them, and by Murphy's law, if it's not required, it won't be used. There is no standard, so one developer will use x as an alias for transaction, another one will use tran and so on.

    In fact, prefixes are just forced tables aliases, so you can easily see what field belongs to what table.

    0 讨论(0)
  • 2020-12-16 20:21

    When I add the field "ordinal" to a table I like to add in a prefix so I don't have to alias ordinal fields from other tables in JOINS. It's handy for JOINS sometimes... not sure I have seen other benefits.

    MediaWiki (the Wikipiedia software) uses that convention. Download the source. They limit themselves to a two character prefix.

    I don't recommend the practice though. For most databases its not necessary.

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