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[color=red]\'[/color]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`),
[color=red]KEY `fk_type` (`item_type`)[/color] 外键约束并没有生效
) 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[color=red]''[/color]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`),
[color=red] CONSTRAINT `fk_type` FOREIGN KEY (`item_type`) REFERENCES `t_item_type` [/color](`type_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值