-1

I'm using MariaDB and MySQL and I'm trying to create a table using the command prompt but I'm getting a syntax error and I am unsure what is wrong. Below is the error:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Order     INT NOT NULL,
        FName   VARCHAR(255),
        LName   VARCHAR(255),
        PhoneNum        VARCH...' at line 3

Here is what I have:

CREATE DATABASE projecttestDB;
USE projecttestDB;

DROP TABLE IF EXISTS Staff;

CREATE TABLE Staff (
    Year    INT NOT NULL,
    Order   INT NOT NULL,
    FName   VARCHAR(255),
    LName   VARCHAR(255),
    PhoneNum    VARCHAR(255),
    Salary  INT, 
    CHECK(Salary>=0),
    PRIMARY KEY (Year, Order)
);
LoFran
  • 21
  • 1
  • 1
  • 2

2 Answers2

4

order is a reserved word in SQL. If you absolutely have to use it, you can escape the column name using backticks:

CREATE TABLE Staff (
    Year    INT NOT NULL,
    `Order` INT NOT NULL, -- Here!
    FName   VARCHAR(255),
    LName   VARCHAR(255),
    PhoneNum    VARCHAR(255),
    Salary  INT, 
    CHECK(Salary>=0),
    PRIMARY KEY (Year, `Order`) -- And here
);

But it's probably a better idea to rename it to something that isn't a reserved word, such as order_num:

CREATE TABLE Staff (
    Year    INT NOT NULL,
    Order_Num INT NOT NULL,
    FName   VARCHAR(255),
    LName   VARCHAR(255),
    PhoneNum    VARCHAR(255),
    Salary  INT, 
    CHECK(Salary>=0),
    PRIMARY KEY (Year, Order_Num)
);
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Order is a system key word. If you change it to something else it will work.

MarkBeras
  • 459
  • 1
  • 3
  • 13