D
D
des1roer2015-08-21 08:51:46
PostgreSQL
des1roer, 2015-08-21 08:51:46

Is it possible to generate a unique key by month and year?

is it possible to create a unique key to prohibit recording if there is already data for such a month?
something like unique constraint with to_char(timestamp,'YYYY-MM')
--
UPD
this way I check that multiple fields are not the same

CONSTRAINT uniq UNIQUE("F_ActualAnaliz_ID", "F_DateAnaliz", "F_User_ID"),

but is it possible
CONSTRAINT uniq UNIQUE(to_char(f_DateAnaliz, 'YYYY-MM')),

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Melkij, 2015-08-21
@des1roer

In Postgresql, yes, you can: www.postgresql.org/docs/9.4/static/indexes-express... You
can build an index for any immutable function, including a custom one.
Only better not to_char, but date_trunc('month', f_DateAnaliz). The index will be more compact.

V
Valentine, 2015-08-21
@gephaest

Yes, you can, moreover, the selection on this field will work faster because you are creating an index.

D
Dmitry Kovalsky, 2015-08-21
@dmitryKovalskiy

A unique key in general can be anything. Though a bit field. Problems can start when for some reason you want to add a second entry with the same key and you have to come up with a composite key. Immediately there will be a problem of backward compatibility and data migration.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question