華為云計(jì)算 云知識 【云小課】云數(shù)據(jù)庫GaussDB(for openGauss)語法入門
【云小課】云數(shù)據(jù)庫GaussDB(for openGauss)語法入門

云小課必用.png

之前我們講了如何連接 數(shù)據(jù)庫 實(shí)例,那連接數(shù)據(jù)庫后如何使用數(shù)據(jù)庫呢?那么我們今天就帶大家了解一下GaussDB(for openGauss),以下簡稱GaussDB的基本語法。

學(xué)習(xí)本節(jié)課程之后,您將可以完成創(chuàng)建數(shù)據(jù)庫、創(chuàng)建表及向表中插入數(shù)據(jù)和查詢表中數(shù)據(jù)等操作。

前提條件

?   GaussDB(for openGauss)實(shí)例正常運(yùn)行。

?   已通過DAS或gsql連接數(shù)據(jù)庫實(shí)例。

操作步驟

1.   通過DAS或gsql連接數(shù)據(jù)庫實(shí)例。

2.   創(chuàng)建數(shù)據(jù)庫用戶。

      默認(rèn)只有創(chuàng)建實(shí)例時(shí)的管理員用戶可以訪問初始數(shù)據(jù)庫,您還可以手動創(chuàng)建其他數(shù)據(jù)庫用戶帳號。

postgres=# CREATE USER joe WITH PASSWORD "xxxxxxxx";

      xxxxxxxx需要替換為指定的密碼,當(dāng)結(jié)果顯示為如下信息,則表示創(chuàng)建成功。

CREATE ROLE

      如上創(chuàng)建了一個(gè)用戶名為joe,密碼為xxxxxxxxx的用戶。

      如下命令為設(shè)置joe用戶為系統(tǒng)管理員。

postgres=# GRANT ALL PRIVILEGES TO joe;

     

3.   創(chuàng)建數(shù)據(jù)庫。

postgres=#  CREATE DATABASE db_tpcds;

      當(dāng)結(jié)果顯示為如下信息,則表示創(chuàng)建成功。

CREATE DATABASE

      創(chuàng)建完db_tpcds數(shù)據(jù)庫后,就可以按如下方法退出postgres數(shù)據(jù)庫,使用新用戶連接到此數(shù)據(jù)庫執(zhí)行接下來的創(chuàng)建表等操作。當(dāng)然,也可以選擇繼續(xù)在默認(rèn)的postgres數(shù)據(jù)庫下做后續(xù)的體驗(yàn)。

postgres=#  \q  
gsql -d db_tpcds -p 8000 -U joe  
Password for user joe:  
gsql  compiled at 2020-05-08 02:59:43 commit 2143 last mr 131)  
Non-SSL connection (SSL connection is recommended when requiring high-security)  
Type "help" for help.  
   
db_tpcds=> 

4.   創(chuàng)建表。

   ?   創(chuàng)建一個(gè)名稱為mytable,只有一列的表。字段名為firstcol,字段類型為integer。

db_tpcds=>  CREATE TABLE mytable (firstcol int);

       未使用“DISTRIBUTE BY”指定分布列時(shí),系統(tǒng)默認(rèn)會指定第一列為哈希分布列,且給出提示。系統(tǒng)返回信息以“CREATE TABLE”結(jié)束,表示創(chuàng)建表成功。

NOTICE:  The 'DISTRIBUTE BY' clause is not specified. Using 'firstcol' as the distribution column by default. 
HINT:  Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column. 
CREATE TABLE

   ?   向表中插入數(shù)據(jù):

db_tpcds=> INSERT INTO mytable values (100);

       當(dāng)結(jié)果顯示為如下信息,則表示插入數(shù)據(jù)成功。

INSERT 0 1

   ?  查看表中數(shù)據(jù):

db_tpcds=> SELECT * from mytable; 
 firstcol  
---------- 
      100 
(1 row)

----結(jié)束