How to update / insert data from a table belong to the database "A" to the table, relate to the database "B"?

how to update / insert data from a table belong to database "A" to the table, belong to database "B"?

for example, I have a table in the name ips, as shown below in database "A"

CREATE TABLE `ips` ( `id` int(10) unsigned NOT NULL DEFAULT '0', `begin_ip_num` int(11) unsigned DEFAULT NULL, `end_ip_num` int(11) unsigned DEFAULT NULL, `iso` varchar(3) DEFAULT NULL, `country` varchar(150) DEFAULT NULL ) ENGINE=InnoDB 

Suppose I have a second table, the country belongs to database "B"

 CREATE TABLE `country` ( `countryid` tinyint(3) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, `ordering` smallint(5) unsigned NOT NULL DEFAULT '0', `iso` char(2) NOT NULL, PRIMARY KEY (`countryid`) ) ENGINE=InnoDB 

Note: two databases are on the same server.

+4
source share
2 answers

You must prefix the table names with the database / schema name. Something like that:

 INSERT INTO `database B`.`country` (columns) SELECT columns FROM `database A`.`ips`; 

Of course, you should replace columns with the necessary column names and / or expressions to suit your needs.

+2
source

In SQLServer, it looks like this:

Paste in x select * from otherdatabase.owner.table

Which can be expanded to select columns, etc.

In Oracle, you may need a database link between them. It was a long time ago for me; -)

0
source

Source: https://habr.com/ru/post/1499601/


All Articles