skip to Main Content

My code :

CREATE DATABASE IF NOT EXISTS thogakade;

USE thogakade;

CREATE TABLE customer(
    customerId VARCHAR(10),
    name VARCHAR(50),
    address TEXT,
    salary DECIMAL(10,2),
    CONSTRAINT PRIMARY KEY(customerId)
    );

CREATE TABLE order(
    orderId VARCHAR(30),
    orderDate DATE NOT NULL,
    custId VARCHAR(45),
    CONSTRAINT PRIMARY KEY(orderId),
    CONSTRAINT FOREIGN KEY(custId) REFERENCES customer(customerId)
    );

This is my code and I can’t create a order table and I got this error message,

Error- 
ERROR 1064 (42000): 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 'order(
orderId VARCHAR(30),
orderDate DATE NOT NULL,
custId VARCHAR(45),
CONSTRA' at line 1

Please give me a solution for this.

2

Answers


  1. order is a reserved keyword in mysql,you need to use ` to wrap it when you want to use it as table name or column name

    CREATE TABLE `order`(
      orderId VARCHAR(30),
      orderDate DATE NOT NULL,
      custId VARCHAR(45),
      CONSTRAINT PRIMARY KEY(orderId),
      CONSTRAINT FOREIGN KEY(custId) REFERENCES customer(customerId)
    );
    

    Also,it would be good to let customerId and custId have the same type and lenght if you want to add foreign key reference.

    DB Fiddle Demo

    Login or Signup to reply.
  2. Try it like this

    CREATE TABLE order(
    orderId VARCHAR(30),
    orderDate DATE NOT NULL,
    custId INT,
    PRIMARY KEY(orderId),
    FOREIGN KEY(custId) REFERENCES customer(customerId)
    );
    

    Note your customer.customerId should be of same datatype and length as order.custId

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search