-1

I have LAMP server hosting a local website for my firm where I need to enter data each month.

The data is values from electricity meters, which are measuring energy consumption in every individual office. I need to insert a unique set of data each month, no duplicates in the same month. How can I do that?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Neven Dary
  • 35
  • 6
  • yes. Firstly look into setting unique constraints on the fields on your database. Possibly a compound primary key. Although the requirement sounds dubious to me. Surely if the office was shut for some time, and used zero power, a duplicate (same meter value, I presume you mean?) later in the month would be ok, in theory? Or you mean not sending two readings on one day? Or something else? – ADyson Sep 25 '17 at 15:54
  • Possible duplicate of https://stackoverflow.com/questions/635937/how-do-i-specify-unique-constraint-for-multiple-columns-in-mysql – Don't Panic Sep 25 '17 at 16:03

2 Answers2

0

If your data is sorted in database you have to select the last item and compare it with you inputted data if it's not equal insert it with query. Else show a message that the date is wrong.

0

I would create a computed column on YYYY-mm part of the date and add a unique index on it, e.g.:

ALTER TABLE <your_table> ADD yyyymm as DATE_FORMAT(date, '%Y-%m');
CREATE UNIQUE INDEX idx_yourtable_office_id ON your_table(office_id, yyyymm);
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102