数据库
首页 > 数据库> > mysql – 创建表时出错:你的SQL语法中有一个错误’order(order_id INT UNSIGNED NOT NULL AUTO_INCREMENT,user_id’在第1行

mysql – 创建表时出错:你的SQL语法中有一个错误’order(order_id INT UNSIGNED NOT NULL AUTO_INCREMENT,user_id’在第1行

作者:互联网

参见英文答案 > Syntax error due to using a reserved word as a table or column name in MySQL                                    1个
我试图用PHP脚本在同一MySQL数据库中创建2个表:
具有主键’user_id’的表’user’和具有主键’order_id’的表’order’和来自’user’表的外键’user_id'(1到多个关系).

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

$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

CREATE TABLE `order` ( ...

或者更好地使用其他名称.

标签:mysql-error-1064,sql,mysql,database
来源: https://codeday.me/bug/20190825/1719058.html