Mysql 我能';我不明白为什么我';m获取无法添加外键约束的错误

Mysql 我能';我不明白为什么我';m获取无法添加外键约束的错误,mysql,sql,foreign-keys,schema,primary-key,Mysql,Sql,Foreign Keys,Schema,Primary Key,我正在为一个项目编写一个模式,我正在尝试将它导入到我的本地主机,这样我就可以编写一些查询,将数据库中的信息拉到网页上。我得到一个#1215-无法添加外键约束错误,它涉及将城市(city\u id)用作外键。我不确定问题是什么,它们都是相同的数据类型(INT),而city\u id是cities表的主键。伙计们,我被难住了,我希望这里有人能帮我一把 编辑:所以我得到了这个工作,但没有订单表,我将粘贴在工作模式下 DROP TABLE IF EXISTS cities; -- Table for s

我正在为一个项目编写一个模式,我正在尝试将它导入到我的本地主机,这样我就可以编写一些查询,将数据库中的信息拉到网页上。我得到一个
#1215-无法添加外键约束
错误,它涉及将
城市(city\u id)
用作外键。我不确定问题是什么,它们都是相同的数据类型(
INT
),而
city\u id
cities
表的主键。伙计们,我被难住了,我希望这里有人能帮我一把

编辑:所以我得到了这个工作,但没有订单表,我将粘贴在工作模式下

DROP TABLE IF EXISTS cities;
-- Table for storing cities
create table cities (
    city_id INT(100) NOT NULL AUTO_INCREMENT,
    city_name VARCHAR(32),
    PRIMARY KEY(city_id)
)ENGINE=InnoDB;

DROP TABLE IF EXISTS drivers;
-- table for Drivers registered in lyft database
create table drivers (
    driver_id INT(100) NOT NULL AUTO_INCREMENT,
    fname VARCHAR(32),
    lname VARCHAR(32),
    address VARCHAR(32),
    contactnumber VARCHAR(32),
    driver_city INT(100),
    PRIMARY KEY(driver_id),
    FOREIGN KEY (driver_city) REFERENCES cities(city_id)
)ENGINE=InnoDB;

DROP TABLE IF EXISTS cars;
-- Table for storing cars information in lyft
create table cars (
    car_id INT(100) NOT NULL AUTO_INCREMENT,
    car_name VARCHAR(32),
    car_type VARCHAR(32),
    car_model VARCHAR(32),
    car_licencenumber VARCHAR(32),
    PRIMARY KEY(car_id)
)ENGINE=InnoDB;


DROP TABLE IF EXISTS customers;
-- Table for storing customers using lyft
create table customers (
    c_id INT(100) NOT NULL AUTO_INCREMENT,
    c_fname VARCHAR(32),
    c_lname VARCHAR(32),
    c_city INT(100),
    PRIMARY KEY (c_id),
    FOREIGN KEY (c_city) REFERENCES cities(city_id)
)ENGINE=InnoDB;

DROP TABLE IF EXISTS owns;
--  Table stores which drivers are having which cars
create table owns (
    driver INT(100),
    car INT(100),
    PRIMARY KEY (driver,car),
    FOREIGN KEY (driver) REFERENCES drivers(driver_id) ON DELETE CASCADE,
    FOREIGN KEY (car) REFERENCES cars(car_id) ON DELETE CASCADE
)ENGINE=InnoDB;
无法添加到数据库的表如下:

DROP TABLE IF EXISTS order;
-- Table stores which drivers are driving a customer
create table order (
    driver INT(100),
    customer INT(100),
    city INT(100),
    PRIMARY KEY (driver, customer, city),
    FOREIGN KEY (driver) REFERENCES drivers(driver_id),
    FOREIGN KEY (customer) REFERENCES customers(c_id),
    FOREIGN KEY (city) REFERENCES cities(city_id)
)ENGINE=InnoDB;
当试图通过
phpmyadmin
添加它时,我得到以下错误:

Static analysis:

2 errors were found during analysis.

    An expression was expected. (near "ORDER" at position 21)
    Unrecognized keyword. (near "ORDER" at position 21)

SQL query:

DROP TABLE IF EXISTS ORDER

MySQL said: Documentation
#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' at line 1
最终编辑:我终于弄明白了,我的问题是,
order
在mysql中是一个保留/关键字,因此抛出了一个错误。我把它改成了订单,一切都很好。我将在下面发布最终的模式,以防任何人遇到和我一样的麻烦

DROP TABLE IF EXISTS cities;
-- Table for storing cities
create table cities (
    city_id INT(100) NOT NULL AUTO_INCREMENT,
    city_name VARCHAR(32),
    PRIMARY KEY(city_id)
)ENGINE=InnoDB;

DROP TABLE IF EXISTS drivers;
-- table for Drivers registered in lyft database
create table drivers (
    driver_id INT(100) NOT NULL AUTO_INCREMENT,
    fname VARCHAR(32),
    lname VARCHAR(32),
    address VARCHAR(32),
    contactnumber VARCHAR(32),
    driver_city INT(100),
    PRIMARY KEY(driver_id),
    FOREIGN KEY (driver_city) REFERENCES cities(city_id)
)ENGINE=InnoDB;

DROP TABLE IF EXISTS cars;
-- Table for storing cars information in lyft
create table cars (
    car_id INT(100) NOT NULL AUTO_INCREMENT,
    car_name VARCHAR(32),
    car_type VARCHAR(32),
    car_model VARCHAR(32),
    car_licencenumber VARCHAR(32),
    PRIMARY KEY(car_id)
)ENGINE=InnoDB;


DROP TABLE IF EXISTS customers;
-- Table for storing customers using lyft
create table customers (
    c_id INT(100) NOT NULL AUTO_INCREMENT,
    c_fname VARCHAR(32),
    c_lname VARCHAR(32),
    c_city INT(100),
    PRIMARY KEY (c_id),
    FOREIGN KEY (c_city) REFERENCES cities(city_id)
)ENGINE=InnoDB;

DROP TABLE IF EXISTS owns;
--  Table stores which drivers are having which cars
create table owns (
    driver INT(100),
    car INT(100),
    PRIMARY KEY (driver,car),
    FOREIGN KEY (driver) REFERENCES drivers(driver_id) ON DELETE CASCADE,
    FOREIGN KEY (car) REFERENCES cars(car_id) ON DELETE CASCADE
)ENGINE=InnoDB;

DROP TABLE IF EXISTS orders;
-- Table stores which drivers are driving a customer
create table orders (
    driver INT(100),
    customer INT(100),
    city INT(100),
    PRIMARY KEY (driver, customer, city),
    FOREIGN KEY (driver) REFERENCES drivers(driver_id),
    FOREIGN KEY (customer) REFERENCES customers(c_id),
    FOREIGN KEY (city) REFERENCES cities(city_id)
)ENGINE=InnoDB;

从错误
无法解析表名
,我猜您在创建表
城市
之前试图创建表
驱动程序
,这意味着您引用的是一个不存在的表。因此,解决方案是首先创建表
cities
first

您使用的引擎是什么?错误与此相同:@liorahaydon显然是InnoDBengine@LioraHaydont抱歉,我已更新架构以显示每个表的引擎、字符集和排序。请将您的答案放入答案中,而不是您的问题中。在最短等待时间后,请接受它,以表明您找到了解决方案。下次在你发帖之前,请用谷歌搜索你的错误信息,不要用你的名字。阅读许多如此热门的答案。请阅读手册,了解一般错误。阅读并行动起来。减少您的代码,直到找到错误时没有错误。已修复,但仍然会出现错误,以下是输出:
在引用表中找不到引用列作为第一列出现的索引,或者表中的列类型与引用表中的约束不匹配。请注意,在使用>=InnoDB-4.1.12创建的表中,ENUM和SET的内部存储类型已更改,并且新表中的这些列无法引用旧表中的这些列。请参阅http://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html 有关正确的外键定义。-------------
通过phpmyadmin导入时,我收到以下错误:
#1215-无法添加外键约束
@Anonymous23234720-12473现在您已经修改了表
cities
city_id INT(100)UNSIGNED
,但是表
drivers
仍然
drivers\u city INT
,数据类型应该相同,所以应该是
driver\u city INT(100)UNSIGNED
抱歉,这只是一个输入错误。我的模式正在运行,但我似乎可以添加
order
表。我已经在编辑中发布了上面的错误。