jbc25
jbc25

Reputation: 1202

primary key in a SQLite table made of two columns

I would like to make a primary key in a SQLite table made of two columns which work like this:

I read a lot but all question I found told how to make two independent primary keys, but it is not my case.

Upvotes: 0

Views: 246

Answers (1)

user647772
user647772

Reputation:

Yes, you can do this.

sqlite> create table t (f1 integer, f2 integer);
sqlite> create unique index i12 on t (f1, f2);
sqlite> .schema
CREATE TABLE t (f1 integer, f2 integer);
CREATE UNIQUE INDEX i12 on t (f1, f2);
sqlite> insert into t values (1,2);
sqlite> insert into t values (1,3);
sqlite> insert into t values (2,2);
sqlite> insert into t values (1,2);
Error: columns f1, f2 are not unique

See the reference of CREATE INDEX.

Upvotes: 2

Related Questions