Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use special characters in MySQL column names?

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?

like image 226
Matthew Mela Avatar asked Dec 15 '22 08:12

Matthew Mela


2 Answers

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)
    )
like image 85
Steve Chambers Avatar answered Dec 28 '22 08:12

Steve Chambers


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)
)
like image 25
Gustav Bertram Avatar answered Dec 28 '22 06:12

Gustav Bertram