How to insert multiple tables into their respective backup tables using insert sql?

Solutions

You should use

Merge Queries

a Merge query is a combination of an Insert query and an Update query. If a given condition is met, such as a row with a given primary key already existing, then an Update query is run. If not, an Insert query is run.

The above is for drupal

If you are using mysql then you can use UPDATE or ON DUPLICATE KEY UPDATE

I have resolved the issue by doing following thing, hope it may be useful to anyone too.

The cause of the problem is that the parent table and its corresponding backup table have same schema so whenever Restore process is took place it just encounter a problem due to same primary key on both tables. Hence to overcome it,first remove Primary Key from backup table as:

ALTER TABLE $backup_tbl_name DROP PRIMARY KEY, CHANGE $primary_key $primary_key int(11) NOT NULL

and then add a new column 'ID' to all backup tables using :

ALTER TABLE $backup_tbl_name ADD `id` BIGINT unsigned NOT NULL   AUTO_INCREMENT COMMENT 'Auto increment field for backup table.' PRIMARY KEY

and making this newly added column Primary key of backup table.

And at last INSERT query has to be modified as well as during insertion newly added column also has to be pass. And it modified as:

INSERT INTO $backup_tbl_name SELECT * , NULL FROM $tbl;
Tags: Database

Similar questions

on submission of form respective form should open
in my form i have to show two radio button and one submit button, for example candidate(radio) organization(radio) submit button. my requirement is that if user click/check on candidate and submit then candidate form should be open. or if user click/check on organization and submit then organization form should be open. my code:
how to display sub menu under the respective main menu
I want to display main menu in custom menu block and display all sub menu below the main menu block on clicking the respective main menu. Iam able to display main menu but not able to display respective sub menus below the clicked main menu. Please help I would like to give a reference if my reqiurement is not clear. The submenu created in the site...
Setting Visibility Rules in Drupal Views respective to particular users
I'm setting up an administrative panel page in Durpal that pulls three views. The view that will be displayed depends on the logged in administrator, thus each of the three administrators will only see the view applicable to them. I've added the following visibility rule to all three views (with the respective uid's). It works. Now, I want to chang...
Main differences between registry and file_registry tables in Drupal SQL tables structure?
I already know that in computing in general, registry is the main data structure for paths (whether as a table in DB or a as a standalone DB). In the case of Drupal, I saw two tables in the DB's. When is "registry" and the second is "file_registry". While I understand that file_registry table holds paths for files, I wonder what are the main kinds ...
Drupal 8 - multisite with shared tables (user tables / all tables)?
I need to create about 20-30 Drupal8 sites on different domains. There will be similar content (difference only in details like city name, ajax calls, etc.) but also there will be a specific content like news. I know all weakness of this idea, but anyway I think that shared tables in one database will be the best solution for this project. My steps...
SQL query for multiple tables using JOIN
Need help with SQL query - my query at the moment works with two tables I want to add third table, but i have no idea how to do that, I tried many times but it gives error... I will try to show all my code as more acurate: SQL QUERY this QUERY grabs all data from table NODE based on website language where type is drug. And give me a list of all ite...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.