暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练第13天|学习openGauss导入数据的操作

原创 赵敬星 2021-12-13
484

坚持学习openGauss数据库,坚持每天打卡。第十三天学习openGauss导入数据的操作。

连接opengauss

root@modb:~# su - omm omm@modb:~$ gsql -r gsql ((opengauss 2.0.0 build 78689da9) compiled at 2021-03-31 21:03:52 commit 0 last mr ) non-ssl connection (ssl connection is recommended when requiring high-security) type "help" for help. omm=#

1.创建表1并在表中插入数据,分别指定字段和整行为缺省值

omm=# create table tab1 omm-# ( omm(# r_reason_sk integer, omm(# r_reason_id character(16), omm(# r_reason_desc character(100) omm(# ); CREATE TABLE omm=# insert into tab1 values(1, 'aaaaaaaabaaaaaaa', 'reason1'); INSERT 0 1 ––明确字段为缺省值 omm=# insert into tab1 values(1, 'aaaaaaaabaaaaaaa', default); INSERT 0 1 ––明确整行为缺省值 omm=# insert into tab1 default values; INSERT 0 1

2.创建表2并将表1的数据全部导入表2中

omm=# create table tab2 omm-# ( omm(# r_reason_sk integer, omm(# r_reason_id character(16), omm(# r_reason_desc character(100) omm(# ); CREATE TABLE omm=# insert into tab2 select * from tab1; INSERT 0 3

3.创建表3和表4,并合并两个表的数据到表3

omm=# create table tab3 omm-# ( product_id integer, omm(# product_name varchar2(60), omm(# category varchar2(60) omm(# ); CREATE TABLE omm=# insert into tab3 values omm-# (1502, 'olympus camera', 'electrncs'), omm-# (1601, 'lamaze', 'toys'), omm-# (1666, 'harry potter', 'toys'), omm-# (1700, 'wait interface', 'books'); INSERT 0 4 omm=# create table tab4 omm-# ( product_id integer, omm(# product_name varchar2(60), omm(# category varchar2(60) omm(# ); CREATE TABLE omm=# insert into tab4 values omm-# (1501, 'vivitar 35mm', 'electrncs'), omm-# (1502, 'olympus ', 'electrncs'), omm-# (1600, 'play gym', 'toys'), omm-# (1601, 'lamaze', 'toys'), omm-# (1666, 'harry potter', 'dvd'); INSERT 0 5 omm=# merge into tab3 np omm-# using tab4 p omm-# on (np.product_id = p.product_id ) omm-# when matched then omm-# update set np.product_name = p.product_name, np.category = p.category omm-# when not matched then omm-# insert values (p.product_id, p.product_name, p.category); MERGE 5

4.将表3的数据输出到文件,再将文件中的数据导入到表5

omm=# copy tab3 to '/home/omm/tab3.dat'; COPY 6 omm=# create table tab5 omm-# ( product_id integer, omm(# product_name varchar2(60), omm(# category varchar2(60) omm(# ); CREATE TABLE omm=# copy tab5 from '/home/omm/tab3.dat'; COPY 6 omm=# select * from tab5; product_id | product_name | category ------------+----------------+----------- 1700 | wait interface | books 1501 | vivitar 35mm | electrncs 1502 | olympus | electrncs 1600 | play gym | toys 1601 | lamaze | toys 1666 | harry potter | dvd (6 rows)

通过学习openGauss导入数据操作,加上作业里面的练习,更加深入理解openGauss数据库的导出导入操作。

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论