课程作业
1.gsql命令连到数据库omm, r选项提供了对gsql命令的历史版本支持。
su - omm
gsql -r
或者
gsql -d omm -r
2.查看数据库的版本、版权信息
select version();
show server\_version;
\copyright
3.常见元命令使用
--\l命令,元命令\l的作用是显示openGauss数据库集簇中,目前有哪些数据库。
\l
omm=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+---------+-------+-------------------
omm | omm | UTF8 | C | C |
postgres | omm | UTF8 | C | C |
template0 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
template1 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
(4 rows)
–\conninfo命令,元命令\conninfo的作用是在gsql中,显示会话的连接信息。
\conninfoopenGauss-# \conninfo
You are connected to database "postgres" as user "omm" via socket in "/tmp" at port "5432".
–\c[onnect] [DBNAME]命令,元命令\ c[onnect] [DBNAME]的作用是在gsql中,切换连接的数据库postgres。
openGauss-# \c postgres
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "postgres" as user "omm".
–\du命令和\dg命令,元命令\dg命令与元命令\du命令的作用类似,都是显示openGauss数据库集簇中,目前有哪些用户和角色。
\du
\dg
–\db命令,元命令\db的作用是显示openGauss数据库集簇中,目前有哪些表空间。
\db
–\dn命令,元命令\dn的作用是显示当前数据库有哪些数据库模式。
\dn
openGauss=# \db
List of tablespaces
Name | Owner | Location
------------±------±---------
pg_default | omm |
pg_global | omm |
(2 rows)
–创建表
CREATE TABLE customer_t(C_customer_sk integer,c_customer_id char(5),c_first_name char(6),c_last_name char(8));
–插入数据
INSERT INTO customer_t (c_customer_sk, c_customer_id, c_first_name,c_last_name) VALUES (3769, 5, 'Grace','White');
\dt命令,命令\dt的作用是显示数据库中所有的表。
\dt
omm=# \dt
List of relations
Schema | Name | Type | Owner | Storage
--------+------------+-------+-------+----------------------------------
public | customer\_t | table | omm | {orientation=row,compression=no}
(1 row)
--\d TableName命令,元命令\d TableName的作用是查看某个表的信息。**
\d customer\_t
\--\di IndexName命令,查看索引信息,元命令\di IndexName的作用是查看某个索引的信息。**
create index idx_customer_id on customer_t(c_customer_id);
\di
-- 可以用\pset命令以不同的方法显示表:**
\pset border 2
SELECT * FROM customer_t;
-- 打开扩展表格式模式。
\x
4.使用两种方法,连到postgres数据库中
gsql -d postgres -p 5432 -r
和
gsql -r
5.测试gsql中的默认事务自动提交功能
openGauss=# show autocommit;
±-----------+
| autocommit |
±-----------+
| on |
±-----------+
(1 row)
–测试gsql中事务默认为自动提交功能
create table customer_new as select * from customer_t;
\q
重新登录后看到之前创建的表customer_new:
```
gsql -d postgres -p 5432 -r
\dt
### 6.测试gsql中的事务手动提交功能
-- Opengauss默认执行完一条语句后,立即提交。可以关闭自动提交功能:
-- 注意:此处设置ATUOCOMMIT必须用大写!
**--插入一些数据**
openGauss=# insert into customer_t(c_customer_sk, c_customer_id, c_first_name,c_last_name) VALUES (6885, 1, ‘Joes’, ‘Hunter’),(4321, 2, ‘Lily’,‘Carter’),(9527, 3, ‘James’, ‘Cook’),(9500, 4, ‘Lucy’, ‘Baker’);
openGauss=# INSERT 0 4
openGauss=#
--查看表中数据
openGauss=# select * from customer_t;
c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------±--------------±-------------±------------
3769 | 5 | Grace | White
6885 | 1 | Joes | Hunter
4321 | 2 | Lily | Carter
9527 | 3 | James | Cook
9500 | 4 | Lucy | Baker
(5 rows)
--执行回滚**
openGauss=# ROLLBACK;
**--检查是否回滚成功**
openGauss=# SELECT * FROM customer_t;
c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------±--------------±-------------±------------
3769 | 5 | Grace | White
(1 row)
#### 7.了解gsql相关帮助
**-连接数据库时,可以使用如下命令获取帮助信息。**
gsql --help
**--\h获取和SQL语法有关的帮助信息**
\h
**--\? 获取和元命令有关的帮助信息**
\?




