View and table with the same name is possible

Is it possible to create a mysql view and table with the same name

For example, I have a table hs_hr_employee, I want to create a presentation as the same name

create VIEW hs_hr_employee AS SELECT * from hs_hr_employee; 

I get the following error

 #1050 - Table 'hs_hr_employee' already exists 

Any help Thankful

Hi

+6
source share
2 answers

you cannot give another name for viewing, for example

 hs_hr_employee_view 

from the manual

In a database, base tables and views have the same namespace, so the base table and view cannot have the same name.

+5
source

As stated, you cannot do this with views, but you can with temporary tables.

If you create a temporary table with the same name as the actual table, the temporary table will shadow (hide) the actual table. This means that you cannot access the actual table until you delete the temporary table:

 mysql> create table t select 1; # actual table t Query OK, 1 row affected (0.58 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> create temporary table t select*from t; # temp table t Query OK, 1 row affected (0.53 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert t select 2; # t refers to the temp table Query OK, 1 row affected (0.06 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select*from t; # temp table +---+ | 1 | +---+ | 1 | | 2 | +---+ 2 rows in set (0.00 sec) mysql> drop table t; # temp table Query OK, 0 rows affected (0.06 sec) mysql> show tables like "t"; # verify that actual table still exists. show tables will not show temporary tables +--------------------+ | Tables_in_test (t) | +--------------------+ | t | +--------------------+ 1 row in set (0.00 sec) mysql> mysql> select*from t; # now t refers to the actual table +---+ | 1 | +---+ | 1 | +---+ 1 row in set (0.00 sec) mysql> drop table t; # actual table Query OK, 0 rows affected (0.14 sec) mysql> 

However, temporary tables are deleted (even if you don’t drop them) after the session is disconnected. You will need to create them every time you connect.

+2
source

Source: https://habr.com/ru/post/893260/


All Articles