sql - database data structure using powers of two -
i'm designing data structure , wanted know if missing doing way.
lets have column day of type int.
1 : monday 2 : tuesday 4 : wednesday 8 : thursday 16 : friday 32 : saturday 64 : sunday
if wanted store monday , friday input 17 day column. if wanted store tuesday , wednesday enter 6 etc.
is valid way of storing data. how query if wanted select record contained saturday , variation of days, or saturday not wednesday. possible? fast?
what concept called?
some people may tell code 'smell' because represents denormalisation, think valid use of bit-mask field:
-- contains saturday , other combination of days select * table (daybitcolumn & 32) = 32 -- contains saturday , other combination of days, except wednesday select * table (daybitcolumn & 32) = 32 , (daybitcolumn & 4) = 0
edit: pointed out @andriy m, can written more succinctly as:
select * table (daybitcolumn & 36) = 32
['&' bitwise and]
Comments
Post a Comment