Your cart is currently empty!
Author: alien
-
Khóa học miễn phí SQL – Rename Table nhận dự án làm có lương
SQL – Rename Table
Table of content
SQL provides two ways to rename an MySQL table. You can use either SQL RENAME TABLE or ALTER TABLE statement to change a table name in MySQL RDBMS.
The SQL RENAME TABLE Statement
You can change a MySQL table name using SQL RENAME TABLE statement.
Syntax
Following is the syntax of the SQL RENAME TABLE Statement −
RENAME TABLE table_name TO new_table_name;
Where, table_name is the current name of an existing table and new_table_name is the new name of the table.
Example: SQL RENAME TABLE Statement
Let us with the name CUSTOMERS which contains the personal details of customers including their name, age, address and salary etc. as shown below −
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Now, let us insert few records into this table using the INSERT statement as follows −
INSERT INTO CUSTOMERS VALUES (1, ''Ramesh'', 32, ''Ahmedabad'', 2000.00 ), (2, ''Khilan'', 25, ''Delhi'', 1500.00 ), (3, ''Kaushik'', 23, ''Kota'', 2000.00 ), (4, ''Chaitali'', 25, ''Mumbai'', 6500.00 ), (5, ''Hardik'', 27, ''Bhopal'', 8500.00 ), (6, ''Komal'', 22, ''Hyderabad'', 4500.00 ), (7, ''Muffy'', 24, ''Indore'', 10000.00 );
The table will be created as follows −
ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 2 Khilan 25 Delhi 1500.00 3 Kaushik 23 Kota 2000.00 4 Chaitali 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 6 Komal 22 Hyderabad 4500.00 7 Muffy 24 Indore 10000.00 Following SQL Query changes the name of the CUSTOMERS table to BUYERS −
RENAME TABLE CUSTOMERS to BUYERS;
Verification
Once you change the name of a table, you can start using the new table name in your SQL queries.
SELECT * FROM BUYERS;
If table name got changed successfully, then it should list down all the records which were available in CUSTOMERS table.
The SQL ALTER TABLE Statement
The ALTER TABLE statement can be used to change or modify the structure of an existing table i.e. using this statement you can add/delete columns, create/destroy indexes, change the of the existing columns, rename the columns and, we can even rename the table.
Syntax
Following is the syntax of the SQL ALTER TABLE statement to rename an existing table −
ALTER TABLE table_name RENAME [TO|AS] new_table_name
Example: SQL ALTER TABLE Statement
Following SQL ALTER TABLE statement will change the table name from BUYERS to CUSTOMERS.
ALTER TABLE BUYERS RENAME TO CUSTOMERS;
Verification
Once you change the name of the table to CUSTOMERS, you can start using this name in your SQL queries.
SELECT * FROM CUSTOMERS;
This will produce the following result:
ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 2 Khilan 25 Delhi 1500.00 3 Kaushik 23 Kota 2000.00 4 Chaitali 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 6 Komal 22 Hyderabad 4500.00 7 Muffy 24 Indore 10000.00 Renaming a Table in SQL Server
There isn”t a query in SQL Server that can rename a table directly. However, it does give you access to a stored procedure called sp_rename that enables you to rename a table.
The sp_rename is a system stored procedure (set of pre-built subroutines that perform tasks within the database) in SQL that can be used to rename various database objects including tables, columns, indexes, and constraints.
Syntax
Following is the basic syntax to rename a table in SQL Server −
EXEC sp_rename ''old_table_name'', ''new_table_name''
Here, you must ensure that old table name is present in the database and that new table name does not already exist. Otherwise, it will issue a warning. Second important point is to make sure that the table is not locked and there is no active transaction involving this table.
Example: Renaming a Table in SQL Server
Assume we already have the CUSTOMERS table in our database. Now, we are going to rename this table from CUSTOMERS to WORKERS, using the following query −
EXEC sp_rename ''CUSTOMERS'', ''WORKERS
Output
The result obtained is as shown below −
Completion time: 2023-08-15T19:21:49.1144163+05:30
Verification
We can verify whether the changes are reflected by retrieving its contents using the SELECT statement as follows −
SELECT * FROM WORKERS;
This will list down all the records available in WORKERS table as follows −
ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 2 Khilan 25 Delhi 1500.00 3 Kaushik 23 Kota 2000.00 4 Chaitali 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 6 Komal 22 Hyderabad 4500.00 7 Muffy 24 Indore 10000.00 Because you have renamed the table to WORKERS so if you try to get the details by using the old table name, it will throw an error showing that the table does not exist.
Rules to be followed while renaming tables
When renaming tables in SQL, there are some rules and best practices that should be followed to ensure that the renaming process goes smoothly and does not cause any unintended consequences or issues.
-
Avoid renaming system tables − System tables are tables that are created and used by the database management system itself. Renaming these tables can cause issues with the functioning of the database system, so it is generally not recommended to rename system tables.
-
Update all references to the table − After renaming a table, any , , triggers, or other database objects that reference the table will need to be updated to use the new name of the table. Failure to update these references can result in errors or issues with the functioning of the database system.
-
Test thoroughly − Before renaming a table in a production environment, it is important to test the renaming process thoroughly in a development or testing environment to ensure that all references to the table have been updated correctly and that the database system continues to function as expected.
-
Use a consistent naming convention − It is a good practice to use a consistent naming convention for tables and other database objects to make it easier to understand and maintain the database system. If you need to rename a table, consider following the same naming convention that you have used for other tables in the database.
-
Backup the database − Before renaming a table, it is recommended to create a to ensure that you have a restore point; in case anything goes wrong during the renaming process.
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
-
Khóa học miễn phí SQL – Clone Tables nhận dự án làm có lương
SQL – Clone Tables
There may be a situation when you need an exact copy of a table with the same columns, attributes, indexes, default values and so forth. Instead of spending time on creating the exact same version of an existing table, you can create a clone of the existing table.
SQL Cloning Operation allows to create the exact copy of an existing table along with its definition. There are three types of cloning possible using SQL in various RDBMS; they are listed below −
- Simple Cloning
- Shallow Cloning
- Deep Cloning
Simple Cloning in MySQL
Simple cloning operation creates a new replica table from the existing table and copies all the records in newly created table. To break this process down, a new table is created using the CREATE TABLE statement; and the data from the existing table, as a result of SELECT statement, is copied into the new table.
Here, clone table inherits only the basic column definitions like the NULL settings and default values from the original table. It does not inherit the indices and AUTO_INCREMENT definitions.
Syntax
Following is the basic syntax to perform simple cloning in MySQL−
CREATE TABLE new_table SELECT * FROM original_table;
Example
Consider the following existing CUSTOMERS table which will be cloned in next new few steps.
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
Now let”s use the following SQL statement to create NEW_CUSTOMERS table using the existing table CUSTOMERS.
CREATE TABLE NEW_CUSTOMERS SELECT * FROM CUSTOMERS;
Output
The output is displayed as −
Query OK, 7 rows affected (0.06 sec) Records: 7 Duplicates: 0 Warnings: 0
Verification
To verify whether the table has been cloned successfully, we can use the following SELECT query −
SELECT * FROM NEW_CUSTOMERS;
If NEW_CUSTOMERS table is created successfully, then it should get all the records which are available in CUSTOMERS table.
Shallow Cloning in MySQL
Shallow cloning operation creates a new replica table from the existing table but does not copy any data records into newly created table, so only new but empty table is created.
Here, the clone table contains only the structure of the original table along with the column attributes including indices and AUTO_INCREMENT definition..
Syntax
Following is the basic syntax to perform shallow cloning in MySQL RDBMS −
CREATE TABLE new_table LIKE original_table;
Example
Following is an example to create a shallow clone copy of the existing table CUSTOMERS.
CREATE TABLE SHALL_CUSTOMERS LIKE CUSTOMERS;
Output
The output is displayed as −
Query OK, 0 rows affected (0.06 sec)
Verification
To verify whether the table has been cloned successfully, we can use the following DESC table_name query −
DESC SHALL_CUSTOMERS;
This will display the following information about the SHALL_CUSTOMERS table which is just a replica of CUSTOMERS table −
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
ID | int(11) | NO | PRI | NULL | |
NAME | varchar(20) | NO | NULL | ||
AGE | int(11) | NO | NULL | ||
ADDRESS | char(25) | YES | NULL | ||
SALARY | decimal(18,2) | YES | NULL |
Deep Cloning in MySQL
Deep cloning operation is a combination of simple cloning and shallow cloning. It not only copies the structure of the existing table but also its data into the newly created table. Hence, the new table will have all the contents from existing table and all the attributes including indices and the AUTO_INCREMENT definitions.
Since it is a combination of shallow and simple cloning, this type of cloning will have two different queries to be executed: one with CREATE TABLE statement and one with INSERT INTO statement. The CREATE TABLE statement will create the new table by including all the attributes of existing table; and INSERT INTO statement will insert the data from existing table into new table.
Syntax
Following is the basic syntax to perform deep cloning in MySQL RDBMS −
CREATE TABLE new_table LIKE original_table; INSERT INTO new_table SELECT * FROM original_table;
Example
Following is an example to create a deep clone copy of the existing table CUSTOMERS. First step is to create a shallow clone of the existing table.
CREATE TABLE DEEP_CUSTOMERS LIKE CUSTOMERS;
The output is displayed as −
Query OK, 0 rows affected (0.06 sec)
Now second step is to copy all the records from the CUSTOMERS table to DEEP_CUSTOMERS.
INSERT INTO DEEP_CUSTOMERS SELECT * FROM CUSTOMERS;
Output
The output is displayed as −
Query OK, 7 rows affected (0.01 sec) Records: 7 Duplicates: 0 Warnings: 0
Verification
To verify whether the table has been cloned successfully, we can use the following SELECT query −
SELECT * FROM DEEP_CUSTOMERS;
If DEEP_CUSTOMERS table is cloned successfully, then it should get all the records which are available in CUSTOMERS.
Table Cloning in SQL Server
However, there is no direct way to fully clone a table in an SQL server. However, we have some work around to handle the situation.
SELECT…INTO STATEMENT
MS SQL Server can make use of the SELECT…INTO statement to create a new table and copies the data from an existing table into it. However, this command only copies the data and not the definition of it, thus, omitting constraints, indexes etc., if any. They need to be added separately if one wishes to have the exact same structure of the original table in their new table.
You can use the SELECT…INTO command to copy a table within the same database as well as across different databases.
Syntax
Following is the basic syntax of the SELECT…INTO statement −
SELECT * INTO new_table FROM original_table;
The above SQL command will create a table new_table using the structure of original_table and then it will copy all the data from original_table to new_table.
Example
Consider the following existing CUSTOMERS table which will be cloned in this section.
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
Now, if you want to clone the data of this table into a new table NEW_CUSTOMERS, let”s use the following SQL query as shown below −
SELECT * INTO NEW_CUSTOMERS FROM CUSTOMERS;
Output
The output will be displayed as −
(7 rows affected)
Verification
To verify whether all the data has been copied into the new table NEW_CUSTOMERS, we shall use the SQL SELECT statement as follows −
SELECT * FROM NEW_CUSTOMERS;
If NEW_CUSTOMERS table is created successfully, then it should get all the records which are available in CUSTOMERS table.
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc