Red icon indicating copy to clipboard operation
Red copied to clipboard

CHECK constraints

Open federico-razzoli opened this issue 6 years ago • 6 comments

CREATE TABLE t (
    birth_date INT,
    death_date INT,
    [ CONSTRAINT chk_a_b ] CHECK (birth_date < death_date OR death_date IS NULL)
);

The CONSTRAINT part is optional and is useful only to specify a name. Names are useful because they appear in error messages.

federico-razzoli avatar Mar 01 '19 22:03 federico-razzoli

::?CLASS.^add-constraint-check: { .birth-date < .death-date OR not .death-date.defined };

Or

::?CLASS.^add-constraint-check: chk_a_b, { .birth-date < .death-date OR not .death-date.defined };

Sent with GitHawk

FCO avatar Mar 01 '19 22:03 FCO

Should it warn when using a driver of database that do not support CHECK

Sent with GitHawk

FCO avatar Mar 01 '19 22:03 FCO

I was going to write "or. like SQLite, accept the check definition but don't actually do the check" only to discover that it actually does, which was somewhat of a pleasant surprise.

jonathanstowe avatar Mar 02 '19 08:03 jonathanstowe

SQLite do use it? I’m surprised! :)

Sent with GitHawk

FCO avatar Mar 02 '19 09:03 FCO

Apparently https://www.sqlite.org/lang_createtable.html I'm of the habit of just assuming that for most things it will accept the definition but just not do anything with it.

jonathanstowe avatar Mar 02 '19 09:03 jonathanstowe

No actually it works as it should in SQLite. In MySQL prior to 8.0 and in MariaDB prior to 10.3 (or something like that) the CHECKs are not enforced.

federico-razzoli avatar Mar 03 '19 23:03 federico-razzoli