外键约束错误

时间:2011-01-21 05:17:02

标签: mysql mysql-error-1005

我有两个表圈和风格,即

CREATE TABLE IF NOT EXISTS `ring` (
  `jewelry_id` int(11) NOT NULL auto_increment,
  `ring_id` varchar(50) NOT NULL,
  `gender` varchar(10) NOT NULL,
  `description` text NOT NULL,
  `image` varchar(100) NOT NULL,
  `type` text NOT NULL,
  PRIMARY KEY  (`jewelry_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

样式表是

CREATE TABLE IF NOT EXISTS `style` (
  `style_id` int(11) NOT NULL AUTO_INCREMENT,
  `style` text NOT NULL,
  `jewelry_id` int(11) NOT NULL,
  PRIMARY KEY (`style_id`),
  KEY `jewelry_id` (`jewelry_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

当我在样式表中添加一个外键时,它给出了我的错误,即

ALTER TABLE `style`
  ADD CONSTRAINT `style_ibfk_1` FOREIGN KEY (`jewelry_id`) REFERENCES `ring` (`jewelry_id`) ON DELETE CASCADE ON UPDATE CASCADE;

错误是

#1005 - Can't create table './j_jewelry/#sql-2c3b_750.frm' (errno: 150) (Details...</a>)

2 个答案:

答案 0 :(得分:3)

MyISAM表不支持外键。制作两个表InnoDB。

答案 1 :(得分:0)

对于外键引用,表类型应为INNODB