By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE test ( entityvalue INT,
entitydate DATETIME,
additional_column INT AS (CASE WHEN entitydate >= '2020-01-01 00:00:00'
THEN entityvalue
ELSE NULL END),
UNIQUE ( additional_column ) );
INSERT INTO test VALUES
(123, '2019-01-01 00:00:00', DEFAULT),
(456, '2021-01-01 00:00:00', DEFAULT);
SELECT * FROM test;
entityvalue | entitydate | additional_column |
---|---|---|
123 | 2019-01-01 00:00:00 | null |
456 | 2021-01-01 00:00:00 | 456 |
-- Try to insert duplicate by `entityvalue` before '2020-01-01 00:00:00'
INSERT INTO test VALUES (123, '2019-01-01 00:00:00', DEFAULT);
-- success
SELECT * FROM test;
entityvalue | entitydate | additional_column |
---|---|---|
123 | 2019-01-01 00:00:00 | null |
456 | 2021-01-01 00:00:00 | 456 |
123 | 2019-01-01 00:00:00 | null |
-- Try to insert duplicate by `entityvalue` after '2020-01-01 00:00:00'
INSERT INTO test VALUES (456, '2021-01-01 00:00:00', DEFAULT);
-- fail
SELECT * FROM test;
Duplicate entry '456' for key 'test.additional_column'