views:

2216

answers:

6

Hi, A friend told me that I should include the table name in the field name of the same table, I wondering why? And should it be like this? Example:

(Table) Users  
(Fields) user_id, username, passowrd, last_login_time

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

+2  A: 

With generic fields like 'id' and 'name', it's good to put the table name in.

The reason is it can be confusing when writing joins across multiple tables.

It's personal preference, really, but that is the reasoning behind it (and I always do it this way).

Whatever method you choose, make sure it is consistent within the project.

Noon Silk
Thank you, I'll keep in my mind consistency.
Omar Dolaimy
+4  A: 

I agree with you. The only place I am tempted to put the table name or a shortened form of it is on primary and foreign keys or if the "natural" name is a keyword.

Users: id or user_id, username, password, last_login_time
Post: id or post_id, user_id, post_date, content

I generally use 'id' as the primary key field name but in this case I think user_id and post_id are perfectly OK too. Note that the post date was called 'post_date" because 'date' is a keyword.

At least that's my convention. Your mileage may vary.

cletus
One advantage of having `user_id` instead of `id` on primary and foreign keys is that it makes NATURAL joins a breeze.
Alix Axel
+5  A: 

I see no reason to include the table name, it's superfluous. In the queries you can refer to the fields as <table name>.<field name> anyway (eg. "user.id").

Filip Navara
@Omar Dolaimy: Your being "really new" has nothing to do with this answer. Consider deleting your comment.
S.Lott
+1  A: 

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.

APC
+3  A: 

Personally I don't add table names for field names in the main table but when using it as a foreign field in another table, I will prefix it with the name of the source table. e.g. The id field on the users table will be called id, but on the comments table it, where comments are linked to the user who posted them, it will be user_id.

This I picked up from CakePHP's naming scheme and I think it's pretty neat.

partoa
This is pretty common (I follow this practice). Interestingly, it's also an argument against table names in column names -- or at least not compatible with doing so as the naming scheme would start to get confusing at first glance.
lilbyrdie
A: 

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