3

When naming tables in SQL I try to stay away from SQL reserved keywords, but today a colleague questioned the use of Events as a table name. They said, anything that turns green in SSMS shouldn't be used as a table name.

Are there any conflicts or gotchas that I should be concerned with using Events as a table name in MS SQL Server?

5

I also generally do my best to avoid using reserved words for user created objects, but sometimes it's really hard to avoid. Some very common terms are reserved words (e.g. User and Login, etc), and so sometimes being forced to avoid them at all cost means you need to choose a less meaningful, less intuitively obvious name for your project, and that really does not benefit your project or company.

So the only real "gotcha" is what you need to do in order to prevent the conflicts: always delimit object names that are reserved words (i.e. always specify the names surrounded by [ and ]).

Now, not all words that change color in SSMS will be a technical conflict, but that doesn't mean that you shouldn't delimit them. Those words could become a technical conflict in a future version, and that could complicate (i.e. increase the time, hence cost of) upgrading.

A simple example:

USE [tempdb];
CREATE TABLE dbo.Select ([Col1] INT); -- "Select" = blue
/*
Msg 156, Level 15, State 1, Line XXXXX
Incorrect syntax near the keyword 'Select'.
Msg 102, Level 15, State 1, Line XXXXX
Incorrect syntax near 'INT'.
*/


CREATE TABLE dbo.[Select] ([Col1] INT);
-- Success


CREATE TABLE dbo.Event ([Col1] INT); -- "Event" = blue
-- Success


CREATE TABLE dbo.Events ([Col1] INT); -- "Events" = green
-- Success

Again, even though "Events" isn't required to be delimited, if you do use it, I would highly recommend that you always specify it as [Events] to guarantee no conflicts.

  • 2
    There are hours of fun to be had working out what silly object names you can get away with in SQL Server. My personal test database currently has tables named "WHERE" (with columns named "FROM" and "ORDER BY"), "mytable ; drop database x ;--" and "💩"... – Jerb May 16 at 9:11
  • 1
    @Jerb Exactly!! I have DBs named: 🙈, 🙉, and 🙊 :-). If using supplementary characters, just be sure to be using a version 90 or newer collation, as collations without a version number don't have sort weights for supplementary characters. – Solomon Rutzky May 16 at 15:39
4

Syntax coloring in SSMS isn't a reason to avoid using the word as an identifier but it is a good practice to avoid naming objects listed as current and future reserved keywords in the documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.