Phate
Phate

Reputation: 6612

How to set a constraint on a group of values?

I already have an id as primary key so I can't use this construct. I have a table with 5 fields, I'd like to not have more than one row with the same values of field1 field2 and field3.

That is, suppose I have A,B,C,D,E as field values (+1 id column)

Rows like;

1 2 3 4 5

A B C F A (row1)

A B E F B (row 2)

are allowed as field 3 is different among rows (C in row1 and E in row 2).

But a row

A B C E B (row 3)

should not be allowed, as field 1 2 and 3 are exactly the same of row 1.

primary key(field1,field2,field3) would solve my problem, but I'd like for it to be an id and so I can't use it.

Upvotes: 0

Views: 40

Answers (1)

CL.
CL.

Reputation: 180060

There are more constraint types than PRIMARY KEY.

Use a UNIQUE constraint:

CREATE TABLE MyTable(
    ID PRIMARY KEY,
    Field1,
    Field2,
    Field3,
    [...],
    UNIQUE (Field1, Field2, Field3)
);

Upvotes: 1

Related Questions