( 'Mateo', 'More', 1) Code language: SQL (Structured Query Language) ( sql ) VALUES( 'Sales'),( 'Markting'),( 'Finance'),( 'Accounting'),( 'Warehouses'),( 'Production') Code language: SQL (Structured Query Language) ( sql ) INSERT INTO employees(first_name,last_name,department_id) Second, we insert sample data into both employees and departments tables: INSERT INTO departments(dept_name) ) Code language: SQL (Structured Query Language) ( sql ) CREATE DATABASE IF NOT EXISTS hr Code language: SQL (Structured Query Language) ( sql ) CREATE TABLE departments (ĭepartment_id INT AUTO_INCREMENT PRIMARY KEY, MySQL RENAME TABLE examplesįirst, we create a new database named hr that consists of two tables: employees and departments for the demonstration. We will discuss this in more detail in the following examples. In addition, you must manually adjust other database objects such as views, stored procedures, triggers, foreign key constraints, etc., that reference to the table. If the name of the table changes, so the application code that refers to the table name needs to be changed as well. For example, you should investigate which applications are using the table. In terms of security, any existing privileges that we granted to the old table must be manually migrated to the new table.īefore renaming a table, you should evaluate the impact thoroughly. Note that you cannot use the RENAME TABLE statement to rename a temporary table, but you can use the ALTER TABLE statement to rename a temporary table. In addition to the tables, we can use the RENAME TABLE statement to rename views.īefore we execute the RENAME TABLE statement, we must ensure that there is no active transactions or locked tables. If the new table new_table_name does exist, the statement will fail. The old table ( old_table_name) must exist, and the new table ( new_table_name) must not. To change one or more tables, we use the RENAME TABLE statement as follows: RENAME TABLE old_table_name TO new_table_name Code language: SQL (Structured Query Language) ( sql ) MySQL provides us with a very useful statement that changes the name of one or more tables. Introduction to MySQL RENAME TABLE statementīecause business requirements change, we need to rename the current table to a new one to better reflect the new situation. Summary: in this tutorial, you will learn how to rename tables using MySQL RENAME TABLE statement and ALTER TABLE statement.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |