MySQL is a popular open-source relational database management system (RDBMS) that is usually used for developing scalable and high-performance databases. MySQL was developed by MySQL AB (currently owned by Oracle Corporation) in 1995.
MySQL is known for its robust, easy, and reliable features with quick processing speeds. MySQL is generally used by dynamic web applications and is commonly used by languages such as PHP, Python, and other server-side programming languages.
In this article, you will learn how to RENAME a view in MySQL along with some examples.
MySQL - Rename View
In relational database management systems (RDBMS) like MySQL, a VIEW is a virtual table interactive with data generated from one or more underlying tables through either a defined query. Unlike a regular table, the VIEW as a query doesn't store the data itself. Instead, it creates a result set when someone queries it.
MySQL provides a feature to RENAME an Existing VIEW. Renaming a VIEW in MySQL will not change the data in the underlying tables it will only change the name by which the view is referenced in SQL queries and the database schema.
Syntax:
RENAME TABLE old_view_name TO new_view_name;
Explanation: In the above syntax you can see that we are Renaming the VIEW from the old_view_name to the new_view_name. For a VIEW to be renamed the VIEW must be present.
Examples of MySQL RENAME VIEW
Let's take an example of the EMPLOYEE table having EMP_ID, NAME, AGE, and SALARY as columns.
CREATE TABLE EMPLOYEE (
EMP_ID INT PRIMARY KEY,
NAME VARCHAR(50),
AGE INT,
SALARY INT
);
Insert the data on it:
INSERT INTO EMPLOYEE (EMP_ID, NAME, AGE, SALARY) VALUES
(1, 'Sahil', 21, 15000),
(2, 'Alen', 22, 13000),
(3, 'John', 22, 14000),
(4, 'Alex', 20, 13000),
(5, 'Mathew', 22, 14000),
(6, 'Sia', 21, 15000),
(7, 'David', 22, 16000),
(8, 'Tim', 21, 14000),
(9, 'Leo', 20, 15000),
(10, 'Tom', 21, 16000);
EMPLOYEE Table:
EMPLOYEE TableNow Let's CREATE 2 VIEWS from the EMPLOYEE Table.
Query:
CREATE VIEW view1 AS
SELECT EMP_ID, SALARY
FROM EMPLOYEE;
CREATE VIEW view2 AS
SELECT EMP_ID, AGE, SALARY
FROM EMPLOYEE
WHERE SALARY=14000;
Output: view1
view1view2:
view2Examples of MySQL Rename View
Example 1: Renaming view1 using RENAME keyword
Let RENAME view1 to employee_details1.
Syntax:
RENAME TABLE old_view_name TO new_view_name;
Query:
RENAME TABLE view1 TO employee_details1;
Output:
successfully renamedExplanation: In the above example we are renaming view1 to employee_details1. In the output we can see that there are 0 rows affected, this is because renaming a view will not change the underlying data in the table.
Example 2: Renaming view2 using RENAME keyword
Lets RENAME view2 to employee_details2
Syntax:
RENAME TABLE old_view_name TO new_view_name;
Query:
RENAME TABLE view1 TO employee_details1;
Output:
successfully renamedExplanation: In the above example we are renaming view2 to employee_details2. In the output we can see that there are 0 rows affected, this is because renaming a view will not change the underlying data in the table.
Example 3: Renaming a View using DROP VIEW and CREATE VIEW Sequence
Let's assume we have a view employees_above_20
that we want to rename to employees_above_age_20
. Here's how you can achieve this:
- 1. Existing View:
employees_above_20
Suppose employees_above_20
is defined as:
CREATE VIEW employees_above_20 AS
SELECT *
FROM EMPLOYEE
WHERE AGE > 20;
- Renaming
employees_above_20
to employees_above_age_20
:
-- Drop the existing view if it exists
DROP VIEW IF EXISTS employees_above_age_20;
-- Create a new view with the desired name and definition
CREATE VIEW employees_above_age_20 AS
SELECT *
FROM EMPLOYEE
WHERE AGE > 20;
Output:
+--------+--------+------+--------+
| EMP_ID | NAME | AGE | SALARY |
+--------+--------+------+--------+
| 1 | Sahil | 21 | 15000 |
| 2 | Alen | 22 | 13000 |
| 3 | John | 22 | 14000 |
| 5 | Mathew | 22 | 14000 |
| 6 | Sia | 21 | 15000 |
| 7 | David | 22 | 16000 |
| 8 | Tim | 21 | 14000 |
| 10 | Tom | 21 | 16000 |
+--------+--------+------+--------+
Conclusion
In conclusion, MySQL allows you to RENAME an existing VIEW. It is a metric that is quite essential in cases where you need to make sure that each naming convention is consistent or helps improve the clarity of the database structure. Be reminded that the renaming of a VIEW does not change the underlying data but rather it does modify only the name by which the view is referenced. Moreover, you should be caring about any occurrence of the dependencies on the VIEW, for instance, stored routines or other views, thus, such dependencies will be updated after the renaming has been fulfilled.
Similar Reads
MySQL | RENAME USER
In MySQL, the RENAME USER statement provides a straightforward way to change the username of an existing user account without altering any associated privileges or settings. In this article, we will learn RENAME USER in MySQL in a step-by-step approach.MySQL RENAME USERIn MySQL, the RENAME USER stat
2 min read
MySQL - Update View
MySQL is a popular open-source relational database management system (RDBMS) that is usually used for developing scalable and high-performance databases. A VIEW serves as a virtual table that interacts with data derived from one or more underlying tables through a defined query. In this article, We
5 min read
PL/SQL RENAME VIEW
PL/SQL which is Oracle Corporation's extension of SQL with a procedural language enables developers and database administrators to carry out complex operations in the databases. One of the frequently performed operations in the process of database maintenance is the change of names of objects to acc
3 min read
MYSQL View
MySQL is an open-source RDBMS, i.e. Relational Database Management System which is maintained by Oracle. MySQL has support for major operating systems like Windows, MacOS, Linux, etc. MySQL makes it easy for users to interact with your relational databases, which store data in the form of tables. Yo
11 min read
SQL - Rename View
In relational databases, views are essential tools used to simplify complex queries, enhance data security, and improve overall database management. However, as our database evolves, the need to rename existing views may arise. This renaming process helps maintain consistency, improves clarity, and
6 min read
SQL UPDATE VIEW
SQL Views are virtual tables that represent the result of a SELECT query. They simplify complex queries and present data in a more understandable format. While views don't store data themselves, they provide a convenient way to access and manipulate data without modifying the underlying tablesIn thi
5 min read
MySQL - ALTER VIEW Statement
The ALTER VIEW statement in MySQL is a powerful tool that allows users to modify the definition of an existing view without the need to drop and recreate it. This statement is particularly useful for changing the query or structure of a view to better help the needs of the application or database de
5 min read
MySQL Reserved Words
MySQL is a popular and widely used Relational Database Management System. Like any programming language, MySQL has its own set of reserved words. Reserved words are specific terms or keywords with predefined meanings within the database system. It is very important to know the reserved words to avoi
5 min read
MySQL CREATE VIEW Statement
MySQL, an open-source relational database management system, offers a variety of features to manage and manipulate data efficiently. One of these features is the CREATE VIEW statement, which allows you to create a virtual table known as a view. A view provides a way to simplify complex queries, enha
5 min read
PL/SQL UPDATE VIEW
In database management, particularly within Oracle Database, the ability to update data through views enhances flexibility, efficiency, and control over data manipulation tasks. Views in Oracle Database act as virtual tables, presenting data from one or more base tables. Although views are commonly
4 min read