views:

190

answers:

2

Can MySQL VARCHAR's store any char like nulls and newlines?

+5  A: 

MySQL can store any chars, but depending on the specified encoding, some characters may be misrepresented if different from your display encoding.

Varchars can contain any chars, even 0x00, since its not a null terminated string, but a length specified string, where the length is specified at the beginning of the string as a 2 or 4 byte value.

I'm not sure about Char columns, but I'm guessing they are null terminated in the sense that the first 0x00 indicates end of string (though storage still takes up the full length).

If you want to store true byte data though, you should use the BLOB type. That is also a length specified type, which is guaranteed to contain whatever data you put into it, and is not affected by encodings at all.

Tor Valamo
+2  A: 

You should probably use the BINARY and VARBINARY data types for storing strings of non-printable characters, especially if you want strings of single bytes instead of actual text characters (which can be multi-byte).

Bill Karwin