#1215 - 无法在我的sql表中添加外键约束

时间:2015-08-05 19:58:06

标签: mysql foreign-keys

我有3个表,其中user_id在名为public class StackOverFlow31840211 { private static final int SENTENCE1_TOKEN_INDEX = 0; private static final int DIGIT_TOKEN_INDEX = SENTENCE1_TOKEN_INDEX + 1; private static final int SENTENCE2_TOKEN_INDEX = DIGIT_TOKEN_INDEX + 1; public static void main(String[] args) { String[] text = { "cats|1|short hair and long hair", "cats|2|black, blue", "dogs|1|cats are better than dogs" }; ArrayList<String> arrayOne = new ArrayList<String>(); ArrayList<String> arrayTwo = new ArrayList<String>(); for (String s : text) { String[] tokens = s.split("\\|"); int tokenType = 0; for (String token : tokens) { switch (tokenType) { case SENTENCE1_TOKEN_INDEX: arrayOne.add(token); break; case SENTENCE2_TOKEN_INDEX: arrayTwo.add(token); break; } ++tokenType; } } System.out.println("Sentences for first token: " + arrayOne); System.out.println("Sentences for third token: " + arrayTwo); } } 的表中定义为主键user_id,在另一个名为users的表中定义为{{1}的外键引用这是我的用户表:

updates

我的更新表是:

user_id

添加外键时出错:

  

#1215 - 无法添加外键约束

所以帮帮我吧?

1 个答案:

答案 0 :(得分:2)

您应该usersuser_id具有相同类型和长度的updatesuser_id_fk

CREATE TABLE `updates` (
`update_id` INT(11) AUTO_INCREMENT ,
`update` VARCHAR(45),
`user_id_fk` INT(11),
`created` INT(11) ,
`ip` VARCHAR(45),
PRIMARY KEY (`update_id`),
FOREIGN KEY (user_id_fk) REFERENCES users(user_id));