我正在尝试创建以下表格设计,但我在下面收到此错误如何为stops
表格中的arrivaltimes
表设置外键?
1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在第4行的“FOREIGN KEY REFERENCES stop(stop_id)”附近使用正确的语法
stt.execute("CREATE TABLE IF NOT EXISTS stops"
+ "(stop_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, "
+ " name varchar(30) NOT NULL, " + " route INT(11) NOT NULL, "
+ " lat double(10,6) NOT NULL, "
+ " longi double(10,6)NOT NULL) ");
stt.execute("CREATE TABLE IF NOT EXISTS arrivaltimes(id INT(11) NOT NULL PRIMARY KEY,"
+ " weekday VARCHAR(20) NOT NULL,"
+ "arrivaltime time NOT NULL,"
+ " stop_id INT FOREIGN KEY REFERENCES stops(stop_id) )" );
答案 0 :(得分:1)
更改
stop_id INT FOREIGN KEY REFERENCES stops(stop_id)
到
stop_id INT, FOREIGN KEY fk_stop_id(stop_id) REFERENCES stops(stop_id)
答案 1 :(得分:0)
我已更新了查询,请注意FOREIGN KEY的语法,其中包含错误。干杯!
stt.execute("CREATE TABLE IF NOT EXISTS stops"
+ "(stop_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, "
+ " name varchar(30) NOT NULL, " + " route INT(11) NOT NULL, "
+ " lat double(10,6) NOT NULL, "
+ " longi double(10,6)NOT NULL) ");
stt.execute("CREATE TABLE IF NOT EXISTS arrivaltimes(id INT(11) NOT NULL PRIMARY KEY,"
+ " weekday VARCHAR(20) NOT NULL,"
+ "arrivaltime time NOT NULL,"
+ " FOREIGN KEY (stop_id) REFERENCES stops(stop_id) )" );
答案 2 :(得分:0)
如果查看MySQL CREATE TABLE
syntax,您可以选择:
内联定义(注意缺少FOREIGN KEY
)
stop_id INT REFERENCES stops(stop_id)
和明确的定义:
stop_id INT,
FOREIGN KEY (stop_id) REFERENCES stops(stop_id)
或更好(带有命名约束):
stop_id INT,
CONSTRAINT fk_arrivaltimes_stops FOREIGN KEY (stop_id) REFERENCES stops(stop_id)