Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Revert backup table data to original table SQL

I have created a backup for my country table.

create table country_bkp as select * from country;

What SQL should I use to restore the country table to it's original state? I can do

insert into country select * from country_bkp;

but it will just have duplicate entries and probably fail as primary key would be same .

Is there an SQL command to merge data back?

Last alternative would be

DROP TABLE country;
create table country as select * from country_bkp;

but I want to avoid this as all the grants/permissions would get lost by this.

Other cleaner way would be

delete from country ;
insert into country select * from country_bkp;

But I am looking for more of a merge approach without having to clear data from original table.

like image 817
Aniket Thakur Avatar asked Dec 08 '22 03:12

Aniket Thakur


2 Answers

Instead of dropping the table, which, as you noted, would lose all the permission defitions, you could truncate it to just remove all the data, and then insert-select the old data:

TRUNCATE TABLE country;
INSERT INTO country SELECT * FROM county_bkp;
like image 125
Mureinik Avatar answered Dec 11 '22 10:12

Mureinik


Only One Solution to Recover Data from Backup table is Rename Original table with random name and than rename Backup table with Original Table name in case if Identity Insert is ON for Original Table.
for example
Original Table - Invoice
Back Up Table - Invoice_back

Now Rename these tables :

Original Table - Invoice_xxx
Back Up Table - Invoice

like image 35
m.rahul Avatar answered Dec 11 '22 10:12

m.rahul