PostgreSQL - имя временной таблицы
В PostgreSQL временная таблица может иметь то же имя, что и постоянная таблица, хотя это не рекомендуется. Когда пользователь создает временную таблицу с тем же именем, что и постоянная таблица, он не может получить доступ к постоянной таблице, пока временная таблица не будет удалена.
Example:
First, create a table named customers
:
CREATE TABLE customers(id SERIAL PRIMARY KEY, name VARCHAR NOT NULL);
Second, create a temporary table with the same name: customers
:
CREATE TEMP TABLE customers(customer_id INT);
Теперь запросите данные из таблицы клиентов, как показано ниже:
ВЫБРАТЬ * ИЗ клиентов;
Output:
At this stage, PostgreSQL accessed the temporary table customers instead of the permanent one. From now on, you can only access the permanent customers
table in the current session when the temporary table customers
is removed specifically.If you list the tables in the test
database, you will only see the temporary table customers, not the permanent ones below:
Note: PostgreSQL creates temporary tables in a special schema, therefore, you must not specify the schema in the CREATE TEMP TABLE statement.
Теперь, если вы перечислите таблицы, используя следующую команду:
dt
The result is as shown below:
The output shows that the schema of the customers
temporary table is pg_temp_3
.