我正在处理项目的架构,并且我正在尝试将其导入到我的本地主机,因此我可以编写一些查询,将信息从数据库提取到网页上。我收到#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;
答案 0 :(得分:0)
从错误Cannot resolve table name
开始,我猜您在创建表drivers
之前尝试创建表cities
,这意味着您正在引用一个不存在的表。所以解决方案是首先创建表cities