Do you prefer verbose naming when it comes to database columns? [closed]

只愿长相守 提交于 2019-11-29 04:35:21

The table name already gives the context. No need to prefix columns name with it. When joining tables use table.column syntax.

Rowland Shaw

I'm a fan of option 3:

CREATE TABLE Products
(
    ProductId int NOT NULL IDENTITY(1,1) PRIMARY KEY,
    CategoryId int NOT NULL FOREIGN KEY REFERENCES Categories(CategoryId),
    Name varchar(200) NOT NULL
)

So the primary key is the only column to gain the table's name as a prefix -- IMHO it makes it easier to see when a join has gone wrong. Then again, I also like using GUIDs for primary keys if there is any possibility of having to cope with a merge replication situation at any point in the future...

kristof

Check also related question: Is prefixing each field name in a table with abbreviated table name a good practice?

As I mentioned in my answer; 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. That is not required any more by the modern systems so it is just a convention that is no longer necessary. As mentioned by Think Before Coding "The table name already gives the context. No need to prefix columns name with it"

I stick with as short as names as possible. People prefixing the name of the table on every column makes me violent. PERSON.first_name, not PERSON.person_first_name. We know its a person, its in the person table... what else would it be?

The only time I go against this rule is for id colums, for example: PERSON.personID.

The rule is, with apologies to Einstein; to be as verbose as necessary, but no more verbose.

I'm usually giving each table a unique prefix. So your example would be something like

CREATE TABLE Products
(
    PROD_ID int NOT NULL IDENTITY(1,1) PRIMARY KEY,
    PROD_CAT_ID int NOT NULL FOREIGN KEY REFERENCES Categories(CAT_ID),
    PROD_NAME varchar(200) NOT NULL
)

This makes joining and selecting columns easier, because you have no name conflicts. Unless you reference the same table more than once you will not even need table name aliases (most likely).

However lately I'm starting to thing that (2) might be better, because it's closer to the naming conventions I'm using when writing code (C# in my case).

ID is terribly confusing when you have lots of joins. I prefer to have explict names of ids that match the name of the id in the foregin key. That way you always know that customerid will join with customerid in any other table that has the column.

Never use name as a field name. It is a reserved word and thus should be avoided. Reserved keyworsds are used by the database in some fashion, using them also as filed names is just poor proactice and can create more errors than using the correct descriptive name form the start.

One school of thought is that a column name should represent a single domain space across the database. For example, a product name is something quite different from a company name. One might be longer than the other, etc. In that respect you should use the first method since it distinguishes between the two.

On the other hand, I agree to some degree with Think Before Coding's logic. I always use the table.column syntax, so the context should be clear.

Using the more verbose column names usually avoids conflicts with reserved words.

Then again, it can be quite redundant if every column in your product table is ProductXxxx.

In other words, I don't have an absolute preference, but I do tend more towards the verbose naming convention.

I prefer to stick with the short version of "ID". It's an easy to understand and follow convention that a record identifier for a table would be called "ID" and it will refer to its "own" table and not to something else (though there is sometimes no need for an "own" identifier in some tables like mappings).

Another outcome that it gives is that whenever you write a query you don't need to think and remember the exact column name. You know that as soon as it makes sense for a table to have a record identifier it could be referenced by "ID".

It simplifies things.

I prefer uid, gid, pid, wid, lid, etc..

I think if the column name is not unique in database, you should prefix it with the tablename. Like, ProductId, ProductName. For columns not conflicting with columns in other table, don't prefix it.

Option 2 is the best (for me of course :) ),
I'm a PHP programmer not a .NET one.

I prefer the second option because the first option, to me, seems like unnecessary repetition. Of course, I'm primarily a Ruby on Rails programmer, so I love the DRY principle. :) I found it annoying coming to C# programming because everything does use the first, explicit-but-repetitious version.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!