Sql-server – SQL table with same name as catalog view

best practicesnaming conventionsql serverssmst-sql

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?

Best Answer

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.