MySQL 数据库中列名中的连字符

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

Hyphens in column names in MySQL DB

mysqldatabasehyphen

提问by euphoria83

May be this question has been answered before but I couldn't find it.

可能这个问题以前已经回答过,但我找不到。

I am using a 2/3 yr old MySQL database which has hyphens in its column names. When I try to use these names from my Java code, the names are broken at the hyphen (e.g. air_port becomes air) and thus are not found. I tried replacing hyphens to underscores in my code hoping that the DB might treat them equally but that doesn't work.

我正在使用一个 2/3 年旧的 MySQL 数据库,它的列名中有连字符。当我尝试在我的 Java 代码中使用这些名称时,这些名称在连字符处被破坏(例如 air_port 变为 air),因此找不到。我尝试在我的代码中将连字符替换为下划线,希望数据库可以平等地对待它们,但这不起作用。

How can I escape the hyphen or how can I access these columns ? Could this be an issue of the character set being used ?

如何转义连字符或如何访问这些列?这可能是正在使用的字符集的问题吗?

回答by dar7yl

enclose the names within `back-ticks`

将名称括在 `back-ticks` 内

回答by Robby Slaughter

Do you have hyphens (-) or underscores (_) in your column names?

列名称中是否有连字符 (-) 或下划线 (_)?

Hyphens are a big problem because if you end up mapping a column name to a variable, most languages do not like to have hyphens inside variable names. Perhaps you are using one of the Java libraries that automatically generates variables or objects whose names are based on column names.

连字符是一个大问题,因为如果您最终将列名映射到变量,大多数语言不喜欢在变量名中使用连字符。也许您正在使用 Java 库之一,该库自动生成名称基于列名的变量或对象。

Depending on the nature of your problem, there are a couple of different approaches you can use:

根据问题的性质,您可以使用几种不同的方法:

  1. Rename all of your columns using ALTER TABLE. Be conscious that this could affect referential integrity or other applications that depend on the database. If you don't know what that means, don't do it.
  2. Create SQL views that simple restate the tables you need but with "better" column names. This is not very efficient, but it will allow you to get what you want.
  3. Use the AS keyword when running your SELECT statements to rename columns within queries.
  1. 使用 ALTER TABLE 重命名所有列。请注意,这可能会影响引用完整性或其他依赖于数据库的应用程序。如果您不知道这意味着什么,请不要这样做。
  2. 创建 SQL 视图,简单地重述您需要的表,但使用“更好”的列名。这不是很有效,但它会让你得到你想要的。
  3. 在运行 SELECT 语句以重命名查询中的列时使用 AS 关键字。

None of these are great solutions, but they should get you started. Good luck!

这些都不是很好的解决方案,但它们应该可以帮助您入门。祝你好运!

回答by Gaston

Hyphens in database names aren't good also. But you can use them with the backtick trick `

数据库名称中的连字符也不好。但是您可以将它们与反引号技巧一起使用`

`name-with-hyphen`

回答by Hexodus

It's better to not use hyphens in your column names. I suffered a big problem with JOIN statements where hyphens caused big trouble - there even escaping names in back ticks didn't work.

最好不要在列名中使用连字符。我在 JOIN 语句中遇到了一个大问题,其中连字符造成了很大的麻烦 - 甚至在反引号中转义名称也不起作用。

Convert the column names to use underscores - this is the safest way to go.

将列名转换为使用下划线 - 这是最安全的方法。

回答by Rodolfo Velasco

I had to create a db named pre-ca_db.

我必须创建一个名为 pre-ca_db 的数据库。

I solved the problem with

我解决了这个问题

create database `pre-ca_db`;

Good luck!

祝你好运!

回答by duffymo

This entryat the MySQL forum suggests that you might have a problem. However, I think it's referring to data and not column names.

MySQL 论坛上的这个条目表明您可能遇到了问题。但是,我认为它指的是数据而不是列名。

Thissays "don't do it." Don't know how authoritative it is.

这就是“不要这样做”。不知道权威如何。