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

openGauss每日一练第1天 | 学习表的基础命令

原创 大道至简 2021-12-11
713

今天学习了第一课,学习了表的创建和删除,以及数据的插入和查询;
以下为第一个课作业:

首先连接数据库

root@modb:~# su - omm
omm@modb:~$ 
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=# 

一、创建表products

1、创建表

CREATE TABLE products
(  product_id             integer,   
  product_name             char(30),    
  category              char(20)
) ;

2、字段增加注释

COMMENT ON COLUMN products.product_id IS '产品编号';
COMMENT ON COLUMN products.product_name IS '产品名字';
COMMENT ON COLUMN products.category IS '种类';

二、插入数据

1、单条插入

omm=# INSERT INTO products VALUES (1502, 'olympus camera', 'electrncs');
INSERT 0 1

2、多条插入

omm=# INSERT INTO products VALUES (1601, 'lamaze', 'toys'),(1700, 'wait interface', 'Books'),(1666, 'harry potter', 'toys');
INSERT 0 3
omm=# 

三、查询表的所有数据和总记录数

1、查询所有数据

omm=# select * from products;
 product_id |          product_name          |       category       
------------+--------------------------------+----------------------
       1502 | olympus camera                 | electrncs           
       1601 | lamaze                         | toys                
       1700 | wait interface                 | Books               
       1666 | harry potter                   | toys                
(4 rows)

2、查询总记录数

omm=# select count(*) from products;
 count 
-------
     4
(1 row)

四、查询所有种类数据,并按照种类的字段升序排序

omm=# select category from products order by category;
       category       
----------------------
 Books               
 electrncs           
 toys                
 toys                
(4 rows)

五、查询种类为toys的记录

omm=# select category from products where category='toys';
omm=#        category       
----------------------
 toys                
 toys                
(2 rows)

六、删除表

omm=# drop table products;
DROP TABLE
omm=# 

表已删除,查询报错:

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

文章被以下合辑收录

评论