mysql建表成功但外键异常

mysql创建表时,当comment内容中出现 \' 字符串时,虽然表创建成功了,但外键创建失败。
解决办法:将"\'"替换为两个单引号(即"''")
http://bugs.mysql.com/bug.php?id=67881


重现步骤
mysql>
mysql> use test
Database changed
mysql> show create table t_item_type\G;
*************************** 1. row ***************************
       Table: t_item_type
Create Table: CREATE TABLE `t_item_type` (
  `type_id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
  `type_name` varchar(255) NOT NULL,
  PRIMARY KEY (`type_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>
mysql> 
mysql> show create table t_item_type\G;
*************************** 1. row ***************************
       Table: t_item_type
Create Table: CREATE TABLE `t_item_type` (
  `type_id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
  `type_name` varchar(255) NOT NULL,
  PRIMARY KEY (`type_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

ERROR:
No query specified


##########################################################################
##### comment中含有 \' 字符串,导致外键异常
mysql> DROP TABLE IF EXISTS t_item;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE t_item
    -> (   item_id INT UNSIGNED AUTO_INCREMENT NOT NULL,
    ->     price SMALLINT UNSIGNED NOT NULL COMMENT 'the price of item',
    ->     item_type TINYINT UNSIGNED NOT NULL COMMENT 'foreign t_item_type \'s of type_id',
    ->     PRIMARY KEY(item_id),
    ->     CONSTRAINT fk_type FOREIGN KEY(item_type) REFERENCES t_item_type(type_id)
    -> ) ENGINE=INNODB;
Query OK, 0 rows affected (0.08 sec)

mysql>
mysql> show create table t_item\G;
*************************** 1. row ***************************
       Table: t_item
Create Table: CREATE TABLE `t_item` (
  `item_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `price` smallint(5) unsigned NOT NULL COMMENT 'the price of item',
  `item_type` tinyint(3) unsigned NOT NULL COMMENT 'foreign t_item_type''type_id',
  PRIMARY KEY (`item_id`),
  KEY `fk_type` (`item_type`)  外键约束并没有生效
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

ERROR:
No query specified


#####################################################################
##### COMMENT 内容中的"\'"替换为"''",则外键成功创建
mysql>
mysql>
mysql>
mysql> DROP TABLE IF EXISTS t_item;
Query OK, 0 rows affected (0.03 sec)

mysql> CREATE TABLE t_item
    -> (   item_id INT UNSIGNED AUTO_INCREMENT NOT NULL,
    ->     price SMALLINT UNSIGNED NOT NULL COMMENT 'the price of item',
    ->     item_type TINYINT UNSIGNED NOT NULL COMMENT 'foreign t_item_type ''s of type_id',
    ->     PRIMARY KEY(item_id),
    ->     CONSTRAINT fk_type FOREIGN KEY(item_type) REFERENCES t_item_type(type_id)
    -> ) ENGINE=INNODB;

SHOW CREATE TABLE t_item\G;Query OK, 0 rows affected (0.06 sec)

mysql>
mysql> SHOW CREATE TABLE t_item\G;
*************************** 1. row ***************************
       Table: t_item
Create Table: CREATE TABLE `t_item` (
  `item_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `price` smallint(5) unsigned NOT NULL COMMENT 'the price of item',
  `item_type` tinyint(3) unsigned NOT NULL COMMENT 'foreign t_item_type''s of type_id',
  PRIMARY KEY (`item_id`),
  KEY `fk_type` (`item_type`),
  CONSTRAINT `fk_type` FOREIGN KEY (`item_type`) REFERENCES `t_item_type` (`type_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>

你可能感兴趣的:(mysql)