300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > MySQL中varchar类型字段隐式转换造成多删除数据

MySQL中varchar类型字段隐式转换造成多删除数据

时间:2022-07-01 02:26:25

相关推荐

MySQL中varchar类型字段隐式转换造成多删除数据

例如一个表中字段是varchar类型:

desc test;+-------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra|+-------+-------------+------+-----+---------+----------------+| id | int | NO | PRI | NULL | auto_increment || name | varchar(10) | YES || NULL ||+-------+-------------+------+-----+---------+----------------+select * from test;+----+------+| id | name |+----+------+| 1 | 111 || 2 | 111a || 3 | 111b || 4 | 222 || 5 | 222a || 6 | 222b |+----+------+select * from test where name=111;+----+------+| id | name |+----+------+| 1 | 111 || 2 | 111a || 3 | 111b |+----+------+

这种情况,如果你去delete from test where name = 111;原本是想删除id = 1这一行,但是却删除了三行,结果就造成了悲剧。

在oracle中不会出现这种情况,直接给你报错:

16:43:24 SCOTT@orcl11g>select * from test where name=111;ERROR:ORA-01722: invalid number

正确的语句应该是delete from test where name = '111'; 是字符串的类型,在where条件中必须加上单引号或双引号。

官方文档说明:

/doc/refman/5.7/en/comparison-operators.html

strings. Strings are automatically converted to numbers and numbers to strings as necessary.

如果查询varchar类型不加引号最终是以数字的形式进行比较,如果字符串无法直接转换成数字,则字符串会进行前置数字正则匹配​

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。