r/SQL • u/arib510 • Jul 09 '26
MySQL [MySQL] Unique constraint on multiple columns isn't working
I have a table that and I want to limit to ensure no duplicate rows get inserted. There's no primary key (because of how the data comes in), so I thought I could add a UNIQUE constraint including multiple columns. I have too many columns to include every single one, so I just included the ones I believed would be most likely to change.
The constraint I added looks like this:
ALTER TABLE table
ADD CONSTRAINT no_dupe_rows
UNIQUE(col1, col2, col3, col4)However, when I test it, it still allows me to insert the same data multiple times, with statements such as the following:
INSERT INTO table(col1, col2, col3, col4)
VALUES (val1, val2, val3, val4)I can run that multiple times and it adds a new row each time even with the UNIQUE constraint in place. What can I do to fix this?