在 MS SQL Server 中创建作为保留字/关键字的表名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/695578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:32:11  来源:igfitidea点击:

Creating table names that are reserved words/keywords in MS SQL Server

sqlsql-server

提问by EverTheLearner

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 Userbut 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.

可以命名已经是关键字的数据库表吗?就我而言,我正在尝试命名将容纳我的用户的表。我将它命名为User,但它在 SQL Server Management Studio 中显示为粉红色,因此我假设它是一个现有的系统表或关键字。谢谢你的建议。

Official list of reserved keywords: Reserved Keywords (Transact-SQL)

保留关键字的官方列表:保留关键字 (Transact-SQL)

回答by racer x

repeat this three times:

重复三遍:

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

不要这样做,我不会使用保留字!

you'll thank me!

你会感谢我的!

回答by Andy White

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: []

您可以创建与关键字同名的表。如果您“引用”表名,它应该可以工作。SQL Server 中的默认引号是方括号:[]

CREATE TABLE [dbo].[user](
    [id] [bigint] NOT NULL,
    [name] [varchar](20) NOT NULL
) ON [PRIMARY]

回答by Jakob Christensen

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.

是的,没关系。在您的查询中,您可以将 [ 和 ] 放在您的表名周围,以便 SQL Server 知道您指的是一个表 - 即

CREATE TABLE [User] ...
SELECT * FROM [User]

回答by brian-brazil

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.

您可以使用 [用户] 来执行此操作。如果可能,请使用与关键字不冲突的表名,以避免混淆和错误。

回答by Viviane Rosa

For MS Query, I have found the double quotes works perfectly in the Query.

对于 MS Query,我发现双引号在 Query 中非常有效。

select T1."Reference"
from MyTable T1

回答by Eduardo Cuomo

Use the 'single quote'for Strings, and "double quote"for column names.

使用“单引号”字符串,而“双引号”列名。

Example:

例子:

INSERT INTO AccountMovement
("Date", Info)
VALUES
('2012/03/17', 'aa'),
('2012/03/17', 'bb'),
('2012/03/17', 'cc'),
('2012/03/17', 'dd')

回答by Jon P

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.

我喜欢这个约定,因为它对我来说很有意义。您有一组用户,因此请调用您的表。如果您拉出一个单独的行,然后可以填充一个名为 User 的对象,则在更下游。

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

如果您的约定要求对表名使用单数,请使用不同的内容,例如:Member、Client 等。

Also see RacerX's answer!

另请参阅 RacerX 的回答!

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

如前所述,如果您 [Brake] 名称在技术上是可以的。

回答by Mark Brackett

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. ;)

顺便说一句,仅仅因为 SSMS 语法为这个词着色并不一定意味着它是一个保留字。Sql 可能会很烦人。;)

回答by dkretz

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.

不要使用与保留字相同甚至相似的任何内容。仅使用 A-Za-z0-9 和下划线。特别是不要使用空格。只使用不需要转义的名称,然后不要将转义用作永久测试。

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

您,以及与您一起工作或将要处理您的代码的每个人,都不需要加重负担。

回答by JoshBerke

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.

正如其他人所说,不要这样做;然而,我在同一条船上。我有一条规则,说我的所有表都以单数形式存储。组织不是组织,资产不是资产 PartsCatalog 有很多 Part 等。

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.

好吧,我有一个 User 表,所以我怎么称呼它?我最终逃脱了[用户]。现在我后悔那个决定,因为我总是忘记逃离桌子;然而,我还没有想出一个更好的名字:会员是领先的候选人。