Re: how to set a DEFAULT value !!




-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message


Which database server is this?

This is definitely the behavior of MySQL, PostgreSQL, and Oracle,
and I'm pretty sure most others follow it as well.

And, can you omit the name of the column you want defaulted, and does this
server then insert the default value?

Yes. One way to think about this is to realize that *all* columns have
an automatic default of NULL, we are just changing it to something a
little more useful:

CREATE TABLE foo (
a int,
b int DEFAULT 22,
c int DEFAULT stockprice('RHAT')
);

is completely identical to:

CREATE TABLE foo (
a int DEFAULT NULL,
b int DEFAULT 22,
c int DEFAULT stockprice('RHAT')
);


-- A PostgreSQL example.
-- stockprice() is a pl/perl function that returns the real-time value
-- (in cents) of RedHat stock via a web service.

INSERT INTO foo(b) VALUES (14);

SELECT * FROM foo;

a | b | c
---+----+------
| 14 | 3025

-- We triggered the DEFAULT values of both a and c because we did
-- not specify them


INSERT INTO foo(a,b,c) VALUES (7,DEFAULT,47);

SELECT * FROM foo;

a | b | c
---+----+------
| 14 | 3025
7 | 22 | 47

-- We told b to use its default value explicitly


INSERT INTO foo(a,b,c) VALUES (DEFAULT,DEFAULT,NULL);

SELECT * FROM foo;
a | b | c
---+----+------
| 14 | 3025
7 | 22 | 47
| 22 |


-- We told a and b to use their default values, and set c explicitly


It's late here, so hope that made sense. :)

--
Greg Sabino Mullane greg@xxxxxxxxxxxx
PGP Key: 0x14964AC8 200604270030
http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8
-----BEGIN PGP SIGNATURE-----

iD8DBQFEUEo0vJuQZxSWSsgRAmAYAKDcMYGCUfTkpsVBGvTDr+rD1sjf/gCdGvYI
lpjCTQT14ynvtN2LOV++rLs=
=ww4D
-----END PGP SIGNATURE-----


.



Relevant Pages

  • Re: [C] strcat() question (ongoing)
    ... > At the machine level, yes, basically (depending on your definition ... We can only "PUT" data in / or assign data to variables such as an int or ... foo is an object? ... > house and look on the microwave. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: call of variadic function
    ... arguments that should be passed to this function are of type int. ... You call foo with more arguments than are ... which is the standard way to access arguments of a variadic function ... Here the else clause of my sentence specifies one of the numerous non- ...
    (comp.lang.c)
  • Re: RISC OS modules with stock gcc?
    ... int bar; ... DCD &ff000004 ... IMPORT bar ... EXPORT foo ...
    (comp.sys.acorn.programmer)
  • Re: call of variadic function
    ... arguments that should be passed to this function are of type int. ... call and the definition of function foo itself does NOT produce any ... which is the standard way to access arguments of a variadic function ... Here the else clause of my sentence specifies one of the numerous non- ...
    (comp.lang.c)
  • Re: Dive Into Java?
    ... So it acts as a casting operator. ... class Foo { ... int _arg; ... Foo(int arg) { ...
    (comp.lang.python)