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

openGauss每日一练第 2天 openGauss查询、更新和删除基本使用

原创 寒冰 2021-12-08
208

openGauss查询、更新和删除基本使用,在这一章节中基本强化了基本的查询,更新,删除的操作。

强制自己敲入所有的代码是学习熟悉数据库操作最好的方式,争取一遍输入正确所有的代码也是一种能力。 这里备注项没有学过自己查资料加入。

COMMENT ON COLUMN product.product_id IS ‘产品编号’;

COMMENT ON COLUMN product.product_name IS ‘产品名’;

COMMENT ON COLUMN product.category IS ‘种类’;
root@modb:~#
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)
Typehelpfor help.

omm=# CREATE TABLE customer_t
omm-# ( c_customer_sk integer,
omm(# c_customer_id char(5),
omm(# c_first_name char(6),
omm(# c_last_name char(8)
omm(# ) ;
ERROR: relation “customer_t” already exists
omm=# select * from customer_t
omm-# ;
c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------±--------------±-------------±------------
6885 | 101 | Joes | Hunter
4321 | 102 | Lily | Carter
9527 | 103 | James | Cook
9500 | 104 | Lucy | Baker
(4 rows)

omm=# delete from customer_t where c_first_name=‘Lucy’;
DELETE 0
omm=# delete from customer_t where c_first_name = ‘Lucy’;
DELETE 1
omm=# select * from customer_t;
c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------±--------------±-------------±------------
6885 | 101 | Joes | Hunter
4321 | 102 | Lily | Carter
9527 | 103 | James | Cook
(3 rows)

omm=# delete from customer_t;
DELETE 3
omm=# select * from cuntomer_t;
ERROR: relation “cuntomer_t” does not exist on gaussdb
LINE 1: select * from cuntomer_t;
^
omm=# select * from customer_t;
omm=# c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------±--------------±-------------±------------
(0 rows)

omm=# drop table customer_t;
DROP TABLE
omm=# CREATE TABLE product (
omm(# product_id INTEGER,
omm(# product_name char(30),
omm(# category char(20)
omm(# );
CREATE TABLE
omm=# COMMENT ON COLUMN product.product_id IS ‘产品编号’;
COMMENT
omm=#
omm=# COMMENT ON COLUMN product.product_name IS ‘产品名’;
COMMENT
omm=#
omm=# COMMENT ON COLUMN product.category IS ‘种类’;
COMMENT
omm=# select * from product;
product_id | product_name | category
------------±-------------±---------
(0 rows)

omm=# insert into product (product_id,product_name,category) values(1502,‘olympus camera’,‘electrncs’),(1601,‘lamaze’,‘toys’),
omm-# (1700,‘wait interface’,‘Books’),(1666,‘harry potter’,‘toys’);
INSERT 0 4
omm=# select * from product;
product_id | product_name | category
------------±-------------------------------±---------------------
1502 | olympus camera | electrncs
1601 | lamaze | toys
1700 | wait interface | Books
1666 | harry potter | toys
(4 rows)

omm=# select * from product limit 1;
product_id | product_name | category
------------±-------------------------------±---------------------
1502 | olympus camera | electrncs
(1 row)

omm=# select * from product limit 3;
WARNING: Session unused timeout.
FATAL: terminating connection due to administrator command
could not send data to server: Broken pipe
The connection to the server was lost. Attempting reset: Succeeded.
omm=#
omm=# select * from product limit 3;
product_id | product_name | category
------------±-------------------------------±---------------------
1502 | olympus camera | electrncs
1601 | lamaze | toys
1700 | wait interface | Books
(3 rows)

omm=# select * from product;
1601 | lamaze | toys
1700 | wait interface | Books
1666 | harry potter | toys
(4 rows)

omm=# product_id | product_name | category
------------±-------------------------------±---------------------
1502 | olympus camera | electrncs

omm=#
omm=# select * from product;
1601 | lamaze | toys
1700 | wait interface | Books
1666 | harry potter | toys
(4 rows)

omm=# product_id | product_name | category
------------±-------------------------------±---------------------
1502 | olympus camera | electrncs

omm=#
omm=#
omm=# omm=#
omm=#

omm=#
omm=# select * from product where product_id>1600;
product_id | product_name | category
------------±-------------------------------±---------------------
1601 | lamaze | toys
1700 | wait interface | Books
1666 | harry potter | toys
(3 rows)

omm=# update product set product_id=product_id-1000 where product_id>1600;
UPDATE 3
omm=# select * from product;
product_id | product_name | category
------------±-------------------------------±---------------------
1502 | olympus camera | electrncs
601 | lamaze | toys
700 | wait interface | Books
666 | harry potter | toys
(4 rows)

omm=# delete product where category=‘toys’;
DELETE 2
omm=# select * from product;
product_id | product_name | category
------------±-------------------------------±---------------------
1502 | olympus camera | electrncs
700 | wait interface | Books
(2 rows)

omm=# delete product;
omm=# DELETE 2

omm=# select * from product;
product_id | product_name | category
------------±-------------±---------
(0 rows)

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

评论