Do underscores in a MySQL table names cause issues?

前端 未结 11 922
不知归路
不知归路 2021-02-06 22:17

Do underscores in table names affect performance or cause issues on some platforms?

For example, user_profiles

Would it be better to use user

相关标签:
11条回答
  • 2021-02-06 22:55

    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.

    0 讨论(0)
  • 2021-02-06 23:03

    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.

    0 讨论(0)
  • 2021-02-06 23:09

    Nope. Underscores are perfectly legal in table names.

    This page here in the MySQL documentation tells you about what characters are allowed.

    Basically:

    Permitted characters in unquoted identifiers:

    ASCII: [0-9,a-z,A-Z$_]
    Extended: U+0080 .. U+FFFF

    Permitted characters in quoted identifiers:

    ASCII: U+0001 .. U+007F
    Extended: 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.

    0 讨论(0)
  • 2021-02-06 23:11

    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.

    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.

    For this reason, I would not include underscores in table names.

    0 讨论(0)
  • 2021-02-06 23:12

    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.

    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.

    0 讨论(0)
  • 2021-02-06 23:13

    I never had issues with underscore when naming my tables. It's just personal preference.

    0 讨论(0)
提交回复
热议问题