【问题标题】:I can't figure out why I'm getting a Cannot add foreign key constraint error我不知道为什么会出现无法添加外键约束错误
【发布时间】:2018-05-21 01:25:30
【问题描述】:

我正在研究一个项目的架构,并尝试将其导入本地主机,这样我就可以编写一些查询,将数据库中的信息提取到网页上。我收到#1215 - Cannot add foreign key constraint 错误,它与使用cities(city_id) 作为外键有关。我不确定问题出在哪里,它们都是相同的数据类型(INT),city_idcities 表的主键。我被难住了,我希望这里有人可以伸出援手。

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

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 中的保留/关键字,所以会引发错误。我将其更改为orders,一切都很好。我将在下面发布最终架构,以防有人遇到和我一样的麻烦。

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;

【问题讨论】:

  • 你用的是什么引擎?错误与那里相同:stackoverflow.com/questions/18391034/…
  • @LioraHaydont 显然,它是 InnoDB 引擎
  • @LioraHaydont 抱歉,我已更新架构以显示每个表的引擎、字符集和整理。
  • 请将您的答案放入答案而不是您的问题中。在最短的等待时间后,请接受它以表明您找到了解决方案。下次在您发布问题之前,请在没有您具体姓名的情况下搜索您的错误消息。阅读许多 SO 点击的许多答案。阅读有关它和一般错误的手册。阅读How to Askminimal reproducible example 并采取行动。减少你的代码直到没有错误来定位错误。

标签: mysql sql foreign-keys schema primary-key


【解决方案1】:

根据错误Cannot resolve table name,我猜您在创建表cities 之前尝试创建表drivers,这意味着您引用的表不存在。所以解决办法是先创建表cities

【讨论】:

  • 已修复,但仍然出现错误,输出如下:Cannot find an index in the referenced table where the referenced columns appear as the first columns, or column types in the table and the referenced table do not match for constraint. Note that the internal storage type of ENUM and SET changed in tables created with >= InnoDB-4.1.12, and such columns in old tables cannot be referenced by such columns in new tables. Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html for correct foreign key definition. ------------
  • 通过 phpmyadmin 导入时出现此错误:#1215 - Cannot add foreign key constraint
  • @Anonymous23234720-12473 现在你已经修改了表citiescity_id INT(100) UNSIGNED,但是表drivers还是driver_city INT,数据类型应该是相同的,所以应该是driver_city INT(100) UNSIGNED跨度>
  • 对不起,这只是一个错字。
  • 我的架构已半工作,但我似乎可以添加order 表。我已经在编辑中发布了上面的错误。
猜你喜欢
  • 2017-10-31
  • 2016-11-21
  • 2018-04-05
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-05
  • 1970-01-01
相关资源
最近更新 更多