Is it possible to have View and table with the same name in MySQL?


No, you cannot give the same name for view and table in MySQL.

Let us first create a demo table −

mysql> create table view_Table_Demo
   -> (
   -> Id int,
   -> Name varchar(20)
   -> );
Query OK, 0 rows affected (0.80 sec)

Now you can insert some records in the table using insert command. The query is as follows −

mysql> insert into view_Table_Demo values(100,'Larry');
Query OK, 1 row affected (0.17 sec)
mysql> insert into view_Table_Demo values(101,'Mike');
Query OK, 1 row affected (0.20 sec)
mysql> insert into view_Table_Demo values(102,'Sam');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement. The query is as follows −

mysql> select *from view_Table_Demo;

Here is the output −

+------+-------+
| Id | Name    |
+------+-------+
| 100 | Larry  |
| 101 | Mike   |
| 102 | Sam    |
+------+-------+
3 rows in set (0.00 sec)

Here you will get an error if you try to give same name for view and table. The query and the error is as follows −

mysql> create VIEW view_Table_Demo AS SELECT * from view_Table_Demo;
ERROR 1050 (42S01): Table 'view_Table_Demo' already exists
NOTE: To avoid the above error, try to give different name.

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements