10

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.

CDR
  • 8,198
  • 11
  • 47
  • 46
  • Possible duplicate of [Naming of ID columns in database tables](https://stackoverflow.com/questions/208580/naming-of-id-columns-in-database-tables) – philipxy Jul 07 '19 at 22:02
  • If you design a table for a simple application, you don't need to use a prefix for fields. But if you want to work on large enterprise applications and you are using too many joins and writing huge SQL queries, it will help you a lot and simplify everything. – Arash Apr 30 '20 at 15:16

14 Answers14

36

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
  • 302,674
  • 57
  • 556
  • 614
  • 2
    +1: DRY - Don't Repeat Yourself. The column name already belongs to the table, the table stuff is obvious from context. – S.Lott Jan 21 '09 at 13:57
  • 1
    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 Jan 21 '09 at 14:22
  • +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 – thinkbeforecoding Feb 25 '09 at 15:08
  • 2
    Using `user_id` instead of `id` allows you to use `USING` rather than `ON` in joins. – David Phillips Sep 28 '12 at 20:19
  • 2
    Using `user_id`might also be a good idea depending on the tools you are using. At my last job we used SAS Enterprise Guide for data analysis. And this tool has the default behavior of wanting to join tables based on shared column names. And as all our tables had an `id` column we had to remove the automatic generated join before we could define the join we really wanted. – Tobias Kremer Aug 30 '16 at 13:45
20

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.

markus
  • 40,136
  • 23
  • 97
  • 142
  • 1
    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 – user44511 Jan 21 '09 at 13:23
  • 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! – markus Jan 21 '09 at 13:45
  • and with joins it becomes even more problematic because you probably WANT to have the same column name in two tables... – markus Jan 21 '09 at 13:46
  • 3
    -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 Jan 21 '09 at 14:12
  • this is just an example, how often does it happen that you query only one table??? – markus Jan 21 '09 at 14:39
  • 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 Jan 21 '09 at 15:07
  • Using the prefix method is OK until you are joining the table on itself. So might as well use aliases instead of prefixes. – Mario Feb 25 '09 at 15:02
10

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.

Community
  • 1
  • 1
kristof
  • 52,923
  • 24
  • 87
  • 110
  • 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 Jan 21 '09 at 14:53
  • 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 Jan 29 '09 at 05:11
7

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
  • 19,903
  • 7
  • 54
  • 87
  • 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 Jan 21 '09 at 15:29
  • I agree. But in my experience, the initial database design is usually far from perfect... – Treb Jan 21 '09 at 16:31
4

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
  • 3,047
  • 2
  • 20
  • 21
3

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
kontur
  • 4,934
  • 2
  • 36
  • 62
3

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
  • 26,265
  • 16
  • 90
  • 133
2

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.

BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
2

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
  • 38,643
  • 9
  • 94
  • 118
2

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
  • 12,910
  • 13
  • 67
  • 106
1

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
  • 2,926
  • 1
  • 23
  • 27
  • 4
    Because tables are meant to be joined, classes aren't. The chances of having name collisions with tables is higher than class do. – Hao Jan 21 '09 at 14:43
0

If you are using a UNIQUE PREFIX for each table, then

  • No need to use an alias for joins (except self join)
  • All the columns in the database should be unique in name
  • You can easily identity the table from column name itself(from a output or select query)
Abdul Rasheed
  • 6,486
  • 4
  • 32
  • 48
0

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
  • 5,587
  • 1
  • 31
  • 39
  • 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 Jan 21 '09 at 14:18
  • @Hao: So you want to save some effort on a few queries by increasing the investment in all queries? – Sebastian Dietz Jan 21 '09 at 15:32
  • 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 Jan 21 '09 at 15:46
  • It's better to put these fullnames on the table level rather than aliasing them incessantly on queries. – Hao Jan 21 '09 at 15:48
0

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
  • 413,100
  • 91
  • 616
  • 614