Rename a table in MySQL using RENAME TABLE command

MySQLMySQLi Database

To rename a table in MySQL, below is the syntax −

rename table yourOldTableName to yourNewTableName ;

Let us first create a table −

mysql> create table DemoTable1
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   UserName varchar(100),
   UserAge int,
   UserCountryName varchar(100)
);
Query OK, 0 rows affected (1.29 sec)

Let us check the description of the table −

mysql> desc DemoTable1;

This will produce the following output −

+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| Id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| UserName        | varchar(100) | YES  |     | NULL    |                |
| UserAge         | int(11)      | YES  |     | NULL    |                |
| UserCountryName | varchar(100) | YES  |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+
4 rows in set (0.20 sec)

Now, we will rename a table in MySQL using RENAME command −

mysql> rename table DemoTable1 to DemoTable2;
Query OK, 0 rows affected (1.22 sec)

Let us check the description of the table once again −

mysql> desc DemoTable2;

This will produce the following output −

+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| Id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| UserName        | varchar(100) | YES  |     | NULL    |                |
| UserAge         | int(11)      | YES  |     | NULL    |                |
| UserCountryName | varchar(100) | YES  |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
raja
Published on 25-Sep-2019 11:03:59
Advertisements