0

我看过以前的线程。跟着他们,但在这里它给出了语法错误:

        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

错误在哪里?

4

2 回答 2

1

查询中的语法错误与索引部分有关。

改变:

INDEX id,

至:

INDEX (id),
于 2013-11-06T05:05:11.597 回答
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));

您试图在子表中使其成为外键的键应将其作为父表中的主键。否则你可能会得到错误。

祝你好运

于 2013-11-06T04:59:11.030 回答