Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should underscores be used in column names?

Tags:

Technically, the underscore character (_) can be used in column names. But is it good practice to use underscores in column names ? It seems to make the name more readable but I'm concerned about technical issues which may arise from using them. Column names will not be prefixed with an underscore.

like image 475
Joe Avatar asked Jun 13 '12 17:06

Joe


People also ask

Can column name have underscore?

Column names can be up to 255 characters long. They must begin with an alphabetic character, $ or underscore, and can contain further alphanumeric, underscore, period, and $ characters.

Should table names have underscores?

Rule 1d (Special Characters) - For table names, underscores should not be used. The underscore character has a place in other object names but, not for tables. Using PascalCase for your table name allows for the upper-case letter to denote the first letter of a new word or name.

Can column names have underscores in SQL?

Rule 2g (Special Characters) – Field names should contain only letters and numbers. No special characters, underscores or spaces should be used. Indexes will remain named as the SQL Server default, unless the index created is for a special purpose.

Can we use underscore database name?

Database names must only consist of the letters a to z (both lower and upper case allowed), the numbers 0 to 9 , and the underscore ( _ ) or dash ( - ) symbols. This also means that any non-ASCII database names are not allowed.


2 Answers

There are no direct technical issue with using an underscore in the name. In fact, I do it quite often and find it helpful. Ruby even auto generate underscores in column names and SQL Servers own system objects use underscores too.

In general, it is a good idea to have some naming convention that you stick to in the database, and if that includes underscores, no big deal.

Any character can be used in the name, if you put square brackets or quotes around the name when referring to it. I try to avoid spaces though, since it makes things harder to read.

There are a few things you want to avoid when coming up with a naming convention for SQL Server. They are:

  1. Don't prefix stored procedures with sp_ unless you are planning to make them system wide.
  2. Don't prefix columns with their data type (since you may want to change it).
  3. Avoid putting stuff in the sys schema (you can with hacking, but you shouldn't).
  4. Pretend your code is case sensitive, even when it isn't. You never know when you end up on a server that has tempdb set up to be case sensitive.
  5. When creating temp table, always specify collation for string types.
like image 91
Thomas Kejser Avatar answered Sep 19 '22 06:09

Thomas Kejser


There is no problem with this, as long as it makes the column name clearer.

like image 33
m.edmondson Avatar answered Sep 20 '22 06:09

m.edmondson