MySQL 表名中的下划线会导致问题吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6637496/
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
Do underscores in a MySQL table names cause issues?
提问by CyberJunkie
Do underscores in table names affect performance or cause issues on some platforms?
表名中的下划线是否会影响性能或在某些平台上导致问题?
For example, user_profiles
例如, user_profiles
Would it be better to use userProfiles
or is it just a matter of personal preference?
使用会更好userProfiles
还是只是个人喜好问题?
回答by Ben
Nope. Underscores are perfectly legal in table names.
不。下划线在表名中是完全合法的。
This page herein the MySQL documentation tells you about what characters are allowed.
MySQL 文档中的此页面告诉您允许使用哪些字符。
Basically:
基本上:
Permitted characters in unquoted identifiers:
ASCII: [0-9,a-z,A-Z$_]
Extended: U+0080 .. U+FFFFPermitted characters in quoted identifiers:
ASCII: U+0001 .. U+007F
Extended: U+0080 .. U+FFFF
不带引号的标识符中允许的字符:
ASCII: [0-9,az,AZ$_]
扩展: U+0080 .. U+FFFF带引号的标识符中允许的字符:
ASCII:U+0001 .. U+007F
扩展:U+0080 .. U+FFFF
Personally I tend to stick with lowercase a-z, the occasional number, and underscores. But as @Vince said, it's just personal preference.
我个人倾向于使用小写字母 az、偶尔的数字和下划线。但正如@Vince 所说,这只是个人喜好。
回答by mike
The only issue I've seen is that when using mysqlshow to view the structure of a table it appears to treat the underscore as a wildcard and returns only matching table names if there is an underscore in the name.
我看到的唯一问题是,当使用 mysqlshow 查看表的结构时,它似乎将下划线视为通配符,如果名称中有下划线,则仅返回匹配的表名称。
I could find no way to view the table structure of a table if there is an underscore in the name. I just discovered and confirmed this myself this morning.
如果名称中有下划线,我将无法查看表的表结构。我今天早上才发现并证实了这一点。
I know this to be true of MySQL versions 4.0.18 and 4.1.22 for older versions and 5.1.52 for newer. Perhaps this is documented somewhere (I haven't taken the time to look yet), but it might be a perplexing thing for others, so I decided to mention it when I ran across this question when looking for information on the problem myself.
我知道 MySQL 4.0.18 和 4.1.22 版本适用于旧版本,5.1.52 版本适用于较新版本。也许这在某处有记录(我还没有花时间去看),但这对其他人来说可能是一件令人困惑的事情,所以我决定在我自己寻找有关问题的信息时遇到这个问题时提及它。
回答by Ross
I found a few links to MySQL bugs that have either been marked closed or can't reproduce regarding underscores. As far as I know there are no issues - I always use underscores over camel-case and haven't experienced any problems.
我发现了一些指向 MySQL 错误的链接,这些错误要么被标记为关闭,要么关于下划线无法重现。据我所知没有问题 - 我总是在驼峰式大小写上使用下划线并且没有遇到任何问题。
回答by psynnott
There's nothing wrong with using underscores, but keep in mind there may be occassions you need to escape the underscore, e.g. My\_Table
使用下划线没有任何问题,但请记住,有时您可能需要转义下划线,例如 My\_Table
回答by Omar Al-Ithawi
No, it's perfectly good. In fact, it is the most recommended naming from MySQL (based on who they name their internal tables!).
不,它非常好。事实上,这是 MySQL 中最推荐的命名方式(基于他们命名内部表的名称!)。
Be aware that naming that in Microsoft Windows the default MySQL behaviour is to lower-case your table names. This may cause problems. I am not sure what causes this.
请注意,在 Microsoft Windows 中命名默认 MySQL 行为是小写您的表名。这可能会导致问题。我不确定是什么原因造成的。
However I personally prefer to name my tables like UserLikesPage
, User
and PostComment
for example, since it reflects the class name in my code and I don't use Windows with MySQL.
不过我个人比较喜欢的名字我的表像UserLikesPage
,User
以及PostComment
例如,因为它反映了我的代码的类名和我不与MySQL使用Windows。
回答by Dominique
You should avoid it. Although it is a permitted character in MySQL documentation, it appears it might cause trouble. For example, in MySQL 5.0 & 5.1 (and may be later versions), the query cache is never hit for queries involving a table name containing an underscore.
你应该避免它。尽管它是 MySQL 文档中允许使用的字符,但它似乎可能会引起麻烦。例如,在 MySQL 5.0 和 5.1(可能是更高版本)中,对于涉及包含下划线的表名的查询,查询缓存永远不会被命中。
回答by Unity
Nope, underscores in a database never cause any issues at all. My experience say that it is a better idea to identify any words in a database column.
不,数据库中的下划线根本不会引起任何问题。我的经验表明,识别数据库列中的任何单词是一个更好的主意。
If we use 'thisIsMyColumn' as a column name it's easy to write them, but 'this_is_my_column' as column name is more readable than the previous one.
如果我们使用 'thisIsMyColumn' 作为列名很容易编写它们,但是作为列名的 'this_is_my_column' 比前一个更具可读性。
回答by alireza ranjbaran
You can simply add grave accent (`) before and after column name. For example:
您可以简单地在列名前后添加重音符 (`)。例如:
CREATE TABLE USERS(
`PERSON_ID` NVARCHAR(100),
`FNAME` NVARCHAR(255),
`LNAME` NVARCHAR(255),
PRIMARY KEY (`PERSON_ID`)
);
回答by alireza ranjbaran
There is no problem using underscores. I think it is just personal preference.
使用下划线没有问题。我认为这只是个人喜好。
回答by TikiTavi
Many database visualization tools such as SQuirreL SQL and DbVisualizer also treat the underscore as a wildcard of sorts, grouping tables "matching" into a tree. For example, a table "document_a" and related tables "document_a_details", "document_a_history". In DbVisualizer, looking at the "document_a" table shows columns for all three tables.
许多数据库可视化工具(例如 SQuirreL SQL 和 DbVisualizer)也将下划线视为各种通配符,将“匹配”的表分组为一棵树。例如,表“document_a”和相关表“document_a_details”、“document_a_history”。在 DbVisualizer 中,查看“document_a”表会显示所有三个表的列。
This is generally not a problem, but can be confusing. For example, using SQuirreL SQL's graphing tools to generate ERDs combines columns from multiple tables into a single table and draws connectors for the relationships of all of these columns in the ERD. This results in relationships being drawn that don't actually exist.
这通常不是问题,但可能会令人困惑。例如,使用 SQuirreL SQL 的图形工具生成 ERD 将来自多个表的列组合到一个表中,并为 ERD 中所有这些列的关系绘制连接器。这导致绘制的关系实际上并不存在。
For this reason, I would not include underscores in table names.
出于这个原因,我不会在表名中包含下划线。