Home > Database > Mysql Tutorial > How to Use MySQL JOIN UPDATE to Increment Train Capacity After Reservation Cancellation?

How to Use MySQL JOIN UPDATE to Increment Train Capacity After Reservation Cancellation?

Patricia Arquette
Release: 2025-01-04 00:29:39
Original
356 people have browsed it

How to Use MySQL JOIN UPDATE to Increment Train Capacity After Reservation Cancellation?

MySQL Syntax for Join Updates: Incrementing Train Capacity on Reservation Cancellation

To update a column in one table based on information from another table, MySQL provides a JOIN UPDATE syntax. This can be particularly useful in scenarios where you need to update a column based on data in a related table.

Example: Incrementing Train Capacity on Reservation Cancellation

Consider two tables, Train and Reservations. The goal is to increment the Capacity column in the Train table when a reservation is cancelled in the Reservations table.

Train
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| TrainID  | varchar(11) | NO   | PRI | NULL    |       |
| Capacity | int(11)     | NO   |     | 50      |       |
+----------+-------------+------+-----+---------+-------+

Reservations
+---------------+-------------+------+-----+---------+----------------+
| Field         | Type        | Null | Key | Default | Extra          |
+---------------+-------------+------+-----+---------+----------------+
| ReservationID | int(11)     | NO   | PRI | NULL    | auto_increment |
| FirstName     | varchar(30) | NO   |     | NULL    |                |
| LastName      | varchar(30) | NO   |     | NULL    |                |
| DDate         | date        | NO   |     | NULL    |                |
| NoSeats       | int(2)      | NO   |     | NULL    |                |
| Route         | varchar(11) | NO   |     | NULL    |                |
| Train         | varchar(11) | NO   |     | NULL    |                |
+---------------+-------------+------+-----+---------+----------------+
Copy after login

Query for Incrementing Capacity

The JOIN UPDATE syntax for this scenario would be:

UPDATE Train t
JOIN Reservations r ON (t.TrainID = r.Train)
SET t.Capacity = t.Capacity + r.NoSeats
WHERE r.ReservationID = "15";
Copy after login

In this query:

  • The JOIN clause establishes a relationship between the Train and Reservations tables based on the TrainID column.
  • The SET clause increments the capacity based on the number of seats reserved in the corresponding reservation.
  • The WHERE clause specifies the specific reservation to update on the basis of ReservationID.

Incremental Update with Arbitrary Value

To increment the capacity by an arbitrary number of seats, simply replace r.NoSeats with the desired value in the SET clause:

UPDATE Train t
JOIN Reservations r ON (t.TrainID = r.Train)
SET t.Capacity = t.Capacity + 5;
Copy after login

Transaction Management

To perform the increment and deletion in a single transaction:

// Java transaction management code

try {
  // Perform the capacity increment
  execute SQL UPDATE statement

  // Delete the reservation
  execute SQL DELETE statement
} catch (SQLException e) {
  // Transaction rollback logic if an error occurs
} finally {
  // Transaction cleanup logic
}
Copy after login

By executing the update before the deletion, the capacity will be incremented correctly before the reservation is removed.

The above is the detailed content of How to Use MySQL JOIN UPDATE to Increment Train Capacity After Reservation Cancellation?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template