[FIXED] Postgres: complex foreign key constraints

Issue

I have this schema

CREATE TABLE public.item (
    itemid integer NOT NULL,
    itemcode character(100) NOT NULL,
    itemname character(100) NOT NULL,
    constraint PK_ITEM primary key (ItemID)
);
create unique index ak_itemcode on Item(ItemCode);

CREATE TABLE public.store (
    storeid character(20) NOT NULL,
    storename character(80) NOT NULL,
    constraint PK_STORE primary key (StoreID)
);

CREATE TABLE public.storeitem (
    storeitemid integer NOT NULL,
    itemid integer NOT NULL,
    storeid character(20) NOT NULL,
    constraint PK_STOREITEM primary key (ItemID, StoreID),
    foreign key (StoreID) references Store(StoreID),
    foreign key (ItemID) references Item(ItemID)
);

create unique index ak_storeitemid on StoreItem (StoreItemID);

And here is the data on those tables

insert into Item (ItemID, ItemCode,ItemName)
 Values (1,'abc','abc');
insert into Item (ItemID, ItemCode,ItemName)
 Values (2,'def','def');
insert into Item (ItemID, ItemCode,ItemName)
 Values (3,'ghi','ghi');
insert into Item (ItemID, ItemCode,ItemName)
 Values (4,'lmno','lmno');
insert into Item (ItemID, ItemCode,ItemName)
 Values (5,'xyz','xyz');

insert into Store (StoreID, StoreName)
 Values ('B1','B1');

insert into StoreItem (StoreItemID, StoreID, ItemID)
 Values (1,'B1',1);
insert into StoreItem (StoreItemID, StoreID, ItemID)
 Values (2,'B1',2);
insert into StoreItem (StoreItemID, StoreID, ItemID)
 Values (3,'B1',3);

Now I created this new table

CREATE TABLE public.szdata (
    storeid character(20) NOT NULL,
    itemcode character(100) NOT NULL,
    textdata character(20) NOT NULL,
    constraint PK_SZDATA primary key (ItemCode, StoreID)
);

I want to have the foreign key constraints set so that it will fail when you try to insert record which is not in StoreItem. For example this must fail

insert into SZData (StoreID, ItemCode, TextData)
 Values ('B1', 'xyz', 'text123');

and this must pass

insert into SZData (StoreID, ItemCode, TextData)
 Values ('B1', 'abc', 'text123');

How do I achieve this without complex triggers but using table constraints? I prefer solution without triggers. SZData table is just for accepting input from external world and it is for single purpose. Also database import export must not be impacted


Solution

I figured out having a function to execute on constraint will solve this issue. The function is_storeitem does the validation. I believe this feature can be used for even complex validations

create or replace function is_storeitem(pItemcode nchar(40), pStoreId nchar(20)) returns boolean as $$
select exists (
    select 1
    from public.storeitem si, public.item i, public.store s
    where si.itemid = i.itemid and i.itemcode = pItemcode and s.Storeid = pStoreId and s.storeid = si.storeid
);
$$ language sql;


create table SZData
(
    StoreID             NCHAR(20)              not null,
    ItemCode            NCHAR(100)             not null,
    TextData            NCHAR(20)              not null,
    constraint PK_SIDATA primary key (ItemCode, StoreID),
    foreign key (StoreID) references Store(StoreID),
    foreign key (ItemCode) references Item(ItemCode),
    CONSTRAINT ck_szdata_itemcode CHECK (is_storeitem(Itemcode,StoreID))
);

This perfectly works with postgres 9.6 or greater.



Answered By - Ramanan T
Answer Checked By - David Goodson (sql.easybugfix Volunteer)

Related Posts

Post a Comment