protein
protein

Reputation: 37

Limit same data entries

Im new to sql and i have two tables, one for fridges and one for food. Only 5 items of food can be stored into one fridge, so i was wondering if there is a way to limit the food table to only have 5 of the same fridge_id entries?

Upvotes: 0

Views: 143

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246268

There is no straightforward way to enforce such a constraint.

The best I can think of is:

  • have a (redundant) column food_count on the fridges table

  • define an AFTER INSERT OR UPDATE OR DELETE trigger on the food table that updates food_count whenever something changes

  • add a check constraint on the fridges table that limits food_count to 5

Upvotes: 1

Related Questions