Simplify the construction of MATCH SIMPLEfk constraint behavior
If at least one column has a multi-column external constraint with MATCH SIMPLEdefault behavior NULL, the constraint is not met. You can build on this to greatly simplify your design.
CREATE SCHEMA test;
CREATE TABLE test.status(
status_id integer PRIMARY KEY
,sub bool NOT NULL DEFAULT FALSE
,UNIQUE (sub, status_id)
);
CREATE TABLE test.entity(
entity_id integer PRIMARY KEY
,status_id integer REFERENCES test.status
,sub bool
,additional_col1 text
,additional_col2 text
,FOREIGN KEY (sub, status_id) REFERENCES test.status(sub, status_id)
MATCH SIMPLE ON UPDATE CASCADE
);
NULL ( ):
BTW, :
refcolumn , reftable.
-:
INSERT INTO test.status VALUES
(1, TRUE)
, (2, TRUE)
, (3, FALSE);
INSERT INTO test.entity(entity_id, status_id, sub) VALUES
(11, 1, TRUE)
, (13, 3, FALSE)
, (14, 2, NULL)
, (15, 3, NULL)
SQL Fiddle ( ).
FK
(status_id, sub) status ( 2 status_id) fk:
CREATE TABLE test.status(
status_id integer
,sub bool DEFAULT FALSE
,PRIMARY KEY (status_id, sub)
);
CREATE TABLE test.entity(
entity_id integer PRIMARY KEY
,status_id integer NOT NULL
,sub bool NOT NULL
,additional_col1 text
,additional_col2 text
,FOREIGN KEY (status_id, sub) REFERENCES test.status
MATCH SIMPLE ON UPDATE CASCADE
);
INSERT INTO test.status VALUES
(1, TRUE)
(1, FALSE)
, (2, TRUE)
, (2, FALSE)
, (3, FALSE);
Etc.
:
, - dba.SE:
... , . . :