> I've just been wondering if the length parameter of a VARCHAR column has
> any effect on storage efficiency or space requirements. Afaik, VARCHAR
> columns only store the amount of data actually written into them and
> require no significantly more memory. So to be especially flexible with
> a particular table column, could I just define it VARCHAR(255) and face
> no further disadvantage of it?

mysql> CREATE TABLE vc (
    ->   vc1 VARCHAR(5),
    ->   vc2 VARCHAR(255)
    -> );
Query OK, 0 rows affected (0.16 sec)

mysql> INSERT INTO vc (vc1, vc2) VALUES
    ->   ('this is a test', 'this is another, longer test');
Query OK, 1 row affected, 1 warning (0.06 sec)

mysql> SELECT * FROM vc;
+------+------------------------------+
| vc1  | vc2                          |
+------+------------------------------+
| this | this is another, longer test |
+------+------------------------------+
1 row in set (0.00 sec)

>From this, we can see how defining the field as VARCHAR(5) limits the
maximum length to 5 characters; we can assume, too, that it will
likewise chop off any strings longer than 255 characters in vc2 the
same way. The length parameter simply provides the upper limit of the
string that might be stored in that field, useful in some instances,
irrelevant in others.

All VARCHARs/TINYTEXTs are stored with a single-byte length prefix,
regardless of how long you let them be (less than 256, of course),
plus the string it's storing. So, for maximum flexibility less than
256 characters, use VARCHAR(255) and don't worry about it.

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to