mysql - 创建表时出错 : You have an error in your SQL syntax near 'order( order_id INT UNSIGNED NOT NULL AUTO_INCREMENT, user_id ' at line 1

标签 mysql sql database mysql-error-1064

我正在尝试使用 PHP 脚本在同一个 MySQL 数据库中创建 2 个表: 表“user”的主键为“user_id”,表“order”的主键为“order_id”,外键“user_id”来自“user”表(一对多关系)。

表用户创建成功,没有问题:

$sql="CREATE TABLE user(
    user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    type ENUM('member','admin') NOT NULL,
    username VARCHAR(30) NOT NULL,
    email VARCHAR(80) NOT NULL,
    pass VARBINARY(32) NOT NULL,
    first_name VARCHAR(40) NOT NULL,
    last_name VARCHAR(40) NOT NULL,
    date_expires DATE NOT NULL,
    date_created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    date_modified TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
    PRIMARY KEY (user_id),
    UNIQUE (username),
    UNIQUE (email) 
    )ENGINE=InnoDB DEFAULT CHARSET=utf8";

但是,我无法创建表顺序:

$sql="CREATE TABLE order(
    order_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    user_id INT UNSIGNED NOT NULL,
    transaction_id VARCHAR(19) NOT NULL,
    payment_status VARCHAR(15) NOT NULL,
    payment_amount DECIMAL(6,2) UNSIGNED NOT NULL,
    payment_date_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (order_id),
    FOREIGN KEY (user_id) REFERENCES user (user_id)
    )ENGINE=InnoDB DEFAULT CHARSET=utf8";

我收到以下错误:

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 'order( order_id INT UNSIGNED NOT NULL AUTO_INCREMENT, user_id ' at line 1

已经检查了语法,找不到错误。您能告诉我出了什么问题吗?非常感谢。

最佳答案

你需要逃脱reserved words就像带反引号的order

CREATE TABLE `order` ( ...

或者最好使用其他名称。

关于mysql - 创建表时出错 : You have an error in your SQL syntax near 'order( order_id INT UNSIGNED NOT NULL AUTO_INCREMENT, user_id ' at line 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24981928/

相关文章:

java - JPQL 多对多选择

mysql - Resque Mysql2::Error: 用户已超出 'max_user_connections' 资源(当前值:10)

java - 无法连接MySQL数据库

mysql - 将 MySQL 查询转换为 Hive

sql - 尝试对 SQL Server 中的两列求和导致错误消息

sql - 索引在 NOT IN 或 <> 子句中起作用吗?

使用外部数据库的 ASP.net Web 窗体应用程序登录

sql - MySQL 复合索引和运算符 BETWEEN

php - MySql 逗号分隔值,使用 Where 子句获取

mysql - 查找嵌套 JSON 中具有 'true' 值的所有记录