Subject Re: [firebird-support] Possible bug with CHECK constraint
Author Helen Borrie
At 09:50 PM 2/10/2004 +0000, you wrote:


>CREATE DATABASE 'test.fdb' USER 'sysdba' PASSWORD 'masterkey';
>
>CREATE TABLE test_master (
> id INTEGER NOT NULL PRIMARY KEY,
> CHECK(COALESCE((SELECT max(id) FROM test_master), 0) < 10)
>);
>
>COMMIT;
>
>INSERT INTO test_master VALUES (1);
>INSERT INTO test_master VALUES (20);
>
>COMMIT;
>
>This script succeeds. Is it normal?

Yes - the check is done before the insert. It should succeed with this
script and fail next time you insert any *value* at all into id.

Try:
insert into test_master values(3);

ISC ERROR CODE:335544558
Operation violates CHECK constraint INTEG_17 on view or table TEST_MASTER

If you really want this check, try this:

CREATE TABLE test_master1 (
id INTEGER NOT NULL PRIMARY KEY,
CHECK(COALESCE(((SELECT max(id) FROM test_master1) + new.id), 0) < 10)
);
COMMIT;
INSERT INTO test_master1 VALUES (1);
INSERT INTO test_master1 VALUES (20);
COMMIT;


./heLen