views:

138

answers:

7

hi just a quick question, is it better to use underscore whilst naming tables or is it better to use camelcase.

example table_name or tableName which one is better? and if there is a reason to be using either what is it?

many thanks.

+1  A: 

I say use whatever naming convention you use in the code that accesses it for high-level structures (like classes).

For example, if you encapsulate the data* in a class called UserInfo, the table should be called UserInfo as well.

* You are encapsulating the data, right?

MiffTheFox
ahh that was a nice piece of info....thanks very much.
bharath
+1  A: 

Rather than provide you with a brief feedback on a few reasons for this and that I will provide a link to Pinal Dave's SQL coding guidelines. They've been well thought out, explained and seem to follow my own preferences on how things should be named and used etc.

Enjoy!

Brian Scott
thanks very much for the link brian...
bharath
Rather specific to SQL Server, but still a good reference.
Craig Trader
Can't say as I agree with all of it though. You should not name storped procs beginning with sp as SQL server will look for a system proc first so you lose a tiny bit of performance in every call.
HLGEM
@HGLEM I suppose it comes down to your preferences, code performance over coding standards / maintainability etc. I'm not saying that sp_ is the way to go with regards to naming stored procedures for the reasons you have mentioned but at least it promotes consistency. +1 for the point though, a new one to me. :-)
Brian Scott
HLGEM - do you need high performance in your code?
sheepsimulator
+1  A: 

My opinion on this is that you should do whatever feels natural for you, if the best naming convention in the world (for the person who wrote it) will not feel natural and you will have to read the document each time you are designing a database than that's bad.

You should do whatever is convenient for you and what flows out of your keyborad better.

In my experience, I moved to ROR from .net a while back and was caught in the way I always saw people do these things and underscored my tables but after a while I neglected this option for my good-old SQL naming convenience.

the same for columns.

Good luck

Avi Tzurel
No, the team should agree on a standard and everyone should use it (and code reviews should rip the dev apart if he doesn't follow the standard). No cowboy coding where everyone uses the standard they feel most comfortable with. And no using the standard form the last place I worked becasue I feel comfortable with that one rather than the one this organization requires nonsense either
HLGEM
@HLGEM working in a team is a different opera all together.when working in a team or managing a team there should definitely be a convention set by the manager.
Avi Tzurel
@HLGEM - I'm going to have to agree with KensoDev. Your comments, whether intended or not, feel like you wish to straight-jacket developers to a coding standard. Standards aren't a bad thing, but my experience is that they are designed to fight last year's battle, and not this year's. Surely there are things we can take from it and apply going forward. But to enforce a coding standard on people when the rules of the game have changed (new language, new problems to solve, new coding techniques) have really soured me to the idea of coding standards. Do you update your standard frequently?
sheepsimulator
Yes, I do wish to straightjacket developers to a database coding standard. This isn't something that changes as database versions change. It's hard to maintain a database where there are no database standards, no one can remember all the possible variations of person_id that are used or whether this table uses underscores and that one doesn't and it adds up to alot of wasted time in writing sql. So yes, I want standards and want them enforced by management. It is unprofessional to not follow corporate standards for anything whether it be coding standards, dress code or work hours.
HLGEM
@HLGEM - I see your point better, especially for things like keeping names the same between columns that have data that reference the same thing, like person_id. I guess database tech hasn't changed all that much, and my comments don't apply. Do you see applying a coding standard for a RDBMS to a 'NoSQL' or 'document' type database a good thing to do? I ask, because I want to understand your perspective better. I appreciate your comments.
sheepsimulator
I would think if you moved to a NoSQL or document database, you would have stardards for that that might be differnt from the standards for the RDBMS. JUst like the standards for C# would be different form the datbase standards as well. You might consider the old standard in developing one for a new technology (after all your developers would be used to the old standard and changes might actually be harder for them to get used to or alternatively it might make it easier for them to think in the new paradigm if they have new standards).
HLGEM
+1  A: 

Everything I have read indicates the best convention is to stick to lower-case for both database names and table names. If you need to put multiple words together, separate them with underscores -- avoid hyphens in table names or else you will have to back-tick all of your table names and database names every time you reference them.

Also, the following configuration parameter is probably relevant to what you are asking:

mysql> show global variables like 'lower%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| lower_case_file_system | OFF   |
| lower_case_table_names | 0     |
+------------------------+-------+
2 rows in set (0.00 sec)

Regarding field names, I tend to use camel-case.

David L Ernstrom
+1  A: 

My preference is to use descriptive names and use Pascal casing (TableName).

Avoid abbreviations, and if you do have to use an abbreviation treat it like a word (use DaylightSavingsTimeInfo or DstInfo instead of DSTInfo).

Naming isn't something that has a single right answer, so the best thing is to pick something and stay consistent. The worst thing is to have a mish-mash of many standards.

Rob Boek
+2  A: 

Some database engines are case-insensitive, and indeed will convert names to all lower-case on some outputs. Because of this, I've taken to using underscores between words and using all lower case.

MySQL respects case as I recall so this isn't an immediate issue for you. But I got burned on this once when I had to port a database from one engine to another -- I think it was from MySQL to Oracle but I wouldn't swear to that -- and all our camelCase names suddenly became run-together names.

I'll also second Rob Boek on the most important thing being consistency. And while we're on the subject, can I make a tangential comment about being consistent in field names? I'm working with a system now where I found that the field "prodid" in one table is in fact the exact same contents as "style" in another, and another system that has "delivereddate" in one table and "datedelivered" in another.

Jay
+1  A: 

I believe that many RDBMSes ignore case, so using camel-case naming schemes may simply not work.

The important thing is to have the column names and table names be descriptive, no matter what nit-picky syntax you decide. If they are descriptive of their data, no one will care whether you used camel-case, underscores, etc.

sheepsimulator
Disagree, I'm working with a legacy database where people didn't care to use a standard in the initial design and it makes it much hadrer to maintain. Pick and standard and use it consistently.
HLGEM
HLGEM - Can you clarify what you mean by standard? Do you mean in terms of syntax (use camelcase, use underscores, always have uppercase SQL), or of content (have columns describe their data, the formatting of the data into HH:MM:SS, formatting)? I think it is detrimental to enforce syntax on developers that the database does not (IME programmers are far too anal about that; it only creates religious battles). But as far as content goes, I think that's okay to talk about in a standard/guidelines.
sheepsimulator
I also would add that HLGEM has a point - some consistency is necessary. For example, columns that contain ids that are shared ought to have the same name across database tables in the specific schema. I'm not advocating a free-for-all here.
sheepsimulator