Donneyo
Donneyo

Reputation: 3

How do i set maximum number in a table on sql database server

Please I have a SQL database project am working on and am asked to ensure that on my table HumanResources.Booking.

  1. Max_Num should store maximum number of members allowed to use a facility at a given time

  2. Actual number should store the number of bookings already made by the members for a facility. Its value cannot exceed the value of Max_Num.

Upvotes: 0

Views: 98

Answers (1)

sniperd
sniperd

Reputation: 5274

It may be difficult to do this with constraints on the actual table without using a trigger or instead of trigger (as Edward kindly commented) which just sounds like a bunch of problems down the road. Yuck.

I would suggest the following:

  • restrict permissions on the table so people can't insert/update the table
  • use a stored procedure to do your inserts/updates
  • put the business logic outlined in your question into the stored procedure

That way your data should be safe from the kind of updates you don't want, yet the application developers can still just throw their data at database: your stored procedure. You can also then handle errors (people trying to break your business logic) nicely.

Note if you are planning on handling large amounts of data it will be well worth putting effort into optimizing your stored procedure.

Upvotes: 1

Related Questions