In your solution 1, you can force markets to reference only distributors this way:
CREATE TABLE Customers (
customer_id SERIAL PRIMARY KEY,
customer_type CHAR(1) CHECK (customer_type IN ('C', 'D')),
UNIQUE KEY (customer_id, customer_type)
);
CREATE TABLE Markets (
market_id SERIAL PRIMARY KEY,
customer_id INT NOT NULL,
customer_type CHAR(1) CHECK (customer_type = 'D'),
FOREIGN KEY (customer_id, customer_type)
REFERENCES Customers (customer_id, customer_type)
);
A foreign key can reference either a primary key or a unique key in the referenced table.
However, note that if you have distributor-specific attributes that are irrelevant to non-distributors, putting them in the Customers
table would violate Third Normal Form. That is, the relevance of these distributor attributes would depend on the value in customer_type
which is not part of the primary key. In Third Normal Form, every attribute must depend on nothing but the primary key.
For this reason, I would choose the second solution, making Distributors
a child table, referencing Customers
. Put distributor-specific attributes into the Distributors
table. Then the Markets
table can reference Distributors
more simply.