chew socks
chew socks

Reputation: 1446

SQL logic for multiple column uniqueness

Given

CREATE TABLE table (a TEXT, b TEXT, c TEXT UNIQUE (a, b) );

does this constraint mean a OR b must be unique or a AND b?

Upvotes: 0

Views: 50

Answers (2)

Yogesh Sharma
Yogesh Sharma

Reputation: 50163

This is your table definition :

CREATE TABLE table (a TEXT, b TEXT, c TEXT UNIQUE (a, b) );

Your table definition has composite unique key UNIQUE (a, b) which means a and b should not be duplicate or you can also say unique pair of a and b. However, NULL can be ignore.

Below sample data illustrate this :

 a    b   c (doesnt matter)
 -----------
'a'  'b' --- valid
'a'  'b' --- will throw error
'b'  'a' --- valid

Upvotes: 1

The Impaler
The Impaler

Reputation: 48780

A and B. The following combinations are valid:

A   B
--- ---
abc def
abc ghi -- 'abc' again, but with different B column
cde ghi -- 'ghi' again, but with different A column

But you cannot add again:

abc def

Upvotes: 2

Related Questions