MySQL 表字段可以包含连字符吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3168644/
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-08-31 16:28:07  来源:igfitidea点击:

Can a table field contain a hyphen?

mysqlsqldatabase

提问by Jacob

I have a table in a MySQL table with a fieldname 'product', and want to rename it to 'ds-product'.

我在 MySQL 表中有一个表,字段名称为“product”,并且想将其重命名为“ds-product”。

The CMS type system I am using uses the id of formfields as the name of the table field to insert into.

我使用的 CMS 类型系统使用表单字段的 id 作为要插入的表字段的名称。

For most this works fine, but for a particular field it prepends 'ds-' to whatever ID I give it, so I must make the table field name match.

对于大多数情况,这可以正常工作,但是对于特定字段,它会在我给它的任何 ID 前面加上“ds-”,因此我必须使表字段名称匹配。

However, when trying to do a query I get the error that

但是,在尝试进行查询时,我收到错误消息

Unknown column 'sales.ds' in 'field list'

“字段列表”中的未知列“sales.ds”

Is there any way I can have a field called ds-product?

有什么办法可以让我拥有一个名为 ds-product 的字段吗?

回答by Bill Karwin

Yes, you can use punctuation, white space, international characters, and SQL reserved words if you use delimited identifiers:

是的,如果使用分隔标识符,则可以使用标点符号、空格、国际字符和 SQL 保留字:

SELECT * FROM `my-table`;

In MySQL, use the back-ticks. In standard SQL, use double-quotes.

在 MySQL 中,使用反引号。在标准 SQL 中,使用双引号。

Or if you use MySQL you can set the ANSI_QUOTESSQL mode:

或者,如果您使用 MySQL,您可以设置ANSI_QUOTESSQL 模式

SET SQL_MODE = ANSI_QUOTES;
SELECT * FROM "my-table";

回答by user5117649

Try putting brackets on the last part of your call on the table. in your case:

尝试将括号放在桌子上通话的最后一部分。在你的情况下:

SELECT * FROM [TABLE-NAME];

just make sure to put the brackets on the table name only. not on the whole database it is located

只需确保将括号仅放在表名上。不是在它所在的整个数据库上

SELECT * FROM some_database.anotherdatabase.[your-table];

P.S. works on columns, too.

PS 也适用于列。

I'm using Microsoft SQL Server Management.

我正在使用 Microsoft SQL Server 管理。