I have a scenario where i would need to add a unique constraint using columns from 2 different tables. I have 2 tables. The first table is like,
TABLE MODEL(
ID PRIMARY_KEY,
KEY
)
and the second table as,
TABLE OWNERS(
ID PRIMARY_KEY,
MODEL_ID FOREIGN_KEY REFERENCES MODEL.ID,
USER
)
My requirement is the combination of MODEL.KEY + OWNERS.USER
should be unique. How can we achieve this?
Note: I'm using Postgres as database. There is no possibility of adding OWNERS.USER to MODEL table.
That should be a simple unique constraint on owners
:
ALTER TABLE owners ADD UNIQUE ("user", model_id);
Avoid a name like user
for columns or tables, because that is a reserved SQL keyword.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.