本章将对YashanDB内置导入导出工具imp和exp进行介绍及提供基础示例。
exp工具是YashanDB的配套导出工具,提供元数据导出及CSV导出能力;imp为YashanDB的配套导入工具,提供元数据导入能力。
用户可通过使用exp工具将YashanDB数据库中的表结构、索引、约束等所有数据生成一个元数据文件,该元数据文件可通过配套的导入工具imp导入至同构的YashanDB数据库中。或者通过exp工具将指定表结构排列的数据导出至CSV文件中,并通过yasldr工具将该CSV文件导入至YashanDB数据库中。
导入前准备
1.准备导入用户:
执行如下命令连接YashanDB数据库,请将password更改成设置的sys用户密码:
$ yasql sys/password YashanDB SQL Personal Edition Release 23.3.1.100 x86_64 Connected to: YashanDB Server Personal Edition Release 23.3.1.100 x86_64 - Linux SQL>
执行如下SQL语句创建用户import_user,并为其指定密码import:
CREATE USER import_user IDENTIFIED BY import;
- 执行如下SQL语句给import_user用户授予DBA权限:
GRANT DBA TO import_user;
执行如下SQL语句切换至import_user用户:
conn import_user/import Connected to: YashanDB Server Personal Edition Release 23.3.1.100 x86_64 - Linux
执行如下SQL语句于import_user用户中创建表并插入数据:
CREATE TABLE classmate_info(c1 INT,c2 CHAR(10)); INSERT INTO classmate_info VALUES(1,'h'),(2,'a'),(3,'c'); CREATE TABLE classmate_info1(c1 INT,c2 INT); INSERT INTO classmate_info1 VALUES(1,2),(3,4),(5,6); COMMIT;
导出数据
1.执行如下SQL语句退出YashanDB数据库:
SQL> exit
$
2.以安装用户登录数据库所在服务器,执行如下命令将import_user用户下所有元数据导出至export.owner.export文件中,请将password更改成设置的sys用户密码:
$ exp sys/password FILE=export.owner.export OWNER=import_user
YashanDB Export Release 23.3.1.100 x86_64 297f388
Start export user [IMPORT_USER] from dba view
Exporting tablespaces...
Exporting database links...
Exporting sequences...
Exporting object synonyms...
Exporting type dependencies synonyms...
Exporting types...
Exporting tables...
exporting table IMPORT_USER.CLASSMATE_INFO 3 rows exported
exporting table IMPORT_USER.CLASSMATE_INFO1 3 rows exported
Exporting access constraints...
Exporting indexes...
Exporting other constraints...
Exporting primary keys...
Exporting foreign keys...
Exporting other objects...
export terminated successfully
3.执行如下命令查看导出的元数据文件:
$ ll
total 24
-rw-r----- 1 yashan yashan 16771 Aug 14 21:08 export.owner.export
drwxrwxr-x 12 yashan yashan 259 Aug 8 16:33 install
drwxr----- 3 yashan yashan 20 Aug 8 16:45 yasdb_data
drwxrwxr-x 3 yashan yashan 22 Aug 8 16:41 yasdb_home
导入数据
1.执行如下命令连接YashanDB数据库:
$ yasql import_user/import
YashanDB SQL Personal Edition Release 23.3.1.100 x86_64
Connected to:
YashanDB Server Personal Edition Release 23.3.1.100 x86_64 - Linux
SQL>
2.执行如下SQL语句删除表classmate_info和classmate_info1:
DROP TABLE classmate_info;
DROP TABLE classmate_info1;
3.通过查询USRE_TABLES视图查看当前用户下所有表信息,此时import_user用户下不存在任何表:
SELECT table_name FROM USER_TABLES;
TABLE_NAME
----------------------------------------------------------------
4.执行如下命令退出YashanDB数据库:
SQL> exit
$
5.以安装用户登录数据库所在服务器,执行如下命令将元数据文件导入至import_user用户,请将password更改成设置的sys用户密码:
$ imp sys/password FILE=export.owner.export FROMUSER=import_user
YashanDB Import Release 23.3.1.100 x86_64 297f388
Start import User [IMPORT_USER] from file export.owner.export
Checking tablespace...
Switch to owner IMPORT_USER
Importing table CLASSMATE_INFO 3 rows imported
Importing table CLASSMATE_INFO1 3 rows imported
import terminated successfully
验证数据
1.登录数据库并查看用户表信息:
$ yasql import_user/import
YashanDB SQL Personal Edition Release 23.3.1.100 x86_64
Connected to:
YashanDB Server Personal Edition Release 23.3.1.100 x86_64 - Linux
SQL> SELECT table_name FROM USER_TABLES;
TABLE_NAME
----------------------------------------------------------------
classmate_info1
classmate_info
2.执行如下SQL语句查看表classmate_info和classmate_info1表中的数据:
SELECT c1,c2 FROM classmate_info;
C1 C2
------------ -------------
1 h
2 a
3 c
SELECT c1,c2 FROM classmate_info1;
C1 C2
------------ ------------
1 2
3 4
5 6
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。