Okay, first of all I want to thank you to two people in the forum which amazed me for the level of knowledge they have.
One of them is FJCC, the forum's moderator, which months ago gave me an example of code using a temporary table that was a piece of art. Brilliant.
The other one is Arineckaig which made possible I understand in an easy way how to create a temporary table.
The example code I am posting is based in a code from Arineckaig
Here you have an example database you can download and you can use to do tests. This example database is in Spanish, it has many tables and we will perform a query using a temporary table example.
You can download the database here:
https://mega.nz/#!gF5lxYQQ!davoeKfKc_GQ ... FEbbHh8ZDI
Okay, we will do the following.
We will use the tbl_EMPLEADOS (tbl_EMPLOYEES) a real table, with data in it...
We will create a temporary table "on the fly" called tbl_temporal...
We will load the data in this temporary table called tbl_temporal, and the data will come from the real table tbl_EMPLEADOS
Finally, we will show the data from the temporary table and we won't show any data from the real table, just the temporary one.
The code is as follows:
Code: Select all
//
// THIS CODE LOAD THE DATA FROM A REAL TABLE TO A TEMPORARY TABLE
//
//
// THE SELECT CREATE THE TEMPORARY TABLE ON THE FLY OBSERVE HOW IT IS NAMED "tbl_TEMPORAL" (FULL STOP) "the field you want to show"
// FOR EXAMPLE: "tbl_temporal"."Nombre" WILL SHOW THE FIELD NOMBRE (NAME) FROM THE TEMPORARY TABLE "tbl_temporal"
//
// SUMMARIZING WE CREATE A TEMPORARY TABLE CALLED tbl_TEMPORAL AND WE ARE SHOWING FROM THAT TEMPORARY TABLE THE FIELDS
//
// Nombre (name in English), "Apellido1ero" (LastName),"Apellido2Seg" (Second Lastname, in Spain we use two last names father and mother)
//
SELECT
"tbl_TEMPORAL"."Nombre",
"tbl_TEMPORAL"."Apellido1ero",
"tbl_TEMPORAL"."Apellido2seg"
// NOW AS USUALLY WE USE THE FROM (SELECT - FROM) AS IN ALL THE QUERIES DUE WE ARE LOADING DATA FROM A REAL TABLE, IT MUST BE PRESENT
// IN THE FROM, SO WE HAVE FROM "tbl_EMPLEADOS", OBSERVE WE HAVE A COMMA BECAUSE WE CONTINUE ADDING DATA TO BE SHOWN IN THE QUERY, AND
// THAT DATA IS THE DATA FROM THE TEMPORARY TABLE
//
// SO... WE HAVE FROM "tbl_EMPLEADOS",
//
// AND NOW WE PROCEED TO LOAD THE DATA USING ANOTHER SELECT FROM WE WILL SELECT ALL THE DATA SELECT * (ASTERISK) FROM THE REAL
// TABLE "tbl_EMPLEADOS" AND WE WILL LOAD THAT DATA IN THE TEMPORARY TABLE "tbl_TEMPORAL"
//
// AT THIS TIME WE ARE CREATING THE TEMPORARY TABLE AND ALSO LOADING ALL THE DATA IN IT. WE LOAD ALL THE DATA IN THE TEMPORARY TABLE
// USING THE SELECT * FROM AND WE DEFINE THE TEMPORARY TABLE USING AN ALIAS AS "tbl_TEMPORAL"
FROM "tbl_EMPLEADOS",
( SELECT *
FROM "tbl_EMPLEADOS") AS "tbl_TEMPORAL"
// FINALLY WE DEFINE A RELATIONSHIP BETWEEN THE REAL TABLE tbl_EMPLEADOS AND THE TEMPORARY TABLE tbl_TEMPORAL FOR THAT WE STABLISH
// THE RELATIONSHIP USING THE WHERE AND RELATING THE PRIMARY KEY FIELD OF BOTH TABLES SEE THE FIELD ID_Empleados IS PRESENT IN BOTH
// TABLES THE REAL ONE tbl_EMPLEADOS AND THE TEMPORARY TABLE tbl_TEMPORAL
WHERE "tbl_EMPLEADOS"."ID_Empleados" = "tbl_TEMPORAL"."ID_Empleados"
THIS CODE LOAD DATA FROM A REAL TABLE INTO A TEMPORARY TABLE:
Code: Select all
SELECT
"tbl_TEMPORAL"."Nombre",
"tbl_TEMPORAL"."Apellido1ero",
"tbl_TEMPORAL"."Apellido2seg"
FROM "tbl_EMPLEADOS",
( SELECT *
FROM "tbl_EMPLEADOS") AS "tbl_TEMPORAL"
WHERE "tbl_EMPLEADOS"."ID_Empleados" = "tbl_TEMPORAL"."ID_Empleados"
What is the strategy behind the use of temporary tables?
I have uploaded a demo database, if someone want to play a bit with it and retrieve other data using temporary tables, I think we could learn a bit more about the strategies behind the use of them. I find this kind of tables very interesting and I would like to see more examples and practical uses for them.
Any idea to show more examples?
Cheers