tags:

views:

1936

answers:

8

Is it ok to name my database tables that are already keywords? For my case, I am trying to name the table that will hold my users. I've named it User but it is showing up as pink in SQL Server Management Studio so I am assuming its an existing System Table or Keyword. Thanks for your advice.

+3  A: 

You can use [User] to do this. If at all possible use a table name that doesn't conflict with a keyword, to avoid confusion and bugs.

+6  A: 

You can create tables with the same name as keywords. If you "quote" the table name it should work. The default quotes in SQL server are square brackets: []

CREATE TABLE [dbo].[user](
    [id] [bigint] NOT NULL,
    [name] [varchar(20)] NOT NULL
) ON [PRIMARY]
Andy White
+3  A: 

Yes, it is ok. In your queries, you can put [ and ] around your table name so that SQL Server knows you are referring to a table - i.e.

CREATE TABLE [User] ...
SELECT * FROM [User]
Jakob Christensen
+10  A: 

repeat this three times:

DO NOT DO IT, I WILL NOT USE RESERVED WORDS!

you'll thank me!

racer x
Oh yes.Very strange things happen when you try to query such table/field with a MS Query (which is the querying tool for MS Office). It will NOT work despite you put brackets on Excel side. You will rename the table/field eventually.
GSerg
Why torture people that have maintain your code later on or even yourself?
ojblass
Respectfully disagree. It's good habit to bracket your table names anyways. And every SQL tool I've used, both MSFT and third party, does this automatically. So in practice, it doesn't make a big difference.
Portman
sometimes you really need to use it, like if your entity name is User, changing to another table name makes the project dangerous.
thephpdeveloper
+1  A: 

As mentioned, you can do it by quoting the name. Of course, you'll also have to quote the name anytime you reference it - trust me, it gets old real quick.

As an aside, just because SSMS syntax colors the word doesn't necessarily mean it's a reserved word. Sql can be annoying like that. ;)

Mark Brackett
+1  A: 

Basic rule for table and column names (or better object names in general):

Don't use anything the same as, or even similar to, a reserved word. Only use A-Za-z0-9 and underscore. Especially don't use spaces. Only use names that don't require escaping, and then don't use escaping as a perpetual test.

You, and everyone who works with you, or will ever work on your code, don't need the aggravation.

le dorfier
+1  A: 

I sit in the camp that says that table names should be plural, so in your case that would be Users.

I like this convention as it makes sense to me. You have a collection of users so call your table that. Further down stream if you pull out an indvidual row that could then populate an object named User.

If your convention dictates use of singular for table names use something different e.g.: Member, Client etc.

Also see RacerX's answer!

As previously mentioned it is tecnically OK if you [Braket] the name.

Jon P
+1 to plural table names.
Portman
A: 

As everyone else has said don't do this; however, I was in the same boat. I have a rule that says all my tables are stored in singular form. Organization not Organizations, Asset not Assets a PartsCatalog has many Part etc.

Well I have a User table so what do I call it? I ended up escaping it [User]. Now I regret that decision because I am always forgetting to escape the table; however, I've not come up with a better name yet: Member is the leading candidate.

JoshBerke
I put my users in a "Person" table. :)
David
But what if you need to model People who aren't users?
JoshBerke