kiq
kiq

Reputation: 39

SQL creating table error at line 1

I keep getting error in my code : "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 '$sql = "CREATE TABLE Kodu( ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, ' at line 1"

I even tried this code in SQLfiddle and it gave me the same error. Can somebody please help me spot the error? thanks

  $sql = "CREATE TABLE Kodu(
    ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    Dateoforder DATE NOT NULL,
    Contract VARCHAR(10),
    Order INT NOT NULL,
    Office VARCHAR(30) NOT NULL,
    Ship VARCHAR(100) NOT NULL 

)";

Upvotes: 0

Views: 206

Answers (2)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

Try this:

CREATE TABLE Kodu(
    ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    Dateoforder DATE NOT NULL,
    Contract VARCHAR(10),
    OrderNumber INT NOT NULL,
    Office VARCHAR(30) NOT NULL,
    Ship VARCHAR(100) NOT NULL 
)

You need to change Order with another name because order is a protected word in SQL

Or wrap into quotes or backtick the word like this (if ANSI mode is enabled):

   CREATE TABLE Kodu(
        ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
        Dateoforder DATE NOT NULL,
        Contract VARCHAR(10),
        "Order" INT NOT NULL,
        Office VARCHAR(30) NOT NULL,
        Ship VARCHAR(100) NOT NULL 
    ) 

Upvotes: 1

Avitus
Avitus

Reputation: 15958

The issue is the keyword order.

You have to escape the work either using back ticks or surrounding with quotes if ansi mode is enabled.

Please read more How do I escape reserved words used as column names? MySQL/Create Table

The better recommendation is to use another word instead of escaping.

Upvotes: 1

Related Questions