I have a table, say:
CREATE TABLE test ( id SERIAL UNIQUE, first integer NOT NULL, second integer NOT NULL, data text NOT NULL ); How do I enforce a check such that the second column is ‘unique’ with respect to the first column, but duplicate values are allowed? In other words, only allow one value in the ‘second’ for each of the ‘first’ column values. Once a value has been inserted into column 'first', only additional values that match the existing (first, second) column combination can be inserted again.
For example, given:
INSERT INTO test (first, second, data) VALUES (1, 2, 'test'); A constraint violation for:
INSERT INTO test (first, second, data) VALUES (1, 3, 'testing'); would occur, but
INSERT INTO test (first, second, data) VALUES (1, 2, 'testing'); or
INSERT INTO test (first, second, data) VALUES (2, 1, 'testing'); or
INSERT INTO test (first, second, data) VALUES (2, 2, 'testing'); would succeed.
Composite Primary key.. Like this?