Cover Image for MySQL Rename Table
95 views

MySQL Rename Table

You can rename a table in MySQL using the RENAME TABLE statement. Here’s the basic syntax:

RENAME TABLE current_table_name TO new_table_name;
  • current_table_name: The name of the table you want to rename.
  • new_table_name: The new name you want to give to the table.

Here’s an example of how to rename a table:

RENAME TABLE old_table_name TO new_table_name;

In this example, replace old_table_name with the current name of the table you want to rename and new_table_name with the desired new name for the table.

For example, if you have a table named “employees” and you want to rename it to “staff,” you would execute the following SQL statement:

RENAME TABLE employees TO staff;

After executing this statement, the “employees” table will be renamed to “staff.”

It’s important to note that the RENAME TABLE statement may not work in some MySQL configurations or with certain storage engines. If you encounter any issues, you can also use the following approach to rename a table:

  1. Create a new table with the desired name.
  2. Copy the data from the old table to the new table.
  3. Drop the old table if no longer needed.

Here’s an example of this approach:

-- Step 1: Create a new table with the desired name
CREATE TABLE new_table_name AS SELECT * FROM old_table_name;

-- Step 2: Copy the data from the old table to the new table
-- Optionally, you can add conditions to select specific data
INSERT INTO new_table_name SELECT * FROM old_table_name;

-- Step 3: If you want to remove the old table
-- Be careful, as this deletes the old table and its data
DROP TABLE old_table_name;

Remember to back up your data and exercise caution when renaming tables, especially in a production environment.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS