is a mysql temporary table unique for each user accessing the script that creates it...?

SpikETidE picture SpikETidE · Mar 16, 2010 · Viewed 10.5k times · Source

While looking for a way to temporarily save the search results when a user searches for a hotel free between particular dates i came across temporary tables.

But certain questions are not answered even in mysql manual.... like...

  1. Will the temporary table be unique for each user that executes the script...? Or will it be overwritten when two different users run the script at the same time...?

  2. When will the table be destroyed..? When the user closes the browser window or just navigates away from the page in which the script runs...?

Thanks for your clarifications...

This is how i do it....

$table = "CREATE OR REPLACE TEMPORARY TABLE $free_room(
              room_id INT(5),
              room_name VARCHAR(150),
              max_persons INT(1),
              rooms_free INT(1),
              room_price INT(6),
              hotel_id INT(4),
              hotel_name VARCHAR(100),
              hotel_stars INT(1),
              hotel_type INT(1)) ENGINE=MEMORY";

    $query_getFreeRooms = "INSERT INTO  $free_room
                           SELECT $rooms.room_id,
                                  $rooms.room_name,
                                  $rooms.max_persons,
                                  $rooms.total_rooms - $rooms.booked_rooms AS rooms_free,
                                  $rooms.room_price,
                                  $hotels.hotel_id,
                                  $hotels.hotel_name,
                                  $hotels.hotel_stars,
                                  $hotels.hotel_type
                           FROM   $hotels,$rooms
                           WHERE  $rooms.room_id NOT IN (SELECT room_id
                                                         FROM   $reservations
                                                         WHERE  $dateCheck)
                           AND    $hotels.hotel_city = '$city_search1'
                           AND    $hotels.hotel_id = $rooms.hotel_id
                           AND    $hotels.hotel_deleted = '0'
                           AND    $rooms.room_deleted = '0'";

Answer

Pascal MARTIN picture Pascal MARTIN · Mar 16, 2010

Quoting the MySQL manual page of CREATE TABLE :

You can use the TEMPORARY keyword when creating a table.
A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed.
This means that two different connections can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name.

Considering that two users will have two distinct connections, the table will be unique for each user.

The temporary table will be dropped when the connection that created it is closed -- in PHP, at least, it means when the script that generates the page ends (So, generally, even before the users actually reads the page)