"外键不匹配"在SQLite中

时间:2016-06-13 21:06:36

标签: sqlite foreign-keys

我试图建立一个数据库,当我尝试插入时,我的表遇到了一些麻烦。

t_users:

CREATE TABLE t_users(
username TEXT primary key not null, 
password TEXT not null,
email TEXT not null);

t_games:

CREATE TABLE t_games(
game_id integer primary key autoincrement not null,
status integer not null, 
start_time DATETIME not null, 
end_time DATETIME);

t_questions:

CREATE TABLE t_questions(
question_id integer primary key autoincrement not null,
question TEXT not null, 
correct_ans TEXT not null, 
ans2 TEXT not null, 
ans3 TEXT not null, 
ans4 TEXT not null);

t_playeranswers:

CREATE TABLE t_players_answers(
game_id integer not null, 
username TEXT not null,
question_id integer not null, 
player_answer TEXT not null, 
is_correct integer not null, 
answer_time integer not null, 
primary key(game_id,username,question_id), 
foreign key(game_id) REFERENCES t_games(game_id), 
foreign key(username) REFERENCES t_users(username), 
foreign key(question_id) REFERENCES t_users(question_id)
);

当我尝试使用此行插入t_players_answers表时:

insert into t_players_answers values(0,'user',2,'the king',1,1);

它似乎无法正常工作,它给了我这个错误:

  

SQL错误:外键不匹配 - " t_players_answers"引用   " t_users"

我检查过用户'存在于t_users表中,我无法找到解决方案..

如果你能帮助我,我会感激不尽!

0 个答案:

没有答案
相关问题