IT박스

한 데이터베이스에서 다른 데이터베이스로 테이블을 복사하는 가장 쉬운 방법은 무엇입니까?

itboxs 2020. 6. 28. 19:22
반응형

한 데이터베이스에서 다른 데이터베이스로 테이블을 복사하는 가장 쉬운 방법은 무엇입니까?


데이터베이스가 다른 사용자에있을 때 한 데이터베이스의 테이블에서 다른 데이터베이스의 테이블로 데이터를 복사하는 가장 좋은 방법은 무엇입니까?

사용할 수 있다는 것을 알고 있습니다

INSERT INTO database2.table2 SELECT * from database1.table1

그러나 여기에서 문제는 모두이다 database1과는 database2다른 MySQL의 사용자 받고있다. 따라서 user1액세스 database1user2가능 database2합니다. 어떤 생각?


쉘 액세스 권한이 mysqldump있는 경우 컨텐츠를 덤프하여에 database1.table1파이프 할 mysqldatabase2있습니다. 여기서 문제 table1는 여전히 그렇습니다 table1.

mysqldump --user=user1 --password=password1 database1 table1 \
| mysql --user=user2 --password=password2 database2

다른 쿼리 로 이름을 바꿔야 table1수도 있습니다 table2. 다른 방법으로 sed를 사용하여 파이프 간 table1을 table2로 변경할 수 있습니다.

mysqldump --user=user1 --password=password1 database1 table1 \
| sed -e 's/`table1`/`table2`/' \
| mysql --user=user2 --password=password2 database2

table2가 이미 존재하면 테이블을 만들 수없는 첫 번째 mysqldump에 매개 변수를 추가 할 수 있습니다.

mysqldump --no-create-info --no-create-db --user=user1 --password=password1 database1 table1 \
| sed -e 's/`table1`/`table2`/' \
| mysql --user=user2 --password=password2 database2

CREATE TABLE db1.table1 SELECT * FROM db2.table1

여기서 db1은 대상이고 db2는 소스입니다.


PHPMyAdmin 을 사용하는 경우 정말 간단 할 수 있습니다. 다음 데이터베이스가 있다고 가정하십시오.

DB1 & DB2

DB1에는 DB2에 복사하려는 테이블 사용자가 있습니다.

PHPMyAdmin에서 DB1을 연 다음 users 테이블로 이동하십시오.

이 페이지에서 오른쪽 상단 "작업" 탭을 클릭하십시오 . 조작 아래 에서 (database.table)에 테이블 복사 섹션을 찾으십시오 .

당신은 끝났습니다!


MySql Workbench : 강력 추천

MySql Workbench의 데이터베이스 마이그레이션 도구

마이그레이션 문제를 쉽게 처리 할 수 ​​있습니다. MySql과 SqlServer간에 선택된 데이터베이스의 선택된 테이블을 마이그레이션 할 수 있습니다. 반드시 시도해보십시오.


MySQL에 Navicat을 사용합니다 ...

모든 데이터베이스 조작이 쉬워집니다!

Navicat에서 두 데이터베이스를 모두 선택한 다음 사용하면됩니다.

 INSERT INTO Database2.Table1 SELECT * from Database1.Table1

MySql Workbench의 내보내기 및 가져 오기 기능을 사용하십시오. 단계 :
1. 원하는 값을 선택하십시오

E.g. select * from table1; 
  1. 내보내기 버튼을 클릭하고 CSV로 저장하십시오.
  2. 첫 번째와 비슷한 열을 사용하여 새 테이블을 만듭니다.

    E.g. create table table2 like table1; 
    
  3. 새 테이블에서 모두 선택

    E.g. select * from table2;  
    
  4. 가져 오기를 클릭하고 2 단계에서 내 보낸 CSV 파일을 선택하십시오.

MySql Workbench의 내보내기 및 가져 오기 단추 샘플


나는 이것이 오래된 질문이라는 것을 알고 여기에 도착하는 사람이 더 나은 접근 방식을 얻도록 대답합니다.

5.6.10부터는 할 수 있습니다

CREATE TABLE new_tbl LIKE orig_tbl;

https://dev.mysql.com/doc/refman/5.7/en/create-table-like.html 문서를 참조하십시오.


테이블이 동일한 mysql 서버에 있으면 다음을 실행할 수 있습니다

CREATE TABLE destination_db.my_table SELECT * FROM source_db.my_table;
ALTER TABLE destination_db.my_table ADD PRIMARY KEY (id); 
ALTER TABLE destination_db.my_table MODIFY COLUMN id INT AUTO_INCREMENT;

또 다른 쉬운 방법이 있습니다.

  1. DB1을 사용하십시오. create create TB1 테이블;
    • 여기에 구문을 클립 보드에 복사하여 DB2에서 TB1을 작성하십시오.
  2. DB2를 사용하십시오.
    • paste the syntax here to create the table TB1

INSERT INTO DB2.TB1 SELECT * from DB1.TB1;


Try mysqldbcopy (documentation)

Or you can create a "federated table" on your target host. Federated tables allow you to see a table from a different database server as if it was a local one. (documentation)

After creating the federated table, you can copy data with the usual insert into TARGET select * from SOURCE


With MySQL Workbench you can use Data Export to dump just the table to a local SQL file (Data Only, Structure Only or Structure and Data) and then Data Import to load it into the other DB.

You can have multiple connections (different hosts, databases, users) open at the same time.


Is this something you need to do regularly, or just a one off?

You can do an export (eg using phpMyAdmin or similar) that will script out your table and its contents to a text file, then you could re-import that into the other Database.


use below steps to copy and insert some columns from one database table to another database table-

  1. CREATE TABLE tablename ( columnname datatype (size), columnname datatype (size));

2.INSERT INTO db2.tablename SELECT columnname1,columnname2 FROM db1.tablename;


IN xampp just export the required table as a .sql file and then import it to the required


One simple way to get all the queries you need is to use the data from information_schema and concat.

SELECT concat('CREATE TABLE new_db.', TABLE_NAME, ' LIKE old_db.', TABLE_NAME, ';') FROM `TABLES` WHERE TABLE_SCHEMA = 'old_db';

You'll then get a list of results that looks like this:

CREATE TABLE new_db.articles LIKE old_db.articles;
CREATE TABLE new_db.categories LIKE old_db.categories;
CREATE TABLE new_db.users LIKE old_db.users;
...

You can then just run those queries.

However it won't work with MySQL Views. You can avoid them by appending AND TABLE_TYPE = 'BASE TABLE' from the initial query:

참고 URL : https://stackoverflow.com/questions/12242772/easiest-way-to-copy-a-table-from-one-database-to-another

반응형