2

I have following 2 tables in my database:

CREATE TABLE `address` (
  `id` bigint(20) NOT NULL auto_increment,
  `entity_creation_timestamp` datetime default NULL,
  `entity_version` bigint(20) default NULL,
  `city` varchar(255) default NULL,
  `country` varchar(255) NOT NULL,
  `county` varchar(255) default NULL,
  `label` varchar(255) default NULL,
  `line1` varchar(255) default NULL,
  `line2` varchar(255) default NULL,
  `line3` varchar(255) default NULL,
  `state` varchar(255) NOT NULL,
  `zip` varchar(255) NOT NULL,
  `zip_extension` varchar(255) default NULL,
  `party_id` bigint(20) default NULL,
  `list_index` int(11) default NULL,
  `last_updated_timestamp` datetime default NULL,
  PRIMARY KEY  (`id`),
  KEY `fk_party_postaladdress_id` (`party_id`),
  CONSTRAINT `fk_party_postaladdress_id` FOREIGN KEY (`party_id`) REFERENCES `sims_party` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

the other one :

CREATE TABLE `state` (
  `state` varchar(255) NOT NULL,
  `name` varchar(255) default NULL,
  PRIMARY KEY  (`state`),
  KEY `pk_state` (`state`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

I am trying to apply a foreign key constraint on 'state' column in 'address' table to 'name' column in the 'state' table..

the command i am using for the same is :

ALTER TABLE address
  ADD CONSTRAINT FK_address_state FOREIGN KEY (state)
    REFERENCES state(state) ON DELETE RESTRICT ON UPDATE RESTRICT;

But each time I run this command, I get the following error:

ERROR 1005 (HY000): Can't create table 'db.#sql-75c_da' (errno: 150)

Any idea where its going wrong...???

4

1 回答 1

0

如果 MySQL 从 CREATE TABLE 语句中报告错误号 1005,并且错误消息引用错误 150,则表创建失败,因为未正确形成外键约束。类似地,如果 ALTER TABLE 失败并且它引用错误 150,这意味着为更改的表将错误地形成外键定义。要显示服务器中最近的 InnoDB 外键错误的详细说明,请发出SHOW ENGINE INNODB STATUS

http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

于 2012-12-05T13:44:29.030 回答