views:

667

answers:

12

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

Example:

Table: User

Fields:
user_id
user_name
user_password

Or do you do you name your fields minimally?

Fields:
id
name
password

If you have used both, then which format you feel helped you most in the long run?

Edit: This question doesn't seem to have a definite answer and both side presents good points. But I have kept the question open for too long time and maybe it's time to mark one answer as accepted. I am therefore marking the highest voted one as accepted.

+19  A: 

Don't do that. It's redundant and leads to frustration in the long run.

The only field, where you could apply this might be id, because user_id would obviously be the id of the user and it would simplify writing joins in SQL. But I wouldn't even do that.

Joachim Sauer
+1: DRY - Don't Repeat Yourself. The column name already belongs to the table, the table stuff is obvious from context.
S.Lott
As we do joins a lot, you'll tend to RY more if the joined tables have name collisions, too many to alias. So do that even for user_id
Hao
+1 I gave the same answer here : http://stackoverflow.com/questions/529863/do-you-prefer-verbose-naming-when-it-comes-to-database-columns/529909#529909
Think Before Coding
+1: DRY your code from redundant portions!
Omar Dolaimy
+13  A: 

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.

tharkun
why can't you just write SELECT user_name, user_password FROM.... If only the user table has the user_ prefix, then you wouldn't have to explicitly say user.whatever
Because you might some day perform some kind of JOIN...
Kev
even if you just select fields from different tables! they might have the same column name, so you need to make clear which column from what table you want to select!
tharkun
and with joins it becomes even more problematic because you probably WANT to have the same column name in two tables...
tharkun
-1 for making a contrived example of making the tablename_fieldname look bad. Who in their right mind would re-specify the *table.* on front of the field when you are only querying one table?
Hao
this is just an example, how often does it happen that you query only one table???
tharkun
Exactly the point,if you query tables,you will still end up identifying them in their full name to disambiguate them on collections,gridview,reports,etc. SELECT user.name as user_name, department.name as department_name, user.password, from user join department on department.id = user.department_id
Hao
Using the prefix method is OK until you are joining the table on itself. So might as well use aliases instead of prefixes.
Mario
+7  A: 

You do not need to do it anymore, and you really should not. The only exception as saua pointed out could be the ID field for the seek of clarity of joins.

The concept of prefixing field names with the table name comes from the old time of legacy systems when each field across the whole database needed to be unique.

So unless you are dealing with legacy systems which require that each field across the whole database has unique name; do not do it.

kristof
IMHO, the only time an ID field needs a prefix is when it's a foreign key. In your joins, you should have the table prefixed anyway, so collision/clarity issues should go away.
Harper Shelby
Harper: thats the exact reason people often prefix the primary key - so it is just the same column name in every table, regardless of PK vs FK
gregmac
+4  A: 

I wouldn't do it. If you want the information which table a field belongs to, you can always write your queries as

select user.id, user.name from user where ...

But imagine you decide for whatever reason to rename one of your tables (maybe from 'user' to 'customer'). You would have to rename all fields as well, to remain consistent.

My opinion: There is no good reason why you should do it, and several good reasons not to do it.

Treb
If there's a compelling reason to rename your table, and fields(user_id to customer_id), so be it, along with pk_newtablename, ck_newtablename__limit_loan, fk_newtablename__primary, etc. If the new name of table is far removed from its original purpose, the initial database design is not good.
Hao
I agree. But in my experience, the initial database design is usually far from perfect...
Treb
A: 

The prefix variant just takes longer to write and makes it harder to read sql statements with many fields.

Even when you are selecting from several tables, this gives you only the benefit of not having to prefix ambiguous fields with the table name. But

SELECT user.name, image.name FROM user, image

is not very different from

SELECT user_name, image_name FROM user, image

The benefit of havong no ambiguous fields in your queries is quickly eaten up by the overhead of having to type the table name each time you are using a column name.

Sebastian Dietz
You would end up aliasing the fields(especially on reports) of this:SELECT user.name, image.name FROM user, image. Why not save yourself the effort? To disambiguate same field name,you have to alias them on PHP,C#,Java,etc. SELECT user.name as user_name, image.name as image_name FROM user, image
Hao
@Hao: So you want to save some effort on a few queries by increasing the investment in all queries?
Sebastian Dietz
Agree. Reporting is the bottomline of all programs. So joining tables is what we frequently do. We often need to alias/disambiguate these kind of fields so they can be mapped to dataset,assoc array,gridview,reports,etc
Hao
It's better to put these fullnames on the table level rather than aliasing them incessantly on queries.
Hao
A: 

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.

Quassnoi
+2  A: 

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.

MicSim
+1  A: 

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?

Tundey
Because tables are meant to be joined, classes aren't. The chances of having name collisions with tables is higher than class do.
Hao
+2  A: 

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.

tyndall
+1  A: 

It's ok to name fields that way(minimally), but for primary key and captions/name. If you consistently name all your primary key as ID, and name as Name, constructing query will degenerate into superfluous aliases:

select i.id as invoice_id

v.id as vendor_id, p.id as product_id, 
v.name as vendor, p.name as product, b.name as branch, c.name as parcel,

i.total_amount,
i.discount,
i.invoice_date

from invoice i
join product p on i.product_id = p.id
join vendor v on i.vendor_id = v.id
join branch b on i.branch_id = b.id
join parcel c on i.parcel_id = c.id

As joining tables and displaying the entity's caption/name is the norm rather than exception, I name my primary key in full form, and for caption/name field, the same name as table name.

create table product
(
product_id uuid not null, -- primary key
product text not null,
bar_code text not null default '',
rfid_code text  not null default '',
current_qty int default 0
);

create table vendor
(
vendor_id uuid not null, -- primary key
vendor text not null,
is_active boolean not null default true
);

create table branch
(
branch_id uuid not null, -- primary key
branch text not null,
sub_branch_of_id uuid,
current_sales money not null default 0,        
);

create table user
(
user_id uuid not null, -- primary key
user text not null,
password text not null default ''
);

So your query won't have superfluous aliases:

select i.invoice_id, p.product_id, v.vendor, p.product, b.branch, c.parcel,

i.total_amount,
i.discount,
i.invoice_date

from invoice i
join product p on o.product_code = p.product_code
join vendor v on o.vendor_code = v.vendor_code
join branch b on o.branch_code = b.branch_code
join parcel c on o.parcel_code = c.parcel_code
Michael Buen
+2  A: 

Putting the prefix on column names can be a good practice. If you're working on a formal (and probably large) database and you're paying any attention to ISO 11179 (particularly the concept of data element names), then it's good to put the full three (or four) part name in: Object - Property - Representation Term. (The fourth optional part is a qualifier.) For example, "user_first_name". That way you have consistency between your data dictionary and the database schema. I wouldn't do this for smaller databases for the reasons already commented on, but in a complex schema this reduces some risk for error.

jalbert
+1  A: 

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
John MacIntyre