How to use special characters in MySQL column names?

我怕爱的太早我们不能终老 提交于 2019-12-10 20:35:37

问题


Here is my failing MySQL code:

CREATE TABLE product (
            id int NOT NULL AUTO_INCREMENT,
            'item-name' VARCHAR(255) NOT NULL,
            'item-description' TEXT,
            'listing-id' VARCHAR(50),
            PRIMARY KEY (id)
        )

The error is:

Error creating table: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
'item-name' VARCHAR(255) NOT NULL, 'item-description' TEXT, 'listing-id'' at line 3

The documentation says to use quotes... What is wrong?


回答1:


Use ` instead of ':

CREATE TABLE product (
        id int NOT NULL AUTO_INCREMENT,
        `item-name` VARCHAR(255) NOT NULL,
        `item-description` TEXT,
        `listing-id` VARCHAR(50),
        PRIMARY KEY (id)
    )



回答2:


You should use the back-quote (`) to quote column names, not the single-quote ('). Look above the tilde key (~).

CREATE TABLE product (
    id INT NOT NULL AUTO_INCREMENT,
    `item-name` VARCHAR(255) NOT NULL,
    `item-description` TEXT,
    `listing-id` VARCHAR(50),
    PRIMARY KEY (id)
)



回答3:


Why do you use quotes? You should use backticks. Try this:

CREATE TABLE product (
    id int NOT NULL AUTO_INCREMENT,
    `item-name` VARCHAR(255) NOT NULL,
    `item-description` TEXT,
    `listing-id` VARCHAR(50),
    PRIMARY KEY (id)
)



回答4:


Please, use normal character in table definition. You can use underscore "_" instead "-".

When you'll query your db, you must always use quote. Impossible for me! :)

If you want follow this road, use the quote

CREATE TABLE product (
    id INT NOT NULL AUTO_INCREMENT,
    `item-name` VARCHAR(255) NOT NULL,
    `item-description` TEXT,
    `listing-id` VARCHAR(50),
    PRIMARY KEY (id)
)


来源:https://stackoverflow.com/questions/17951180/how-to-use-special-characters-in-mysql-column-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!