SQL语法错误外键

时间:2015-01-30 16:21:07

标签: mysql sql database innodb

我有问题,我尝试连接2 db表,如果我进行查询以建立DB结构,答案是语法错误。

我在这里粘贴代码,有人可以帮助我吗?

DROP TABLE IF EXISTS album;
CREATE TABLE `album` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(30) DEFAULT NULL,
  `description` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=armscii8;

DROP TABLE IF EXISTS picture;
CREATE TABLE `picture` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `id_album` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`id_album`) REFERENCES album(id)
) ENGINE=InnoDB DEFAULT CHARSET=armscii8;
抱歉我的英语不好。

2 个答案:

答案 0 :(得分:1)

认为您的id_album必须有unsigned,与id中的album相同

答案 1 :(得分:0)

只需在外键行中添加Unsigned,如下所示:

DROP TABLE IF EXISTS album;
CREATE TABLE `album` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(30) DEFAULT NULL,
  `description` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=armscii8;

DROP TABLE IF EXISTS picture;
CREATE TABLE `picture` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `id_album` int(11) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`id_album`) REFERENCES album(id)
) ENGINE=InnoDB DEFAULT CHARSET=armscii8;