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

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

    We should define primary keys with prefix of tablename.

    We should use use_id instead if id and post_id instead of just id.

    Benefits:-

    1) Easily Readable

    2) Easily differentiate in join queries. We can minimize the use of alias in query.

    user table : user_id(PK)

    post table : post_id(PK) user_id(FK) here user table PK and post table FK are same

    As per documentation,

    3) This way we can get benefit of NATURAL JOIN and JOIN with USING

    Natural joins and joins with USING, including outer join variants, are processed according to the SQL:2003 standard. The goal was to align the syntax and semantics of MySQL with respect to NATURAL JOIN and JOIN ... USING according to SQL:2003. However, these changes in join processing can result in different output columns for some joins. Also, some queries that appeared to work correctly in older versions (prior to 5.0.12) must be rewritten to comply with the standard.

    These changes have five main aspects:

    1) The way that MySQL determines the result columns of NATURAL or USING join operations (and thus the result of the entire FROM clause).

    2) Expansion of SELECT * and SELECT tbl_name.* into a list of selected columns.

    3) Resolution of column names in NATURAL or USING joins.

    4) Transformation of NATURAL or USING joins into JOIN ... ON.

    5) Resolution of column names in the ON condition of a JOIN ... ON.

    Examples:-

    SELECT * FROM user NATURAL LEFT JOIN post;
    SELECT * FROM user NATURAL JOIN post;
    SELECT * FROM user JOIN post USING (user_id);
    

提交回复
热议问题