How do I make it not add duplicates? I want to make it check by all other columns beside ID.
I want this to be invalid.
ID col1 col2 col3
1 first middle last
ID col1 col2 col3
2 first middle last
I want this to be valid.
ID col1 col2 col3
1 first middle last
ID col1 col2 col3
2 first last middle
You need to use a composite UNIQUE INDEX on all three columns. See this example table definition:
CREATE TABLE example (
ID INT PRIMARY KEY AUTO_INCREMENT,
col1 VARCHAR(32) NOT NULL,
col2 VARCHAR(32) NOT NULL,
col3 VARCHAR(32) NOT NULL,
UNIQUE(col1, col2, col3)
);
-- expected to be valid
INSERT INTO example (ID, col1, col2, col3) VALUES
(NULL, 'first', 'middle', 'last'),
(NULL, 'first', 'last', 'middle');
-- expected to be invalid
INSERT INTO example (ID, col1, col2, col3) VALUES
(NULL, 'first', 'middle', 'last'),
(NULL, 'first', 'middle', 'last');
DEMO @ SQL Fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With