Middle
Is a linking table needed between users and promo_codes?
sobes.tech AI
Answer from AI
A linking table between users and promo_codes is needed if there is a many-to-many relationship between users and promo codes. For example, if one user can use multiple promo codes, and one promo code can be used by multiple users.
If each promo code is only associated with one user, or vice versa, then a linking table is not necessary — it is enough to add a foreign key to one of the tables.
Example of a linking table:
CREATE TABLE user_promo_codes (
user_id INT,
promo_code_id INT,
used_at TIMESTAMP,
PRIMARY KEY (user_id, promo_code_id),
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (promo_code_id) REFERENCES promo_codes(id)
);