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

后端 未结 14 2024
-上瘾入骨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:13

    I'd recommend sticking with table aliases, like:

    SELECT 
        user.id,
        user.email, 
        user.firstname, 
        user.secondname,
        avatar.filename
    FROM
        pain_in_the_butt_table_name user
    LEFT JOIN
        table_with_the_avatars avatar
    ON avatar.user_id = user.id
    

    The advantages:

    • maintaining an easily comprehendable list of fields you select, and which tables you take them from

    • avoid typing long or hard to comprehend table names and replace them with a short and understandable name (which you should have done when creating the table)

    • perform joins that are readable (your example would read:

    LEFT JOIN table_with_the_avatars.user_id ON user.user_id = table_with_the_avatars.avatars_user_i

    • create even shorter aliases, in my example that would mean u instead of user, and a instead of avatar, shortening your query

提交回复
热议问题