I am working on a mock airline ticket reservation app using javascript and a postgres database.
--EDITED--
I need to create a function/trigger that will only allow me to insert a new row in my TICKET if the number of tickets for a flight does not exceed the max number of seats for the aircraft.
How do you write a function isolates single values from queries: 1)SELECT count(*) FROM ticket WHERE flight_id = x 2)SELECT maxSeats FROM airplane where aircraft_code = (code for for airplane the correlates with flight).
How do I get single values from these two queries to check (number of seats <= max_seats).
Thanks.
You can derive the "number of seats" by
SELECT COUNT(*) FROM TICKET WHERE flight_id = {flight_id}. Adding anumber of seatsattribute in FLIGHT would be a normalisation error and force you to keep it in sync with the TICKETs for the FLIGHT.In a platform that supports it, you could add a CHECK constraint that calls a function to do the necessary checks by querying other tables. As far as I know, while you can do this in Postgres, there are problems re concurrent updates and it is recommended using triggers.
Not relevant with Postgres, but check this thread if you are interested
Something like this would work: