Go to the first, previous, next, last section, table of contents.


Literals. How do you write strings and numbers?

Strings

A string may have ' or " around it.

\ is a escape character. The following escape characters are recognised:

\0
An ascii 0 character.
\n
A newline character.
\t
A tab character.
\r
A return character.
\b
A backspace character.
\'
A ' character.
\"
A " character.
\\
A \ character.
\%
A % character. This is used in wildcard strings to search for %.
\_
A _ character. This is used in wildcard strings to search for _.
A ' inside a string started with ' may be written as ".
A " inside a string started with " may be written as "".

Some example selects that shows how it works.

MySQL> select 'hello', "'hello'", '""hello""', "'h"e"l"l"o"', "hel""lo";
1 rows in set (0.00 sec)

+-------+---------+-----------+-------------+--------+
| hello | 'hello' | ""hello"" | 'h'e'l'l'o' | hel"lo |
+-------+---------+-----------+-------------+--------+
| hello | 'hello' | ""hello"" | 'h'e'l'l'o' | hel"lo |
+-------+---------+-----------+-------------+--------+
mysql> select 'hello', "hello", '""hello""', "'ello", 'e"l"lo', '\'hello';
1 rows in set (0.00 sec)

+-------+-------+-----------+-------+--------+--------+
| hello | hello | ""hello"" | 'ello | e'l'lo | 'hello |
+-------+-------+-----------+-------+--------+--------+
| hello | hello | ""hello"" | 'ello | e'l'lo | 'hello |
+-------+-------+-----------+-------+--------+--------+
mysql> select "This\nIs\nFour\nlines";
1 rows in set (0.00 sec)

+--------------------+
| This
Is
Four
lines |
+--------------------+
| This
Is
Four
lines |
+--------------------+

If you want to insert binary data into a blob the following characters must be represented by escape sequences:

\0
Ascii 0. Should be replaced with "\0" (A backslash and a 0 digit).
\
Ascii 92, backslash
'
Ascii 39, Single quote
"
Ascii 33, Double quote

One doesn't have to escape " inside ' and " inside '.

If you write C code you can use the C API function mysql_escape_string(char *to,char *from,uint length) to escape characters for the INSERT clause. (Note that 'to' must be at least 2 times bigger than from). In perl you can use the quote function.

You should run the escape function on every possible string that may have a one of the above special characters!

Numbers

Integers are just a sequence of digits. Floats use . as a decimal separator.

Examples of valid numbers are: 1221, 294.42, -32032.6809e+10.

NULL

When using the text file export formats, NULL may be represented by \N. See section LOAD DATA INFILE syntax


Go to the first, previous, next, last section, table of contents.