'I want to create unique constraint based on check input start date should be greater than exits end date in db and need to apply same for jpa java

create or replace function IsUnique (i_id smallint, i_clg_name character, i_pin_code character, i_start_time time) returns bigint as $$
begin
  if exists (
    select *
    from college_list as tt
    where tt.id = i_id and tt.clg_name = i_clg_name 
    and tt.pin_code = i_pin_code  and tt.end_time > i_start_time) then return 0
  else 
    then return 1 -- Unique
end if;
end
$$ LANGUAGE PLPGSQL;

alter table college_list add constraint unique_key check (IsUnique(id ,clg_name ,pin_code ,start_time time) = 1)
go



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source