例如一个表中字段是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条件中必须加上单引号或双引号。
官方文档说明:
https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html
strings. Strings are automatically converted to numbers and numbers to strings as necessary.
如果查询varchar类型不加引号最终是以数字的形式进行比较,如果字符串无法直接转换成数字,则字符串会进行前置数字正则匹配