데이터베이스 구조를 복제하는 저장 과정
DELIMITER $$
DROP PROCEDURE IF EXISTS `test_copy`.`sp_copy_db_schema`$$
CREATE PROCEDURE `test_copy`.`sp_copy_db_schema`(IN db_from varchar(64),IN db_to varchar(64))
BEGIN
declare i int default 0;
declare cnt int default 0;
-- Get the columns number
select count(1) from information_schema.tables where table_schema = db_from into cnt;
-- Loop table name one time
while i < cnt do
-- Danymic query
set @stmt1 = concat('select table_name from information_schema.tables where table_schema = ''',db_from,''' limit ',i,
',1 into @tb_name;');
prepare s1 from @stmt1;
execute s1;
deallocate prepare s1;
set @stmt1 = '';
-- Begin copy schema to new database
set @stmt2 = concat('create table if not exists ',db_to,'.',@tb_name,' select * from ',db_from,'.',@tb_name,' where 1 != 1');
prepare s2 from @stmt2;
execute s2;
deallocate prepare s2;
set @stmt2 = '';
-- Ready for the next table
set i = i + 1;
end while;
END$$
DELIMITER ;
테스트:
라이브러리: TEST
대상 라이브러리:TESTCOPY
----------------
mysql> show tables from test\G
*************************** 1. row ***************************
Tables_in_test: a
*************************** 2. row ***************************
Tables_in_test: animals
*************************** 3. row ***************************
Tables_in_test: b
*************************** 4. row ***************************
Tables_in_test: cubeexample
*************************** 5. row ***************************
Tables_in_test: employees
*************************** 6. row ***************************
Tables_in_test: eugene
*************************** 7. row ***************************
Tables_in_test: ewp
*************************** 8. row ***************************
Tables_in_test: info
*************************** 9. row ***************************
Tables_in_test: ip
*************************** 10. row ***************************
Tables_in_test: lk
*************************** 11. row ***************************
Tables_in_test: lk2
*************************** 12. row ***************************
Tables_in_test: lk3
*************************** 13. row ***************************
Tables_in_test: members
*************************** 14. row ***************************
Tables_in_test: mocha_document_content
*************************** 15. row ***************************
Tables_in_test: no_part_tab
*************************** 16. row ***************************
Tables_in_test: os_propertyentry
*************************** 17. row ***************************
Tables_in_test: part_tab
*************************** 18. row ***************************
Tables_in_test: person
*************************** 19. row ***************************
Tables_in_test: rooms
*************************** 20. row ***************************
Tables_in_test: score
*************************** 21. row ***************************
Tables_in_test: shirt
*************************** 22. row ***************************
Tables_in_test: song
*************************** 23. row ***************************
Tables_in_test: song2
*************************** 24. row ***************************
Tables_in_test: t1
*************************** 25. row ***************************
Tables_in_test: t11
*************************** 26. row ***************************
Tables_in_test: t2
*************************** 27. row ***************************
Tables_in_test: table1
*************************** 28. row ***************************
Tables_in_test: table2
*************************** 29. row ***************************
Tables_in_test: table_a
*************************** 30. row ***************************
Tables_in_test: temp
*************************** 31. row ***************************
Tables_in_test: temp2
*************************** 32. row ***************************
Tables_in_test: test1
*************************** 33. row ***************************
Tables_in_test: total_user
33 rows in set (0.01 sec)
---------------------
mysql> show tables from test_copy\G
Empty set (0.00 sec)
-----
mysql> call sp_copy_db_schema('test','test_copy');Query OK, 0 rows affected (13.97 sec)
--------------
mysql> show tables from test_copy\G
*************************** 1. row ***************************
Tables_in_test_copy: a
*************************** 2. row ***************************
Tables_in_test_copy: animals
*************************** 3. row ***************************
Tables_in_test_copy: b
*************************** 4. row ***************************
Tables_in_test_copy: cubeexample
*************************** 5. row ***************************
Tables_in_test_copy: employees
*************************** 6. row ***************************
Tables_in_test_copy: eugene
*************************** 7. row ***************************
Tables_in_test_copy: ewp
*************************** 8. row ***************************
Tables_in_test_copy: info
*************************** 9. row ***************************
Tables_in_test_copy: ip
*************************** 10. row ***************************
Tables_in_test_copy: lk
*************************** 11. row ***************************
Tables_in_test_copy: lk2
*************************** 12. row ***************************
Tables_in_test_copy: lk3
*************************** 13. row ***************************
Tables_in_test_copy: members
*************************** 14. row ***************************
Tables_in_test_copy: mocha_document_content
*************************** 15. row ***************************
Tables_in_test_copy: no_part_tab
*************************** 16. row ***************************
Tables_in_test_copy: os_propertyentry
*************************** 17. row ***************************
Tables_in_test_copy: part_tab
*************************** 18. row ***************************
Tables_in_test_copy: person
*************************** 19. row ***************************
Tables_in_test_copy: rooms
*************************** 20. row ***************************
Tables_in_test_copy: score
*************************** 21. row ***************************
Tables_in_test_copy: shirt
*************************** 22. row ***************************
Tables_in_test_copy: song
*************************** 23. row ***************************
Tables_in_test_copy: song2
*************************** 24. row ***************************
Tables_in_test_copy: t1
*************************** 25. row ***************************
Tables_in_test_copy: t11
*************************** 26. row ***************************
Tables_in_test_copy: t2
*************************** 27. row ***************************
Tables_in_test_copy: table1
*************************** 28. row ***************************
Tables_in_test_copy: table2
*************************** 29. row ***************************
Tables_in_test_copy: table_a
*************************** 30. row ***************************
Tables_in_test_copy: temp
*************************** 31. row ***************************
Tables_in_test_copy: temp2
*************************** 32. row ***************************
Tables_in_test_copy: test1
*************************** 33. row ***************************
Tables_in_test_copy: total_user
33 rows in set (0.00 sec)
본문은'하느님, 우리는 만나지 않으면 흩어지지 않습니다!'에서 나온 것이다.블로그, 전재는 작가에게 연락하세요!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Java 호출 Oracle 스토리지 프로세스 상세 정보Java 호출 Oracle 스토리지 프로세스 상세 정보 단계: 1. Oracle 스토리지 프로세스 작성 2. 데이터베이스 작성 연결 도구 클래스 얻기 3. 간단한 응용 프로그램 호출 저장 프로세스 작성 구현: 1. ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.