[thelist] mySql question...

rudy rudy937 at rogers.com
Thu Aug 28 06:36:15 CDT 2003

> I do an INSERT on f1, but don't give f2 anything, I want the db to
> auto-magically copy the data from f1 and insert it into f2.

no, not possible, not exactly like that

heck, in mysql, you cannot even use a function as a default...

   "Default values must be constants. This means, for example, that
    you cannot set the default for a date column to be the value of a
    function such as NOW() or CURRENT_DATE."

however, all is not lost

the first solution is to actually give f2 the value of f1

thus, when you insert, and you know you're not going to have a value for f2,
just give it the value you gave f1

  insert into yourtable (f1, f2)  values ('foo', f2)

this is, i believe, non-standard syntax

the standard way would be

  insert into yourtable (f1, f2)  values ('foo', 'foo')

another solution is simply to let the field go null if you do not provide a

  insert into yourtable (f1)  values ('foo')

then when you query it, use coalesce

   select f1, coalesce(f2, f1) as f1value ...

this means if f2 is null, you'll get the f1 value, unless of course it too
is null, in which case you'll get null


More information about the thelist mailing list