今天学习数据的导入,该操作日常也是用的比较多。
学习笔记:
- 表字段默认值:跟oracle区别的是,可以插入的时候指定default关键字;
这边说下语法比较不一样:
ALTER TABLE <table> ALTER COLUMN <column> SET DEFAULT 'XXX';
ALTER TABLE <table> ALTER COLUMN <column> DROP DEFAULT;
复制
整行一个默认值插入:
insert into <table> DEFAULT VALUES;
复制
使用COPY命令的拷贝
copy <table> to stdout; --将表数据输出到stdout
复制
copy <table> to '/xxx/xx/xxx.dat'; --将表数据拷贝到文件
复制
copy reason_t3 from '/home/omm/reason.dat'; --将数据从文件拷贝到表
复制
- 创建表
CREATE TABLE <table1> (LIKE <table2> );
复制
当然也支持 创建并复制数据
CREATE TABLE <table1> as select * from <table2>;
复制
课后练习:
1.创建表1并在表中插入数据,分别指定字段和整行为缺省值
openGauss=# create table t (id int,name char(30) default 'XXX');
CREATE TABLE
openGauss=# insert into t values (1,'a'),(1);
ERROR: VALUES lists must all be the same length
LINE 1: insert into t values (1,'a'),(1); ^
openGauss=# insert into t values (1,'a'),(1,default);
INSERT 0 2 ^
openGauss=# insert into t default values;
INSERT 0 1
openGauss=# select * from t;
id | name
----+--------------------------------
1 | a
1 | XXX
| XXX
(3 rows)
复制
2.创建表2并将表1的数据全部导入表2中
openGauss=# create table t2 (like t);
CREATE TABLE
openGauss=# insert into t2 select * from t;
INSERT 0 3
openGauss=# select * from t2;
id | name
----+--------------------------------
1 | a
1 | XXX
| XXX
(3 rows)
复制
3.创建表3和表4,并合并两个表的数据到表3
openGauss=# create table t3(a int,b int);
CREATE TABLE
openGauss=# insert into t3 values(1,1),(2,2);
INSERT 0 2
openGauss=# create table t4(a int,b int);
CREATE TABLE
openGauss=# insert into t4 values(3,3),(4,4);
INSERT 0 2 ^
openGauss=# merge into t3 using t4 on (t3.a=t4.a) when matched then update set t3.b=t4.b when not matched then insert values(t4.a,t4.b);
MERGE 2
openGauss=# select * from t3;
a | b
---+---
1 | 1
2 | 2
3 | 3
4 | 4
(4 rows)
openGauss=#
复制
4.将表3的数据输出到文件,再将文件中的数据导入到表5
openGauss=# copy t3 to '/home/omm/t3.dat';
COPY 4
openGauss=# create table t5 (like t3);
CREATE TABLE
openGauss=# copy t5 from '/home/omm/t3.dat';
COPY 4
openGauss=# select * from t5;
a | b
---+---
1 | 1
2 | 2
3 | 3
4 | 4
(4 rows)
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
openGauss荣获中国软件行业协会多奖项,技术升级再创行业新高度
openGauss
433次阅读
2025-04-30 14:30:58
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
MogDB
299次阅读
2025-04-17 10:41:41
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
云和恩墨
198次阅读
2025-04-16 09:52:02
GitCode 成 openGauss 新归宿,国产开源数据库里程碑事件
严少安
155次阅读
2025-04-27 11:37:53
荣誉时刻!openGauss认证证书快递已发,快来看看谁榜上有名!
墨天轮小教习
141次阅读
2025-04-23 17:39:13
单个执行机并行执行MySQL到openGauss数据迁移子任务
Clipnosis
123次阅读
2025-04-30 16:39:58
openGauss6.0.0适配操作系统自带的软件,不依赖三方库
来杯拿铁
90次阅读
2025-04-18 10:49:53
opengauss使用gs_probackup进行增量备份恢复
进击的CJR
85次阅读
2025-04-09 16:11:58
Postgresql数据库单个Page最多存储多少行数据
maozicb
78次阅读
2025-04-23 16:02:19
openGauss新特性 | openGauss-DataVec向量数据库特性介绍
openGauss
47次阅读
2025-04-17 10:41:47