MySQL naming conventions, should field name includ

2019-03-18 00:37发布

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) Users  
(Fields) user_id, username, password, last_login_time

I see that the prefix 'user_' is meaningless since I know it's already for a user. But I'd like to hear from you too. note: I'm programming in php, mysql.

9条回答
来,给爷笑一个
2楼-- · 2019-03-18 01:24

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

查看更多
甜甜的少女心
3楼-- · 2019-03-18 01:25

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.

查看更多
Juvenile、少年°
4楼-- · 2019-03-18 01:25

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);
查看更多
登录 后发表回答