【问题标题】:I can't create a table in MySQL, Please give a solution我不能在MySQL中创建表,请给出解决方案
【发布时间】:2022-09-30 15:37:54
【问题描述】:

我的代码:

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)
    );

这是我的代码,我无法创建订单表并收到此错误消息,

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

请给我一个解决方案。

    标签: mysql


    【解决方案1】:

    order是mysql中的保留关键字,当你想使用它作为表名或列名时,你需要使用`包裹它

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

    DB Fiddle Demo

    【讨论】:

      猜你喜欢
      • 2011-01-03
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多