我见过以前的帖子。跟着他们,但在这里它给出了语法错误:
create table temp (name varchar(20), id varchar(128), hash varchar(128),
INDEX id, CONSTRAINT FOREIGN KEY (id) references user_record(userid), CONSTRAINT
FOREIGN KEY (hash) references post_data(hash));
错误:
#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 ' CONSTRAINT FOREIGN KEY (id)
references user_record(userid), CONSTRAINT FOREIGN ' at line 1
哪里出错?
答案 0 :(得分:1)
请参阅下面的示例,这可能会有所帮助: 这是一个用户表:
CREATE TABLE user(
userid INTEGER(8),
email VARCHAR(30),
password VARCHAR(30),
createdat datetime,
PRIMARY KEY(userid));
CREATE TABLE profile(
userid INTEGER(8),
firstname VARCHAR(20),
middlename VARCHAR(20),
lastname VARCHAR(20),
city VARCHAR(20),
state VARCHAR(20),
country VARCHAR(20),
zip INTEGER(5),
PRIMARY KEY(userid),
INDEX(userid),
CONSTRAINT FOREIGN KEY(userid) REFERENCES user(userid));
CREATE TABLE tags(
tagid INTEGER(8),
tagname VARCHAR(40),
PRIMARY KEY(tagid));
CREATE TABLE notes(
notesid INTEGER(8),
notes VARCHAR(50),
CONSTRAINT FOREIGN KEY(userid) REFERENCES user(userid),
CONSTRAINT FOREIGN KEY(tagid) REFERENCES tags(tagid));
您尝试将其作为子表中的外键的密钥应将其作为父表中的主键。否则你可能会收到错误。
祝你好运答案 1 :(得分:1)
查询中的语法错误与索引部分有关。
更改:
INDEX id,
要:
INDEX (id),