Is it possible to set constraints for VARCHAR columns in MySQL?
I would like to not permit more than one row of a specific value.
______________
| MyTable |
--------------
| pk id INT |
| name VARCHAR |
If I now inserted two rows with the same name, “peter”, I would like to throw an error. So, the constraint should only check if there is already a “peter” stored. But multiple rows of other values have to work.
INSERT INTO MyTable VALUES (1, "peter");
INSERT INTO MyTable VALUES (2, "peter"); // This should fail
INSERT INTO MyTable VALUES (3, "steven");
INSERT INTO MyTable VALUES (4, "steven"); // Shall succeed
Is it possible in some way in MySQL to check for specified values?
no, thats not possible using constraints. you could add an unique constraint on
name, but that would prevend all duplicate entrys, not just “peter”. with that said, you’ll have to do this additional check on your own.