For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > February 2006 > How do I set defaults?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How do I set defaults?
Tony Peardon

2006-02-08, 6:58 pm

Its simple, really. I've got a table called name which holds, you guessed
it, names. The fields are prefix, first, middle, last, and suffix. Only a
persons first name need be included, as all other fields can be null, but I
don't want them to be null by default. I want them set to an empty string by
default instead. Null will be used to signify that a person does not have
part of the name, while the empty string will signify only that the database
doesn't yet know the information. I know this is easy, but I'm doing
something wrong. Could someone please clear this up for me.

Here's a line of relevant code...

alter table name add column suffix varchar(5) null default('');

so what is the matter with that?

also, assuming that suffix already exists within table name, what would the
sql be to just add a default to it?



J.O. Aho

2006-02-08, 6:58 pm

Tony Peardon wrote:
> Its simple, really. I've got a table called name which holds, you guessed
> it, names. The fields are prefix, first, middle, last, and suffix. Only a
> persons first name need be included, as all other fields can be null, but I
> don't want them to be null by default. I want them set to an empty string by
> default instead. Null will be used to signify that a person does not have
> part of the name, while the empty string will signify only that the database
> doesn't yet know the information. I know this is easy, but I'm doing
> something wrong. Could someone please clear this up for me.
>
> Here's a line of relevant code...
>
> alter table name add column suffix varchar(5) null default('');


ALTER TABLE name ADD suffix VARCHAR(5) DEFAULT '';


> so what is the matter with that?
>
> also, assuming that suffix already exists within table name, what would the
> sql be to just add a default to it?


ALTER TABLE name MODIFY suffix VARCHAR(5) DEFAULT '';

http://dev.mysql.com/doc/refman/5.0/en/alter-table.html


//Aho
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com