Hacker News new | ask | show | jobs
by jamesmstone 1437 days ago
pre postgres 15 can you solve this with two unique constraints ?

unique (EmployeeName), unique (EmployeeName, CarID)

2 comments

You’d most likely solve this by making the CarID NOT NULL because there’s why would you even have a row with no useful data?

And if this is your employee table, you’d have UNIQUE (EmployeeData) and UNIQUE (CarID), and it would do what you want.

That's not quite the same, since the previous solution of unique (EmployeeName, CarID) allowed for one employee to have 2 separate cars. You have to use conditional indexes to solve this.