tags:

views:

61

answers:

1

Hello, if i have a table which has columns with fixed lenght, Will mySQL count the backslashes as characters?

Example :

people(name[10],age[3])

If i store in the name column a MySQL escaped value like : Blahblah\'s Will MySQL cut it at the quote?

Thanks

+4  A: 

No, escape characters do not add the length of char or varchar string, because escape characters are not stored at all.

CREATE TABLE a (name char(5));

INSERT INTO a VALUES ('1234567890');
INSERT INTO a VALUES ('12\'345678');

SELECT * FROM a;
+-------+
| name  |
+-------+
| 12345 | 
| 12'34 | 
+-------+
2 rows in set (0.00 sec)
Daniel Vassallo
More info: it's not that MySQL doesn't "count" the backslash as a character. It doesn't store it at all. The backslash is just how you communicate with the database in SQL.
dkamins
@dkamins: excellent point; greatly improves the answer! Daniel must have been just like me, a bit peeved at Axel not trying things for himself, that he too only thought of answering by way of empiric evidence. Indeed, it's not a matter of "counting", but a matter of difference between the representation of the string in scripts and display versus the effective format of the storage. In scripts etc. we need the escape lest we ambiguously interpret the quote (is it part of the string or is it the quote used to end the string?). but for storage no such ambiguity is possible.
mjv
@mvj: Good point. Rephrased my answer to make it more accurate.
Daniel Vassallo