I have two tables. In table_b, I want to reference two columns (not primary key) from table_a (as when I join the two tables, I want to join on both product_id and group_id). I also want the combo of those two keys to be unique. Is this foreign key essentially a composite key if I don't have the primary key id on table_b? Does it make sense for me to create this foreign key in table_b (even though its not referencing primary keys from table_a?
Does it make sense to have two constraints like below on table_b?
CREATE TABLE table_a(
id SERIAL PRIMARY KEY,
product_id VARCHAR, // can be null
group_id VARCHAR NOT NULL,
)
CREATE TABLE table_b(
id SERIAL PRIMARY KEY,
product_id VARCHAR NOT NULL,
group_id VARCHAR NOT NULL,
CONSTRAINT fk_key FOREIGN KEY(product_id, group_id) REFERENCES table_a(product_id, group_id)
CONSTRAINT unique_id UNIQUE (product_id, group_id);
)
Note that table_a product_id can be null, but not that of in table_b. All references from table_a in table_b will only include if product_id and group_id is not null. Thanks!