-1

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.

mcdenyer
  • 3
  • 3
  • 1
    Write a query that returns all the rows that you want. Then write an insert that modifies that query to insert those rows. If there are no rows to insert then it will not insert any rows. How is this different from any insert? (Rhetorical.) PS "looking to get pointed in the general right direction on how to approach this kind of problem" is not a valid SO question. [ask] [Help] PS Phrases like "need to compare values" are too vague to be helpful. [mre] – philipxy Nov 28 '21 at 03:57
  • Still no [mre]. Or sign of research. Please don't ask us to write your code. Please before considering posting google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular names/strings/numbers, 'site:stackoverflow.com' & tags; read many answers. PS Please no "EDITED" etc, just make your post the best presentation as of now. Please before you post look at the formatted version of your post below the edit box. Read the edit help re inline & block formats for code & quotations. Also re line breaks. Please avoid "thanks" etc. – philipxy Nov 29 '21 at 09:12
  • Research how to use the TOP Keyword. – Wes Palmer Nov 29 '21 at 14:07

1 Answers1

2

I need to do a transaction that does the following: -INSERT new row in TICKET table that includes a foreign key(flight_id) that references the FLIGHT table. -UPDATE the number of seats(increment by one) in the FLIGHT table for the flight(flight_id) that the ticket was just inserted.

You can derive the "number of seats" by SELECT COUNT(*) FROM TICKET WHERE flight_id = {flight_id}. Adding a number of seats attribute in FLIGHT would be a normalisation error and force you to keep it in sync with the TICKETs for the FLIGHT.

Do I need to use a trigger or function for this that will simply throw an error when I try to commit? or is there a way to set up a special kind of constraint for a column that references the value in another table(ie. FLIGHT.num_seats_booked < AIRPLANE.max_num_seats.)

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.

I need to only commit the above transaction if their are still seats available for the flight.

Or should I be using an IF/ELSE query inside of my transaction?

Just looking to get pointed in the general right direction on how to approach this kind of problem

Not relevant with Postgres, but check this thread if you are interested


How do i go about writing a function that compares number of tickets for a flight and max_seats for an airplane? I'm not sure how to grab a single value from two queries to compare them and return true or false in a function.

Something like this would work:

SELECT COUNT(*) < (
    SELECT MaxSeats
    FROM Flight
    INNER JOIN Aircraft
    ON Aircraft.AircraftCode = Flight.AircraftCode
    WHERE FlightId = {flightId})    
FROM Ticket
WHERE FlightId = {flightId}
  • Thanks Giannis. I will derive number of seats by querying the tickets table. How do i go about writing a function that compares number of tickets for a flight and max_seats for an airplane? I'm not sure how to grab a single value from two queries to compare them and return true or false in a function. – mcdenyer Nov 28 '21 at 15:29
  • @mcdenyer See edited answer – Giannis Tsim Nov 28 '21 at 18:14
  • @mcdenyer You are welcome. Accept the answer if it covers you. – Giannis Tsim Nov 29 '21 at 05:44
  • Giannis, you mentioned that eliminating the num_seats_booked from flights would hurt my normalization form. If my flight table only has a single key PK(flight_id) then wouldn't the only thing that the num_seats_booked depend on would be the pk. flight_id ->num_seats_booked. I am still working to understand normalization. Can you explain which Normalization form this violates? I am trying to make my relations be in 3nf or BCNF. – mcdenyer Dec 01 '21 at 15:15
  • @mcdenyer Sorry but I am not sure about this. This is definitely redundant data and that naturally strikes me as something that could be further normalized. Not thought about NFs and if / how you can classify it as a normalization error that way. Since you have a Ticket entity storing the relevant facts, can you still regard “num_seats_booked” as functionally dependent solely on Flight PK? I will try to get back to you about this. – Giannis Tsim Dec 01 '21 at 17:15