views:

60

answers:

3

If you try to create a TEXT column on a table, and give it a default value in MySQL, you get an error (on Windows at least). I cannot see any reason why a text column should not have a default value. No explanation is given by the MySQL documentation. It seems illogical to me (and somewhat frustrating, as I want a default value!). Anybody know why this is not allowed?

+2  A: 

Without any deep knowledge of the mySQL engine, I'd say this sounds like a memory saving strategy. I assume the reason is behind this paragraph from the docs:

Each BLOB or TEXT value is represented internally by a separately allocated object. This is in contrast to all other data types, for which storage is allocated once per column when the table is opened.

It seems like pre-filling these column types would lead to memory usage and performance penalties.

Pekka
A: 

Are you sure you want a TEXT column, not a VARCHAR one? TEXT columns are for things which can become more than 255 bytes long.

Scytale
This should be a comment. Also,yes, he does mean `TEXT` - those columns can't have a default value. `VARCHAR` can.
Pekka
I know what he _means_, I was asking whether he really _needs_ a TEXT column. Because if he doesn’t, using (VAR)CHAR instead would be a plausible solution. This was why I’ve decided to post this as an answer. No need to vote it down (and leave an answer not talking about TEXT colums at all at 0).
Scytale
Yes, I need more than 255 characters unfortunately.
Russ
A: 

What exactly are you doing? A quick test here showed that a text column can quite happily have a default value:

    mysql> create table test (a char(32) not null default 'this works', b char(32) not null);
    Query OK, 0 rows affected (0.03 sec)
    mysql> insert into test (b) values('hello');
    Query OK, 1 row affected (0.01 sec)
    mysql> select * from test;

+------------+-------+
| a          | b     |
+------------+-------+
| this works | hello |
+------------+-------+
1 row in set (0.00 sec)
David Knell
Ah - I'd not read 'text column' to mean, literally, a TEXT column..
David Knell
Sorry to confuse you - I have modified the question now to put TEXT in upper case, for clarity.
Russ