Click here to Skip to main content
16,020,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Database where one table records the stock of items. On purchase of items, the requisite amount is added to stock while on sale, requisite amount is deducted.

The columns of the table is as follows:

C#
Item(F.key)----Qty----Date----IsCurrent


The item attribute is a foreign key to the items table. Quantity shows the stock at the given date.

IsCurrent is a boolean that indicates whether the given stock is the most current one.

So, at any given time, for any particular item, there can be only one record with IsCurrent set to true.

What I have tried:

I have used an index:

CREATE UNIQUE INDEX onlyonecurrent_index
    ON mycompany.stock USING btree
    (item COLLATE pg_catalog."default")
    TABLESPACE pg_default    WHERE iscurrent
;



This takes care of the constraint. However, it means inserting records to this table is a hassle. I have to find the current stock, clear the IsCurrent flag and then enter the new entry.

I have thought of two ways to correct the problem. I can maintain two tables, one StockHistory and another CurrentStock, where the archived data will be put in StockHistory.

Or, I can simply remove the IsCurrent flag and sort the items by date to get the most current entry.

However I am not fond of any of those approaches.

Is there any tried and tested way to handle a problem like this?
Posted
Updated 7-Mar-17 21:30pm
Comments
PIEBALDconsult 3-Mar-17 20:29pm    
Well, generally (in my opinion anyway), such things are the domain of the application, not the database schema.
I prefer not to have an "iscurrent" column, but use start and end dates for the record's applicability.
Leave the end date NULL or set to the far future to indicate "current".
That makes several types of query much easier.
Peter Leow 3-Mar-17 23:49pm    
Isn't the stock with the latest date is the most current? Can't you use the date field in that table for this purpose?
Sabyasachi Mukherjee 4-Mar-17 6:01am    
I thought of that approach, but there can be many sales on one day, in which case the latest stock can become confusing.
Peter Leow 4-Mar-17 6:27am    
Every stock and sales entries to the database tables should be accompanied with their respective dates and times, why is it confusing?

correction to prev post - Unique constraint should be on (item, iscurrent).
 
Share this answer
 
Method A
You can define column 'IsCurrent' of table 'STOCK' as NULLable and define a UNIQUE constraint on the column.
Whenever you insert a record, generate 2 SQLs:
(1)
update STOCK set IsCurrent = NULL where item = <item_key>
(2)
insert into STOCK values (<item_key>, <qty>, <tran_date>, TRUE)

Since UNIQUE constraint ignores NULLs this should work

Method B
Alternatively generate a unique TRANSACTION key (maybe Database Sequence) for every transaction and add columns in both ITEM and STOCK Tables to store this value. Again you will need to generate 2 SQLs:
(1)
insert into STOCK values (<item_key>, <qty>, <tran_date>, <trans_key>)
(2)
update ITEM set LATEST_TRAN_KEY = <trans_key> where item_key = <item_key>

Needless to say you can identify the latest record 'where stock.tran_key = item.latest_tran_key'

(Of course you cannot define a Foreign key constraint for this column ITEM.Latest_Tran_Key as you have already defined a foreign key for column ITEM in STOCK table)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900