Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can two temporary tables with the same name exist in separate queries

Tags:

postgresql

I was wondering, if it is possible to have two temp tables with the same name in two separate queries without them conflicting when called upon later in the queries.

Query 1: Create Temp Table Tmp1 as ...

Query 2: Create Temp Table Tmp1 as ...

Query 1: Do something with Tmp1 ...

I am wondering if postgresql distinguishes between those two tables, maybe through addressing them as Query1.Tmp1 and Query2.Tmp1

like image 591
Phill Avatar asked Nov 11 '15 18:11

Phill


People also ask

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.

Can we have two tables with the same name in mysql?

You can have tables of the same name only if they are in separate databases, and you use the database name as a qualifier.

Can local temporary tables be seen by another connection to the same database?

Local temporary objects (Tables and Stored Procedures) are completely safe from being seen by other sessions.

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.


1 Answers

Each connection to the database gets its own special temporary schema name, and temp tables are created in that schema. So there will not be any conflict between concurrent queries from separate connections, even if the tables have the same names. https://dba.stackexchange.com/a/5237 for more info

The PostgreSQL docs for creating tables states:

Temporary tables exist in a special schema, so a schema name cannot be given when creating a temporary table.

like image 145
Yosef Weiner Avatar answered Dec 25 '22 12:12

Yosef Weiner