clear markdown feedback
clear markdown feedback
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 3799399 fiddles created (41686 in the last week).

CREATE TABLE tbl ( id_medico int , fecha_inicial timestamp , fecha_final timestamp , EXCLUDE USING gist (id_medico WITH =,tsrange(fecha_inicial, fecha_final) WITH &&) -- no overlap ); INSERT INTO tbl (id_medico,fecha_inicial, fecha_final) VALUES (1, '2021-04-22 08:00:00', '2021-04-22 10:00:00') , (1, '2021-04-22 13:00:00 ', '2021-04-22 14:00:00') ;
2 rows affected
 hidden batch(es)


-- Works INSERT INTO tbl (id_medico,fecha_inicial, fecha_final) VALUES (2,'2021-04-22 08:00:00', '2021-04-22 10:00:00'); TABLE tbl;
1 rows affected
id_medico fecha_inicial fecha_final
1 2021-04-22 08:00:00 2021-04-22 10:00:00
1 2021-04-22 13:00:00 2021-04-22 14:00:00
2 2021-04-22 08:00:00 2021-04-22 10:00:00
 hidden batch(es)


-- Fails INSERT INTO tbl (id_medico,fecha_inicial, fecha_final) VALUES (1, '2021-04-22 07:00:00', '2021-04-22 09:00:00');
ERROR: conflicting key value violates exclusion constraint "tbl_id_medico_tsrange_excl" DETAIL: Key (id_medico, tsrange(fecha_inicial, fecha_final))=(1, ["2021-04-22 07:00:00","2021-04-22 09:00:00")) conflicts with existing key (id_medico, tsrange(fecha_inicial, fecha_final))=(1, ["2021-04-22 08:00:00","2021-04-22 10:00:00")).
 hidden batch(es)