Mysql different Database data table import data _mysql

Source: Internet
Author: User

Background

Now I have such a need:

The user table for database a needs to be imported into the account table of database B
User table field: Uid,username,email,password,regdate,salt
Account table field: Id,name,email,password,type,salt
The imported field has only Username,email,password,salt and regdate needs to meet a certain condition
There are several scenarios for writing SQL, mainly distinguishing between insert into and replace into

Situation

The imported data does not exist at all in the table in library B

Direct insert into is good, use the replace into effect

INSERT into ' B '-' account ' (name,password,email,salt)
SELECT username,password,email,salt from
' A '. ' Users '
WHERE regdate>1418313600

Part of the imported data exists

Data part exists in order to distinguish the need to add a unique index to two tables, this unique index (unique) must be the value in the imported field
I added a unique index to the username and name fields in two tables, respectively
Direct replacement

Replace into when the two-table data repeats (must be defined unique) at the time of the operation, the row is deleted (Note that it is an entire row) before the INSERT into

The result is that the Type field in the Account table, if it has a value, is deleted and reinserted, which becomes the default value.

REPLACE into ' B '-' account ' (name,password,email,salt)
SELECT username,password,email,salt from
' A '. ' Users '
WHERE regdate>1418313600

Ignore duplicates

With the Ignore keyword, if you encounter duplicates, do not operate when the forward

INSERT IGNORE into ' B '-' account ' (name,password,email,salt)
SELECT username,password,email,salt from
' A '. ' Users '
WHERE regdate>1418313600

Partially Updated

than if you only want to update the password and salt fields, you can use on DUPLICATE KEY update

INSERT into ' B '-' u_account ' (name,password,email,salt)
SELECT username,password,email,salt from
' A '. ' Bbs_ Ucenter_members '
WHERE regdate>1418313600 on DUPLICATE KEY UPDATE password=values (PASSWORD), salt=values (salt )

Summarize

Replace into more violent, online also have said prudent use, preferably with INSERT into instead

Questions

The above operation is two databases on the same server, directly a SQL can be done
What would you do if you were to guide two libraries on a different two server?
The current temporary approach:
Import the user table from a library to another server on the B library, and write a SQL fix
or query out the specified data, export SQL, and replace insert into insert ignore in SQL (with limitations)

The above mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.