H2数据库列名称"GROUP"是保留字 [英] H2 database column name "GROUP" is a reserved word

查看:430
本文介绍了H2数据库列名称"GROUP"是保留字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在H2中使用名为GROUP的列创建表?不久前,我看到一个使用[*]之类的示例,但似乎找不到.

How do I create a table in H2 with a column named GROUP? I saw an example that used something like [*] a while ago, but I can't seem to find it.

推荐答案

跟踪下划线

添加跟踪下划线:GROUP_

SQL规范明确承诺,其中没有关键字将带有下划线.因此,可以确保您创建的任何带有下划线或下划线的命名都不会与关键字或保留字发生冲突.

The SQL spec explicitly promises that no keyword will ever have a trailing underscore. So you are guaranteed that any naming you create with a leading or trailing underscore will never collide with a keyword or reserved word.

我在数据库中命名我所有的列,约束等,并在其后加上下划线.一开始似乎有些怪异,但您已经习惯了.事实证明,它具有很好的副作用:在所有编程以及笔记和电子邮件中,当我看到尾随的下划线时,我都知道上下文是数据库,而不是编程变量或业务术语.

I name all my columns, constraints, etc. in the database with a trailing underscore. Seems a bit weird at first, but you get used to seeing it. Turns out to have a nice side-effect: In all the programming as well as notes and emails, when I see the trailing underscore I know the context is the database as opposed to a programming variable or a business term.

另一个好处是放心.这样就消除了由于关键字冲突而导致的所有可能的错误和怪异问题.如果您在想没什么大不了的,那么要记住和避免的几个SQL关键字",请再考虑一下.有一个不计其数的关键字和保留字,一个不计其数超过一千.

Another benefit is peace-of-mind. Such a relief to eliminate an entire class of possible bugs and weird problems due to keyword collision. If you are thinking, "No big deal - what's a few SQL keywords to memorize and avoid", think again. There are a zillion keywords and reserved words, a zillion being over a thousand.

Shiva的答案也是正确的:在名称"GROUP"周围添加引号确实可以解决问题.不利之处在于,记住添加这些引号会很累又麻烦.

The answer by Shiva is correct as well: Adding quotes around the name, "GROUP", does solve the problem. The downside is that remembering to add those quotes will be tiresome and troublesome.

其他提示:为了最大程度地跨各种SQL数据库兼容,请使用所有小写字母进行命名. SQL规范指出,所有名称都应以大写形式存储,同时允许使用小写形式.但是不幸的是,某些(大多数?)数据库在这方面没有遵循规范.经过数小时的各种数据库研究,我得出的结论是,全小写可为您提供最大的可移植性.

Further tip: For maximum compatibility across various SQL databases, do your naming in all lowercase. The SQL spec says that all names should be stored in uppercase while tolerating lowercase. But unfortunately some (most?) databases fail to follow the spec in that regard. After hours of study of various databases, I concluded that all-lowercase gives you maximum portability.

所以我实际上建议您为列命名:group_

So I actually suggest you name your column: group_

多个单词名称如下:given_name_date_of_first_contact_

Multiple word names look like this: given_name_ and date_of_first_contact_

不幸的是,我无法引用SQL规范,因为它受版权保护.在 SQL:2011 规范中,阅读第5.4节名称和标识符 >在语法规则标题3的注111中.在 SQL-92中参见5.2节第11项.只需搜索单词underscore即可.

I cannot quote the SQL spec because it is copyright protected, unfortunately. In the SQL:2011 spec, read section 5.4 Names and identifiers under the heading Syntax Rules item 3, NOTE 111. In SQL-92 see section 5.2, item 11. Just searching for the word underscore will work.

这篇关于H2数据库列名称"GROUP"是保留字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆