No, there's no need to place quotes around numeric literals.
With MySQL it's fine if the numeric literals are enclosed in single quotes; I think the reason we see it done so often is that it's convenient for programmers not to care about whether it's character or numeric, and just put quotes around all the literals.
There is a difference in behavior when a value is quoted and not quoted when the value is not a numeric literal. For example, given an id
column of type INTEGER
, in this context
... WHERE id = '1X'
The '1X'
here will be interpreted as a literal numeric with a value of 1, but in this context
... WHERE id = 1X
The 1X
here will be interpreted as a column name, rather than as a numeric literal. This will likely cause MySQL to throw an 'Unknown column' exception.
Consider the difference in how these will be interpreted...
... WHERE id = 'id' -- 'id' will be interpreted as numeric literal value 0
... WHERE id = id -- id will be interpreted as a column name
So it really boils down to which behavior is best for your application, when what you expect to be interpreted as a numeric literal is something other than numeric.
My personal preference is to NOT quote the numeric literals. This is probably due to my experience with other DBMSs and the need to avoid issues caused by implicit data conversions. My personal preference is also to use prepared statements, and avoid my statements including values as literals in the SQL text. (With MySQL that point is mostly moot, since a prepared statement with bind variables gets converted into plain SQL text when it's sent to the database... but that's done by the MySQL library, not my code. And again, this is preference is probably most informed by my longer experience using other RDBMSs (Oracle, Teradata, DB2, SQL Server) rather than MySQL itself.