1

I'm using a query command to create a SQL Server table. I don't want the column name do match any of the SQL Server keywords (ex: use, create,...). How to check the column names (in query string) do not match any SQL Server keywords?

Update 1:

Of course, except for creating a keyword list, then go hand-in-hand comparison.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Loi NT
  • 48
  • 6
  • what do you exactly mean by `How to check the column name (in query string) does not match the MS SQL keyword?` You want to know what are the reserved keyword ? – Squirrel Sep 12 '18 at 02:48
  • Most of the IDEs show the collision automatically. But just test it by some sql statement dispatch before the release :) – The Bitman Sep 12 '18 at 08:39
  • Hi @LoiNT, if one of the responses below answered your question, please make sure you upvote _and_ mark that question as the answer. This helps those who have given time to answer your question and it also helps others who come in future looking for an answer to the same question. Thanks! – pcdev Aug 08 '19 at 06:49

2 Answers2

4

Since you're asking about naming, let me give you a piece of advice which will lead to one possible answer to your question:

If you're not sure, just get into the habit of always surrounding object names in [SquareBraces], then it doesn't matter too much what you call your objects.

But having said that, it's definitely best to follow a good convention, either the convention common in your workplace, or find another sensible convention to follow (sometimes it seems there are more opinions about naming conventions than there are people coding them!).

My preferences (FWIW) put simply are:

  • Table names that reflect the entity that a single row contains, eg, [Order], [OrderLine], [Customer], etc. To answer your original question, you'll notice that ORDER is a keyword here, but as soon as you put square braces around it the parser knows it's an object. Works every time, and you don't have to make contrived or redundant names for the table that holds Order records, for example CustomerOrder.
  • Composite table names for many-many linking tables reflect both tables being linked where it makes sense, such as [Customer_Contact], [Product_ProductCategory], etc.
  • Primary keys are always called simply [Id], regardless which entity it is a primary key for, unless there's a very good technical reason not to, which is rare.
  • Foreign keys are named after the table and column of the PK they refer to, eg. [CustomerId].

As to why I name tables in the singular, rather than think of a table as a collection of, say "Customers", I think of it as a repository where objects of the type "Customer" are stored. This works particularly well when using some ORMs (I like Dapper in C#, but it doesn't work so well with some others where pluralism is almost forced), so you'll have to work out what works best for you and what you're most comfortable with.

Good luck!

pcdev
  • 2,852
  • 2
  • 23
  • 39
2

While writing the script to create a table, a column name that matches a keyword will change in color either pink, blue, grey. Just encase the column in question with [].

Example:

CREATE TABLE MyTable([DateTime] datetime)

Not sure if there's a system table in system databases where you could query this from but dropping it here it case it may be of use to you.

https://learn.microsoft.com/en-us/sql/t-sql/language-elements/reserved-keywords-transact-sql?view=sql-server-2017

CurseStacker
  • 1,079
  • 8
  • 19