zoukankan      html  css  js  c++  java
  • MySQL错误:ERROR 1064 (42000): use near 'order’解决

    今天在创建mysql表格

    [SQL] create table order(
    order_id int(10) PRIMARY KEY AUTO_INCREMENT,
    user_id int(10),
    goods_id int(10),
    goods_name varchar(20) not null,
    goods_type varchar(20) not null,
    goods_price double not null,
    order_date datetime not null,
    FOREIGN KEY(user_id) REFERENCES userinfo(user_id),
    foreign key(goods_id) REFERENCES hotel(goods_id)
    )

    出现了这个错误

    [Err] 1064 - 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(10) PRIMARY KEY AUTO_INCREMENT,
    user_id int(10),
    goods' at line 1

    解决

    表名使用了关键字,数据库中命名要避免使用关键字。Hibernate中查询语句为字段起别名,目的就是避免使用关键字。

    (order是关键字,不能作为表名)

    2、数据库Cannot add foreign key constraint

    解决

    产生这个错误的多数原因有一下两点: 
    1,两张表里要设主键和外键的字段的数据类型或者数据长度不一样 (例如这个是int 另外一个是tinyint,或者都是int,但是设置的长度不同)


    2,某个表里已经有记录了

    3、两个表的引擎不一样,查看表的引擎语句:

    show table status from 数据库名 where name='表名';

    4、要设置外键的字段不能为主键

    5、改建所参考的字段必须为主键

    6、两个字段必须具有相同的数据类型和约束

    我遇到的情况就是4、5。后面关联的表的字段必须被设置为主键,才能关联成功。

  • 相关阅读:
    项目maven update 后启动项目出现导常:org.springframework.web.context.ContextLoaderListener
    oracle 函数
    sql 字符串函数、数学函数
    sql 内连接 子查询 合并查询
    sql 单表查询练习
    oracle 实现主键自增
    create alter rename desc select update delete insert
    oracle 数据类型
    oracle 导入导出 dmp 的三种方式
    oracle imp dmp
  • 原文地址:https://www.cnblogs.com/yangmie/p/13091656.html
Copyright © 2011-2022 走看看