Can hyphens be used in MySQL table field names?
When using MySQL tables, field names containing characters such as hyphens may cause problems. This usually happens when a third-party CMS system uses field IDs as table field names. While most fields work fine, problems arise when CMS adds "ds-" before a specific field ID.
To solve this problem and allow field names to contain hyphens (e.g. "ds-product"), MySQL provides a solution called delimiter identifiers. These identifiers allow a variety of characters to be used in field names, including punctuation, special characters, and even SQL reserved words.
Use delimiter identifier
To use a delimiter identifier, enclose the field name in question with backticks (`) in MySQL, or double quotes ("") in standard SQL. For example:
<code class="language-sql">SELECT * FROM `ds-product`; -- MySQL SELECT * FROM "ds-product"; -- 标准 SQL</code>
Set ANSI_QUOTES SQL mode
Another option (specific to MySQL) is to set the ANSI_QUOTES SQL mode. This mode allows the use of double quotes to separate field names.
<code class="language-sql">SET SQL_MODE = ANSI_QUOTES; SELECT * FROM "ds-product";</code>
MySQL users can efficiently handle tables with fields that contain hyphens or other non-traditional characters by using delimiter identifiers or setting the ANSI_QUOTES SQL mode. This flexibility enhances database schema customization and usability.
The above is the detailed content of Can MySQL Tables Use Hyphens in Field Names?. For more information, please follow other related articles on the PHP Chinese website!