1

I have simple table which looks like below one:

id   info   user     start_date            end_date              resource_id
31   NULL   aorlik   2018-01-04 08:00:00   2018-01-04 10:00:00   1
32   NULL   aorlik   2018-01-04 15:00:00   2018-01-04 17:00:00   1

and now I'm looking for simple way for make column start_date as unique with dependency with column resource_id. I would like to make some kind of protection to avoid set same start_date twice. It should allow only for set the same time record for new resource_id. I suppose that's very easy but I have got stuck :/ I use MySQL and mysql-workbench.

Armin Orlik
  • 195
  • 2
  • 14
  • add unique key to your table – Bibin Mathew Jan 09 '18 at 10:40
  • Did you try searching at all? _"I have got stuck"_ does not explain to readers what you already tried and what they should/shouldn't tell you in response. Example duplicate: [How do I enforce uniqueness in a table?](https://stackoverflow.com/questions/10432831/how-do-i-enforce-uniqueness-in-a-table) – underscore_d Jan 09 '18 at 10:41
  • @BibinMatthew yes I was trying with unique key and search for good answer before I have asked my question. Have a nice day dude! – Armin Orlik Jan 09 '18 at 10:50

1 Answers1

2

You need to apply a constraint, in this case a unique constraint:

ALTER TABLE simple_table -- this defines the table to be altered
ADD UNIQUE -- the constraint type
start_resource(start_date,resource_id);  -- constraint_name(columns used, comma separated)
JohnHC
  • 10,935
  • 1
  • 24
  • 40