shelbypereira
shelbypereira

Reputation: 2245

MySQL enforce uniqueness on year part of date

Is it possible to enforce uniqueness in MySQL on a pair of columns X,Y where X is Date and Y is Integer. I need uniqueness to apply to Year(X),Y. using UNIQUE it is easy to do this on the pair of columns, but I don't see how I can add the constraint by extracting the Year part of the date.

Upvotes: 0

Views: 71

Answers (1)

David Stokes
David Stokes

Reputation: 120

If you are running MySQL 5.7 you could use generated tables to concatinate two columns together and define the generated column as unique.

CREATE TABLE unq (mydate DATE, myint INT, mydateint CHAR(30) AS (CONCAT(mydate,myint)) UNIQUE);

Upvotes: 1

Related Questions