Question
Which database constraint is most important for preventing two different long URLs from getting the same short code?
A
A UNIQUE constraint or primary key on short_code
B
A NOT NULL constraint on long_url
C
A default timestamp on created_at
D
A nullable constraint on expiration_date
Explanation
A UNIQUE constraint or primary key on short_code enforces uniqueness at the database level.
Application logic can try to generate unique short codes, but the database should still enforce uniqueness. A UNIQUE constraint or primary key on the short_code column prevents accidental collisions. If a collision occurs during insertion, the service can retry with a new code or salt.