我的代码如下:
CREATE DATABASE IF NOT EXISTS store;
CREATE TABLE IF NOT EXISTS customer (
customer_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(20) NOT NULL,
middle_name VARCHAR(20),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(60) NOT NULL,
usr_pword CHAR(10) NOT NULL,
reg_date DATE,
PRIMARY KEY (customer_id),
FOREIGN KEY (rental_id) REFERENCES (rental_id),
UNIQUE (email)
);
CREATE TABLE IF NOT EXISTS rental (
rental_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
date_out DATE NOT NULL,
date_in DATE NOT NULL,
cost INTEGER NOT NULL,
PRIMARY KEY (rental_id),
FOREIGN KEY (customer_id) REFERENCES (customer_id),
FOREIGN KEY (game_id) REFERENCES (game_id)
);
CREATE TABLE IF NOT EXISTS game (
game_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
game_name VARCHAR(60),
game_publisher VARCHAR(20),
PRIMARY KEY (game_id),
FOREIGN KEY (rental_id) REFERENCES (rental_id)
);
我得到错误: 错误代码1064您的SQL语法错误...在'rental_id UNIQUE(email))附近;'
答案 0 :(得分:3)
此行的语法无效,用于声明外键:
FOREIGN KEY (rental_id) REFERENCES (rental_id),
您必须在任何外键定义中声明引用的表。然后在表名后面,在括号中的表中声明引用的列。例如:
FOREIGN KEY (rental_id) REFERENCES `ReferencedTable` (rental_id),
您应该学习阅读SQL语法文档,因为它可以解决这个问题和其他问题。例如,http://dev.mysql.com/doc/refman/5.7/en/create-table.html显示外键引用的语法:
reference_definition:
REFERENCES tbl_name (index_col_name,...)
[MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]
[ON DELETE reference_option]
[ON UPDATE reference_option]
请注意tbl_name
如何跟随REFERENCES
关键字,然后是括号列表。
检查语法参考文档应该是解决语法错误的第一种方法,就像你得到的那样。