/ /同じ名前のビューとテーブルは可能です - mysql、database、view

同じ名前のビューとテーブルは可能です - mysql、database、view

同じ名前のmysqlビューとテーブルを作成することは可能ですか?

私はテーブルhs_hr_employeeを持っている私は同じ名前としてビューを作成したい

create VIEW hs_hr_employee AS SELECT * from hs_hr_employee;

私は次のエラーに陥っている

#1050 - Table "hs_hr_employee" already exists

どんな助けにも感謝

よろしく

回答:

回答№1は5

のような別の名前を表示することはできません

hs_hr_employee_view

から マニュアル

データベース内では、基本テーブルとビューは同じ名前空間を共有します。 基本表とビューに同じ名前を付けることはできません。


回答№2については2

述べたように、あなたはビューではできませんが、あなたは できる 一時テーブルを使用します。

実際のテーブルと同じ名前のテンポラリテーブルを作成すると、テンポラリテーブルは 実際のテーブルを非表示にします。つまり、実際のテーブルにアクセスすることはできません 〜まで 一時テーブルを削除しました:

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>

しかし、一時テーブルはなくなります( drop あなたのセッションが切断されたら)。接続するたびに再作成する必要があります。