可以使用相同名称的视图和表格

时间:2021-04-22 01:04:40

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

是否可以使用相同的名称创建一个mysql视图和表

for example i have a table hs_hr_employee i want create a view as a same name

例如,我有一个表hs_hr_employee我想创建一个同名的视图

create VIEW hs_hr_employee AS SELECT * from hs_hr_employee;

I m getting following error

我得到了以下错误

#1050 - Table 'hs_hr_employee' already exists

Any help Thankful

任何帮助感激

Regards

问候

2 个解决方案

#1


5  

you can't , give to view different name like

你不能,给予查看不同的名字之类的

hs_hr_employee_view

from manual

来自手册

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

在数据库中,基表和视图共享相同的名称空间,因此基表和视图不能具有相同的名称。

#2


2  

As stated, you can't do it with views, but you can with temporary tables.

如上所述,您无法使用视图执行此操作,但可以使用临时表。

If you create a temporary table with the same name as an actual table, the temporary table will shadow (hide) the actual table. This means that you can't access the actual table until you have dropped 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 gone (even if you do not drop them) once your session is disconnected. You'll need to re-create them every time you connect.

但是,一旦断开会话,临时表就会消失(即使您不删除它们)。每次连接时都需要重新创建它们。

#1


5  

you can't , give to view different name like

你不能,给予查看不同的名字之类的

hs_hr_employee_view

from manual

来自手册

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

在数据库中,基表和视图共享相同的名称空间,因此基表和视图不能具有相同的名称。

#2


2  

As stated, you can't do it with views, but you can with temporary tables.

如上所述,您无法使用视图执行此操作,但可以使用临时表。

If you create a temporary table with the same name as an actual table, the temporary table will shadow (hide) the actual table. This means that you can't access the actual table until you have dropped 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 gone (even if you do not drop them) once your session is disconnected. You'll need to re-create them every time you connect.

但是,一旦断开会话,临时表就会消失(即使您不删除它们)。每次连接时都需要重新创建它们。