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

openGauss每日一练第13天 课程笔记和作业

数据库环境

openGauss:2.0.0 - 数据库实训平台

学习目标

学习openGauss导入数据

学习笔记

  • 明确为缺省值
omm=# insert into reason_t1 values(1, 'AAAAAAAABAAAAAAA', DEFAULT); INSERT 0 1 omm=# insert into reason_t1 DEFAULT VALUES; INSERT 0 1
  • 将查询结果作为插入的数据
omm=# INSERT INTO reason_t2 SELECT * FROM reason_t1; INSERT 0 4
  • MERGE INTO 合并两个表的数据
omm=# MERGE INTO newproducts np omm-# USING products 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 4
  • 数据在表和文件之间的互传
omm=# copy reason_t1 to '/home/omm/reason.dat'; COPY 4 omm=# copy reason_t3 from '/home/omm/reason.dat'; COPY 4

课后作业

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

omm=# create table t1(id integer,name char(30)); CREATE TABLE omm=# select * from t1; id | name ----+------ (0 rows) omm=# insert into t1 values omm-# (1,'tim'), omm-# (2,default); INSERT 0 2 omm=# select * from t1; id | name ----+-------------------------------- 1 | tim 2 | (2 rows) omm=# insert into t1 default values; INSERT 0 1 omm=# select * from t1; id | name ----+-------------------------------- 1 | tim 2 | | (3 rows)

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

omm=# create table t2 (id integer,name char(30)); CREATE TABLE omm=# insert into t2 select * from t1; INSERT 0 3 omm=# select * from t2; id | name ----+-------------------------------- 1 | tim 2 | | (3 rows)

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

omm=# create table t3(id integer,username VARCHAR2(30)); CREATE TABLE omm=# insert into t3 values omm-# (1,'tim'), omm-# (2,'rose'), omm-# (3,'tom'), omm-# (4,'jerry'); INSERT 0 4 omm=# select * from t3; id | username ----+---------- 1 | tim 2 | rose 3 | tom 4 | jerry (4 rows) omm=# create table t4(id integer,username VARCHAR2(30)); CREATE TABLE omm=# insert into t4 values omm-# (1,'tim_t4'), omm-# (2,'lily'), omm-# (3,'tom_t4'), omm-# (4,'jerry_t4'), omm-# (5,'sun'); INSERT 0 5 omm=# select * from t4; id | username ----+---------- 1 | tim_t4 2 | lily 3 | tom_t4 4 | jerry_t4 5 | sun (5 rows) omm=# merge into t3 omm-# using t4 omm-# on (t3.id = t4.id) omm-# when matched then omm-# update set t3.username = t4.username omm-# when not matched then omm-# insert values (t4.id,t4.username); MERGE 5 omm=# select * from t3; id | username ----+---------- 1 | tim_t4 2 | lily 3 | tom_t4 4 | jerry_t4 5 | sun (5 rows)

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

omm=# select * from t3; id | username ----+---------- 1 | tim_t4 2 | lily 3 | tom_t4 4 | jerry_t4 5 | sun (5 rows) omm=# copy t3 to '/home/omm/t3.dat'; COPY 5 omm=# \q omm@modb:~$ cd /home/omm/ omm@modb:~$ ls reason.dat t3.dat omm@modb:~$ cat t3.dat 1 tim_t4 2 lily 3 tom_t4 4 jerry_t4 5 sun omm=# create table t5 (like t3); CREATE TABLE omm=# \d t5 Table "public.t5" Column | Type | Modifiers ----------+-----------------------+----------- id | integer | username | character varying(30) | omm=# copy t5 from '/home/omm/t3.dat'; COPY 5 omm=# select * from t5; id | username ----+---------- 1 | tim_t4 2 | lily 3 | tom_t4 4 | jerry_t4 5 | sun (5 rows) omm=#

学习资源


欢迎各位同学一起来交流学习心得!

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

评论