2

I have a table:

enter image description here

I need make UNIQUE NAME if CITY = NEW.city

enter image description here

Blue - success. Row inserted... Red - failed, cecause John with city = 1 exists.

Mike Doe
  • 16,349
  • 11
  • 65
  • 88
John John
  • 33
  • 7
  • How add unique key for name with condition? – John John Mar 16 '19 at 10:37
  • There's no such thing like a constraint with condition. You can add a unique index on columns `(name, city)`. – Mike Doe Mar 16 '19 at 10:37
  • 1
    Possible duplicate of [MySQL Trigger to prevent INSERT under certain conditions](https://stackoverflow.com/questions/2981930/mysql-trigger-to-prevent-insert-under-certain-conditions) – Mike Doe Mar 16 '19 at 10:39

1 Answers1

3

Add a unique key on NAME and CITY

ALTER TABLE `table_name`
ADD UNIQUE `unique_name_city` ( `NAME`, `CITY` );
izem
  • 413
  • 5
  • 7