【发布时间】:2018-05-21 01:25:30
【问题描述】:
我正在研究一个项目的架构,并尝试将其导入本地主机,这样我就可以编写一些查询,将数据库中的信息提取到网页上。我收到#1215 - Cannot add foreign key constraint 错误,它与使用cities(city_id) 作为外键有关。我不确定问题出在哪里,它们都是相同的数据类型(INT),city_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 中的保留/关键字,所以会引发错误。我将其更改为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 Ask 和minimal reproducible example 并采取行动。减少你的代码直到没有错误来定位错误。
标签: mysql sql foreign-keys schema primary-key