How to rename a table in MySQL / MariaDB

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Hello, friends. In this post, you will learn how to rename a table in MySQL / MariaDB, The procedure is simple and can save you some headache.

In some situations, you may want to rename a certain table in MySQL / MariaDB. This process although simple is important to know if you work daily with this powerful database manager.

Let’s go.

Install MySQL / MariaDB

The procedure to install MySQL / MariaDB will depend on your distribution. However, we have several posts that talk about it, so just follow it for your system.

Once you have MySQL / MariaDB ready for work, you can continue.

How to rename a table in MySQL / MariaDB

Once you are connected to the program console, you can follow this structure

RENAME TABLE `DATABASE`.`TABLE NAME` TO `DATABASE`.`NEW TABLE NAME`;

As you can see, the structure of the command is simple to follow. In short, just specify with RENAME the current table name and a new one.

Pressing ENTER will make the change.

Another option, just as easy but more explicit, consists in choosing and using the database where the table to rename is located

USE DATABASE;

And from there, execute this command

RENAME TABLE `TABLE NAME` TO `NEW TABLE NAME`;

For this post, I will create a database called Sample and inside it a table Car with some structure

CREATE DATABASE Sample;
use Sample;
CREATE TABLE `Car` (id INT(11) default NULL auto_increment,
  s char(60) default NULL,
    PRIMARY KEY (id)
    );

Something simple

Now, let’s rename it

RENAME TABLE `Sample`.`Car` TO `Sample`.`Kar`;

Now show the changes

show tables;

Output

Rename a table in MySQL / MariaDB
Rename a table in MySQL / MariaDB

Conclusion

In this post, you learned how to rename tables in MySQL / MariaBD. So quick and easy, a trick that can help you.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Angelo
Angelo
I am Angelo. A systems engineer passionate about Linux and all open-source software. Although here I'm just another member of the family.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook