Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'";
like image 222
SpikETidE Avatar asked Mar 16 '10 12:03

SpikETidE


People also ask

Are temp tables unique to sessions?

Temporary tables only exist within the session in which they were created and persist only for the remainder of the session. As such, they are not visible to other users or sessions.

What is a temporary table in MySQL?

A TEMPORARY table is visible only within the current session, and is dropped automatically when the session is closed. This means that two different sessions can use the same temporary table name without conflicting with each other or with an existing non- TEMPORARY table of the same name.

What is the advantage of using a temporary table instead of a table?

Advantages of Temporary Tables You can create a temporary table and insert, delete and update its records without worrying about whether you have sufficient rights to change data in permanent tables, or whether you might be accidentally doing so.

Can global temporary tables can be shared between concurrent connections?

Global temp tables can be referenced from the same connection or a different connection so long as the global temp table has not gone out of scope. The database connection is the same when a temp table is created and referenced from a script within the same SSMS tab.


1 Answers

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)

like image 74
Pascal MARTIN Avatar answered Dec 02 '22 20:12

Pascal MARTIN