首批通过分布式安全可靠测评,为关键业务系统打造
常用 SQL 迁移脚本
更新时间:2026-04-20 12:39:54
本文介绍如何通过一些常用 SQL 迁移脚本进行数据迁移。
表与表之间数据迁移
通过 INSERT INTO......SELECT...... 语句进行数据迁移。
语法格式:
INSERT INTO table2
SELECT [(col_name[, col_name] ...)] FROM table1
WHERE [expr];
| 参数 | 是否必填 | 描述 | 示例 |
|---|---|---|---|
| INSERT INTO table2 | 是 | 数据迁移的目的表 | insert into table_B |
| SELECT [(col_name[, col_name] ...)] | 是 | 选择需要迁移的列,如果要选中全部数据可以用'*'表示。 注意 选择的列数量需要与目的表中的列数量保持一致。 |
select id,name,height |
| FROM table1 | 是 | 数据迁移的源表 | from table_A |
| WHERE [expr] | 否 | 迁移数据的筛选条件;不填表示迁移 select 选中的所有行记录。 | where height > 95 |
语法示例:
下述语句示例展示了如何将表 table_A 中符合条件的数据插入表 table_B 中:
obclient> select * from table_A;
+----+------+--------+-------+
| id | name | height | glass |
+----+------+--------+-------+
| 1 | ali | 175 | no |
| 2 | lin | 162 | no |
| 3 | hei | 172 | no |
+----+------+--------+-------+
3 rows in set
obclient> select * from table_B;
Empty set
obclient> insert into table_B select id,name,height from table_A where height > 170;
Query OK, 2 rows affected
Records: 2 Duplicates: 0 Warnings: 0
obclient> select *from table_B;
+----+------+--------+
| id | name | height |
+----+------+--------+
| 1 | ali | 175 |
| 3 | hei | 172 |
+----+------+--------+
2 rows in set
资源单元迁移
通过 ALTER SYSTEM 语句进行资源单元迁移。
启动资源单元的迁移
obclient> ALTER SYSTEM MIGRATE UNIT = [unit_id] DESTINATION = [ip_port]取消资源单元的迁移
obclient> ALTER SYSTEM CANCEL MIGRATE UNIT unit_id;