0

In Microsoft SQL Server, I had a table called [Key], but I hated having to address it as [Key] each time.

So I renamed it to ki instead. Well, at least the square brackets are gone.

Now what I'd like to do is:

CREATE SYNONYM Key FOR ki

But that didn't work. I guess I'm out of luck. Any other bright ideas?

I've never had a table with a plural name like people or addresses or anything. It's always been the person table or the address table.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Phillip Senn
  • 46,771
  • 90
  • 257
  • 373
  • 3
    The reason you have to use the square brackets is because you trying to name an object the same a reserved word. Why not name it something that means something instead of the super ambiguous Key? – Sean Lange May 08 '15 at 19:29
  • 3
    If you're insisting on using one of the [reserved T-SQL keywords](https://msdn.microsoft.com/en-us/library/ms189822.aspx) then **no**, you cannot skip the square brackets – marc_s May 08 '15 at 19:29
  • 3
    SQL Server can be configured to comply with the SQL standard regarding quoted identifiers. In that case you can use double quotes instead of those dreaded brackets: `"key"` –  May 08 '15 at 19:30
  • You can also use single quotes for the names. I usually use single quotes when I'm selecting queries and making computed columns. – John Odom May 08 '15 at 19:31
  • You can use the ANSI standard double quotes instead of square brackets. The `QUOTED_IDENTIFIER` defaults to on since at least 2008. Otherwise, as the others say, you picked a reserved keyword. It's like naming a table `SELECT`. It's going to cause problems unless you make it obviously an object name with identifiers. – Bacon Bits May 08 '15 at 19:34
  • 1
    Interestingly, in my shop we just use square brackets on everything... whether required or not. All schema names, table names, and column names are always fully bracketed, and table names are always fully qualified (as in "[dbo].[MyTable]"). It eliminates a lot of ambiguity, and it's a huge hardship as there are some SQL formatters out there that will automatically add the brackets for you. So just suck it up and type [Key] and get over it :-) – pmbAustin May 08 '15 at 20:24

1 Answers1

1

I know alot of people don't like this, but I always use prefixes or suffixes for everything in sql.
tbl for tables, stp for procedures, v for views etc'.
In column names the table name itself is the prefix: person_id, person_fkvalue etc'.

This way I never have to deal with reserved words, and can immediately know to what table the column belongs to.
This also eliminates the need of using aliases for table names in joins.
I'm a big fan of using meaningful names, so even derived tables usually gets a name and not a letter.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121