An old question, but since I'm studying this problem right now, here are my two cents:
As far as I can tell, there is no such thing as a user-defined constant. However, you can create a function and return it to the value you need:
CREATE FUNCTION `kMyConstant` () NO SQL DETERMINISTIC RETURNS TINYINT UNSIGNED BEGIN RETURN 0; END
This will obviously return 0 every time.
I'm sure this would be incredibly inefficient compared to just putting 0 in your code, but it would still work in a very small fraction of a second (each time).
We will warn that if you put a request like: update myTable set myVariable = kMyConstant(); There are two possible performance results:
- By placing DETERMINISTIC in the definition, since the parameters do not change for each row, it will be evaluated only once.
- MySQL optimizer is not so smart.
Performance problems, at least you could scatter kMyConstant () throughout your code and know that it will always be the same value, and if you want to change the return value, you just change the function.
PS. If this is overestimated too often, you can always start your routines set @kMyConstant = kMyConstant(); and then use @kMyConstant , but the coding overhead is increasing, so I assume that your mileage will depend on how and how often you use the constant.
source share