MySQL rename and overwrite an existing table - mysql

In MySQL user manual, it says the following command is way faster than the RENAME command since no table copy is required.
ALTER TABLE table1 RENAME TO table2;
http://dev.mysql.com/doc/refman/5.7/en/alter-table.html
However, is there anyway that we can quickly rename/overwrite a table into an existing table in MYSQL 5.6? I have tried the following command and it shows error "You have an error in your SQL synthax;"
ALTER TABLE table1 RENAME OVERWRITE TO table2;
Could any guru enlighten? Thanks.

Not quite sure where you saw that. But ...
RENAME TABLE old_table TO new_table;
This statement is equivalent to
the following ALTER TABLE statement:
ALTER TABLE old_table RENAME new_table;
Source: http://dev.mysql.com/doc/refman/5.7/en/rename-table.html
If you want to rename a table and a table with that name already exists, you need to first do.
DROP TABLE old_table
Because
MySQL checks the destination table name before checking whether the
source table exists. For example, if new_table already exists and
old_table does not, the following statement fails as shown here:
(gives an example)

Related

Request for changing table's name [duplicate]

Renaming a table is not working in MySQL
RENAME TABLE group TO member;
The error message is
#1064 - You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'group
RENAME TO member' at line 1
The query is working fine on other tables for me, but not with the table group.
group is a keyword (part of GROUP BY) in MySQL, you need to surround it with backticks to show MySQL that you want it interpreted as a table name:
RENAME TABLE `group` TO `member`;
added(see comments)- Those are not single quotes.
Please try
RENAME TABLE `oldTableName` TO `newTableName`
The MySQL syntax for RENAME TABLE statement is the following:
RENAME TABLE <old_table_name> TO <new_table_name>
In your query, you've used group which is one of the keywords in MySQL. Try to avoid MySQL keywords for names while creating tables, field names and so on.
ALTER TABLE old_table_name RENAME new_table_name;
or
RENAME TABLE old_table_name TO new_table_name;
Table name change
RENAME TABLE old_table_name TO new_table_name;
Rename a table in MySQL :
ALTER TABLE current_name RENAME new_name;
group - is a reserved word in MySQL, that's why you see such error.
#1064 - You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'group
RENAME TO member' at line 1
You need to wrap table name into backticks:
RENAME TABLE `group` TO `member`;
ALTER TABLE `group` RENAME `member`
group is keyword so you must have to enclose into group
For Mysql 5.6.18 use the following command
ALTER TABLE `old_table` RENAME TO `new_table`
Also if there is an error saying ".... near RENAME TO ..." try removing the tick `
RENAME TABLE tb1 TO tb2;
tb1 - current table name.
tb2 - the name you want your table to be called.
According to mysql docs: "to rename TEMPORARY tables, RENAME TABLE does not work. Use ALTER TABLE instead."
So this is the most portable method:
ALTER TABLE `old_name` RENAME `new_name`;
Try any of these
RENAME TABLE `group` TO `member`;
or
ALTER TABLE `group` RENAME `member`;
Rename table
Syntax
The syntax to rename a table in MySQL is:
ALTER TABLE table_name
RENAME TO new_table_name;
Example
Let's look at an example that shows how to rename a table in MySQL using the ALTER TABLE statement. or example:
ALTER TABLE contacts
RENAME TO people;
Running The Alter Command
1.Click the SQL tab at the top.
2.In the text box enter the following command: ALTER TABLE exampletable RENAME TO new_table_name;
3.Click the go button.
source : https://my.bluehost.com/hosting/help/2158
You can use
RENAME TABLE `group` TO `member`;
Use back tick (`) instead of single quote (').
Without giving the database name the table is can't be renamed in my case, I followed the below command to rename the table.
RENAME TABLE current_db.tbl_name TO current_db.tbl_name;
Right Click on View > New Query
And Type:
EXEC sp_rename 'Table', 'NewName'
Then Click on Run button at the top left corner of the page.

SELECT * INTO statement

I was trying to use this statement SELECT * INTO new_table FROM old_table but it's giving me the error of undeclared value. I wanted to create a back table from one existing table to another new table.
Do I have first to create another table? or am I missing something.
the link below shows that the same statement can be used, so I don't why it's not working for me
https://www.w3schools.com/sql/sql_select_into.asp
You should use the INSERT ... SELECT statement for MySQL instead.
INSERT INTO new_table
SELECT *
FROM old_table
But new_table must exist. If you have MySQL version >= 8.0.19, then you can use instead the following syntax:
INSERT INTO new_table TABLE old_table;
You may check 13.2.6.1 INSERT ... SELECT Statement.
Background information
The SELECT...INTO is not exactly standard, it has selective support by vendors.
The statement does exist in MySQL, but is used for "query result to be stored in variables or written to a file".

Rename a table in MySQL

Renaming a table is not working in MySQL
RENAME TABLE group TO member;
The error message is
#1064 - You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'group
RENAME TO member' at line 1
The query is working fine on other tables for me, but not with the table group.
group is a keyword (part of GROUP BY) in MySQL, you need to surround it with backticks to show MySQL that you want it interpreted as a table name:
RENAME TABLE `group` TO `member`;
added(see comments)- Those are not single quotes.
Please try
RENAME TABLE `oldTableName` TO `newTableName`
The MySQL syntax for RENAME TABLE statement is the following:
RENAME TABLE <old_table_name> TO <new_table_name>
In your query, you've used group which is one of the keywords in MySQL. Try to avoid MySQL keywords for names while creating tables, field names and so on.
ALTER TABLE old_table_name RENAME new_table_name;
or
RENAME TABLE old_table_name TO new_table_name;
Table name change
RENAME TABLE old_table_name TO new_table_name;
Rename a table in MySQL :
ALTER TABLE current_name RENAME new_name;
group - is a reserved word in MySQL, that's why you see such error.
#1064 - You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'group
RENAME TO member' at line 1
You need to wrap table name into backticks:
RENAME TABLE `group` TO `member`;
ALTER TABLE `group` RENAME `member`
group is keyword so you must have to enclose into group
For Mysql 5.6.18 use the following command
ALTER TABLE `old_table` RENAME TO `new_table`
Also if there is an error saying ".... near RENAME TO ..." try removing the tick `
RENAME TABLE tb1 TO tb2;
tb1 - current table name.
tb2 - the name you want your table to be called.
According to mysql docs: "to rename TEMPORARY tables, RENAME TABLE does not work. Use ALTER TABLE instead."
So this is the most portable method:
ALTER TABLE `old_name` RENAME `new_name`;
Try any of these
RENAME TABLE `group` TO `member`;
or
ALTER TABLE `group` RENAME `member`;
Rename table
Syntax
The syntax to rename a table in MySQL is:
ALTER TABLE table_name
RENAME TO new_table_name;
Example
Let's look at an example that shows how to rename a table in MySQL using the ALTER TABLE statement. or example:
ALTER TABLE contacts
RENAME TO people;
Running The Alter Command
1.Click the SQL tab at the top.
2.In the text box enter the following command: ALTER TABLE exampletable RENAME TO new_table_name;
3.Click the go button.
source : https://my.bluehost.com/hosting/help/2158
You can use
RENAME TABLE `group` TO `member`;
Use back tick (`) instead of single quote (').
Without giving the database name the table is can't be renamed in my case, I followed the below command to rename the table.
RENAME TABLE current_db.tbl_name TO current_db.tbl_name;
Right Click on View > New Query
And Type:
EXEC sp_rename 'Table', 'NewName'
Then Click on Run button at the top left corner of the page.

Create table in MySQL that matches another table?

I am using MySQL. I have a table called EMP, and now I need create one more table (EMP_TWO) with same schema, same columns, and same constraints. How can I do this?
To create a new table based on another tables structure / constraints use :
CREATE TABLE new_table LIKE old_table;
To copy the data across, if required, use
INSERT INTO new_table SELECT * FROM old_table;
Create table docs
Beware of the notes on the LIKE option :
Use LIKE to create an empty table based on the definition of another
table, including any column attributes and indexes defined in the
original table:
CREATE TABLE new_table LIKE original_table; The copy is created using the same
version of the table storage format as the original table. The SELECT
privilege is required on the original table.
LIKE works only for base tables, not for views.
CREATE TABLE ... LIKE does not preserve any DATA DIRECTORY or INDEX
DIRECTORY table options that were specified for the original table, or
any foreign key definitions.
If you want to copy only Structure then use
create table new_tbl like old_tbl;
If you want to copy Structure as well as data then use
create table new_tbl select * from old_tbl;
Create table in MySQL that matches another table?
Ans:
CREATE TABLE new_table AS SELECT * FROM old_table;
Why don't you go like this
CREATE TABLE new_table LIKE Select * from Old_Table;
or You can go by filtering data like this
CREATE TABLE new_table LIKE Select column1, column2, column3 from Old_Table where column1 = Value1;
For having Same constraint in your new table first you will have to create schema then you should go for data for schema creation
CREATE TABLE new_table LIKE Some_other_Table;
by only using the following command on MySQL command line 8.0 the following ERROR is displayed
[ mysql> select * into at from af;]
ERROR 1327 (42000): Undeclared variable: at
so just to copy the exact schema without the data in it you can use the create table with like statement as follows:
create table EMP_TWO like EMP;
and to copy table along with the data use:
create table EMP_TWO select * from EMP;
to only copy tables data after creating an empty table:
insert into EMP_TWO select * from EMP;

MySQL Can't RENAME TABLE

In MySQL,
I want to change my table's name.
For example:
RENAME TABLE old_table to old;
Than I can meet syntax error message.
But:
RENAME TABLE old_table to old_;
It is success.
I don't want to use _(underscore).
ALTER TABLE oldTableName
rename to newTableName