CREATE UNIQUE INDEX idx_twocols ON t1t2(t1_id, t2_id)
You probably need to add NOT NULL to the declarations for each of the two columns.
Alternatively, you can discard the primary key column (if you all use it for uniqueness) and create a primary key in a combination of t1_id and t2_id :
CREATE TABLE t1t2( t1_id integer NOT NULL, t2_id integer NOT NULL, PRIMARY KEY (t1_id, t2_id), foreign key(t1_id) references t1(id), foreign key(t2_id) references t2(id));
PRIMARY KEY - a special case of a UNIQUE index. Using a composite PRIMARY KEY saves one column and one index, but requires your application to know both t1_id and t2_id to retrieve one row from the table.
Larry lustig
source share