SQL for Smarties | SQL Programming Style | Trees and Hierarchies in SQL | SQL Puzzles and Answers | Data and Databases


Monday, April 10, 2006

varchar(MAX)

SQL Apprentice Question
I need help understanding varchar(MAX).

I really don't see the difference if I use varchar(50) in a field that will
be truncated anyway when the field contains only 40 characters. So what do I
do when that field needs 51 characters? Hence why not use varchar(MAX) when
one does not know what field length can become in time?

Celko Answer
>>I really don't see the difference if I use varchar(50) in a field [sic] that will
be truncated anyway when the field [sic] contains only 40 characters.
<<

If you always truncate this column (NOT field!), why did you make it
too long? All you have done is make maintaining the code harder and
invite a loss of data integrity.

>> So what do I do when that field [sic] needs 51 characters? Hence why not use varchar(MAX) when one does not know what field [sic] length can become in time? <<

Let's get back to the basics of an RDBMS. Rows are not records; fields
are not columns; tables are not files. If you keep using the wrong
words and concepts, you will never.

I also see that you use the "Magical" VARCHAR(50) declaration that is
so popular among ACCESS programmers who have not learned SQL yet :)

Part of design is in picking the proper data types and constraints.
You should know what the max size for a VARCHAR(n) column is. If you
don't know. then you are storing free text in SQL; it was never meant
for that purpose. You want a document system.

I bet that you do not research on your schema's requirements and have
been blindly using a default VARCHAR(50) declaration because it is
quick and easy. Stop that.

No comments: