Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Authors

Search

XML Feeds

Google Ads

« Best Practice: Every table should have a primary keySQL Server 2008 R2 November CTP Available For Download »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

Column names (and table names) should not have spaces or any other invalid characters in them. This is considered bad practice because it requires you to use square brackets around your names. Square brackets make the code harder to read and understand. The query (presented below) will also highlight columns and tables with numbers in the names. Most of the time, when there is a number in a column name, it represents a de-normalized database. There are exceptions to this rule, so not all occurrences of this problem need to be fixed.

Based on a comment from Aaron Bertrand, I decided to modify the code below. I recognize that some organizations allow (and may even encourage) the use of the underscore character. In the newly modified code below, you can include a list of acceptable symbols. The code below allows the underscore symbol and the $ symbol. Modify this local variable to include any symbol that is acceptable within your organization.

How to detect this problem:

  1. DECLARE @AcceptableSymbols VARCHAR(100)
  2.  
  3. SET @AcceptableSymbols = '_$'
  4.  
  5. SELECT 'ColumnName' AS Type,
  6.        Table_Name + '.' + Column_Name AS Problem
  7. FROM   Information_Schema.Columns
  8. WHERE  Column_Name like '%[^a-z' + @AcceptableSymbols + ']%'
  9.  
  10. UNION all
  11.  
  12. SELECT  'TableName', Table_Name
  13. FROM    Information_Schema.Tables
  14. WHERE   Table_Name Like '%[^a-z' + @AcceptableSymbols + ']%'

How to correct it: If this is a number issue, you may need to redesign your database structure to include more tables. For example, if you have a StudentGrade table with (StudentId, Grade1, Grade2, Grade3, Grade4) you should change it to be StudentGrade with (StudentId, Grade, Identifier). Each student would have multiple rows in this table (one for each grade). You would need to add an identifier column to indicate what the grade is for (test on November 10, book report, etc).

If this is a weird character issue, then you should change the name of the column so it is a simple word or phrase without any spaces, numbers, or symbols. When you do this, make sure you check all occurrences of where this is used from. This could include procedures, function, views, indexes, front end code, etc...

Level of severity: mild
Level of difficulty: moderate

About the Author

George has been developing software professionally for 19 years, first for the department of defense, and then for various other companies. In 1998, George started his software company, Orbit Software, specializing in School Bus Transportation software. His specialty is refining SQL Server queries to deliver optimal performance.
Social SitingsTwitterLTD RSS Feed
329 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

7 comments

Comment from: Noel [Visitor] · http://twitter.com/NoelMcKinney
I keep "weird" characters out of "normal" table names that will go to production. This lets me reserve such characters for use in table names that are for development/test use only and should not go to production databases. My favorite weird character is the dollar sign because it really stands out... For example, I might name a table of development or test results "_$Test$Benchmark$Run1"
11/10/09 @ 09:29
Comment from: Aaron Bertrand [Visitor] · http://www.sqlblog.com/blogs/aaron_bertrand/
George, your check for an "invalid column name" is a little strict. What about underscores? Just because spaces and dashes are bad characters for a column name does not make all symbols taboo.
11/10/09 @ 10:38
Comment from: George Mastros [Member] Email
Aaron, I dislike any symbols in names. This is my preference, but I can see where different organizations would allow certain symbols in their names. I will modify the code (later today) so that it allows a list of acceptable symbols.

Thank you for your feedback!
11/10/09 @ 10:51
Comment from: Emtucifor [Member] Email
George, if someone does the totally crazy and says ']' is an acceptable character, the code will break.
11/12/09 @ 11:52
Comment from: Naomi [Member] Email
What about - in table names?
12/04/09 @ 09:46
Comment from: SQLDenis [Member] Email
Naomi, table names or actually any object name (proc, database, function, view etc etc) should all follow the same kind of naming conventions
12/05/09 @ 08:06
Comment from: Naomi [Member] Email
I forgot right now what caused me to ask this question - some question in one of the forums, but don't remember where.
12/07/09 @ 11:37

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)