MySQL: Can hyphens be used in table field names?
MySQL table field naming generally follows certain conventions, which include avoiding the use of hyphens. However, there may be situations where you need to use hyphenated field names, such as when integrating with external systems or adhering to specific naming rules.
To overcome this limitation, MySQL provides the option to use delimited identifiers, allowing you to include punctuation, spaces, and even SQL reserved words in field names. These delimiter identifiers, enclosed in backticks or double quotes, provide a way around traditional naming restrictions.
For example, in your case you can rename your "product" table field to "ds-product" using backticks like this:
<code class="language-sql">ALTER TABLE sales RENAME COLUMN product TO `ds-product`;</code>
Alternatively, you can modify the SQL mode in MySQL to enable ANSI_QUOTES mode. This mode allows you to use double quotes as delimiters for field names:
<code class="language-sql">SET SQL_MODE = ANSI_QUOTES; ALTER TABLE sales RENAME COLUMN product TO "ds-product";</code>
By using the delimiter identifier, you can have a table field named "ds-product" without triggering the "Unknown column" error you encountered before.
The above is the detailed content of MySQL Table Field Names: Can I Use Hyphens?. For more information, please follow other related articles on the PHP Chinese website!