Subject Re: [firebird-support] Server defaults - what are they?
Author Lucas Franzen
Barry,

> It seems that in order to understand various
> application/component/sql requirements and
> behaviors, I need to understand something
> about "server defaults."

It's quite easy.

CREATE TABLE TEST (
TEST_ID BIGINT NOT NULL,
TEST_DEFAULTED VARCHAR(20) DEFAULT 'DEFAULT VALUE',
TEST_NOT_DEFAULTED VARCHAR(20),
CONSTRAINT PK_TEST PRIMARY KEY ( TEST_ID )
);

If you do inserts like:

INSERT INTO TEST
VALUES ( 1, '', '' );
INSERT INTO TEST
VALUES ( 2, NULL, NULL );

INSERT INTO TEST ( TEST_ID, TEST_DEFAULTED )
VALUES ( 3, '' );
INSERT INTO TEST ( TEST_ID, TEST_DEFAULTED )
VALUES ( 4, NULL );

INSERT INTO TEST ( TEST_ID, TEST_NOT_DEFAULTED )
VALUES ( 5, '' );
INSERT INTO TEST ( TEST_ID, TEST_NOT_DEFAULTED )
VALUES ( 6, NULL );

you'll get these results: ('' indicates empty string)
TEST_ID TEST_DEFAULTED TEST_NOT_DEFAULTED
1 '' ''
2 <NULL> <NULL>
3 '' <NULL>
4 <NULL> <NULL>
5 DEFAULT VALUE ''
6 DEFAULT VALUE <NULL>

So, the dafault value will only be applied when the field itself is NOT
part of your insert statement! (So if you have queries like "SELECT *
FROM TEST", you'll have to check the components you use how they can
handle default values (on the client side)).
Even passing NULL will not cause the default value being applied!
(NULL isn't the absence of a value, it represents an unknown value).

And DEFAULT VALUES do ONLY apply when you're inserting.
Never on update.

This isn't Firebird specific, this is SQL standard.

Hth
Luc.