openGauss的SCHEMA的原理及管理介紹
?? 1.何為Schema摘要:
本篇介紹了openGauss的SCHEMA的原理及管理。
Schema又稱作模式。通過管理Schema,允許多個用戶使用同一數(shù)據(jù)庫而不相互干擾,可以將數(shù)據(jù)庫對象組織成易于管理的邏輯組,同時便于將第三方應用添加到相應的Schema下而不引起沖突。
?????? 數(shù)據(jù)庫(database)、用戶(user)、schema、和表的關系如下圖所示:

每個數(shù)據(jù)庫包含一個或多個Schema。數(shù)據(jù)庫中的每個Schema包含表和其他類型的對象。數(shù)據(jù)庫創(chuàng)建初始,默認具有一個名為public的Schema,且所有用戶都擁有此Schema的usage權限,只有系統(tǒng)管理員和初始化用戶可以在public Schema下創(chuàng)建函數(shù)、存儲過程和同義詞對象,其他用戶即使賦予create權限后也不可以創(chuàng)建上述三種對象。可以通過Schema分組數(shù)據(jù)庫對象。Schema類似于操作系統(tǒng)目錄,但Schema不能嵌套。
??????注意要點
相同的數(shù)據(jù)庫對象名稱可以應用在同一數(shù)據(jù)庫的不同Schema中 CREATE USER創(chuàng)建用戶的同時,系統(tǒng)會在執(zhí)行該命令的數(shù)據(jù)庫中,為該用戶創(chuàng)建一個同名的SCHEMA 可以把schema理解成操作系統(tǒng)的目錄(文件夾),所不同的是schema只有一層,不能嵌套創(chuàng)建
?? 2.Schema語法
? 2.1 創(chuàng)建SCHEMA
語法:CREATE SCHEMA schema_name [ AUTHORIZATION user_name ] ;
案例1:創(chuàng)建一個名為tpcds的模式
openGauss=# create schema tpcds;
openGauss=# \dn+ tpcds
List of schemas
Name | Owner | Access privileges | Description | WithBlockChain
-------+-------+-------------------+-------------+----------------
tpcds | omm | | | f

? 2.2 修改SCHEMA
修改模式的名稱語法:ALTER SCHEMA schema_name RENAME TO new_name; 修改模式的所有者語法:ALTER SCHEMA schema_name OWNER TO new_owner;
案例1:重命名tpcds為tpcds1
openGauss=# ALTER SCHEMA tpcds RENAME TO tpcds1;
ALTER SCHEMA
openGauss=# \dn+ tpcds1
List of schemas
Name | Owner | Access privileges | Description | WithBlockChain
--------+-------+-------------------+-------------+----------------
tpcds1 | omm | | | f
案例2:創(chuàng)建一個用戶jeames, 并將tpcds1的owner修改為jeames
openGauss=# CREATE USER jeames PASSWORD 'abcd@123';
CREATE ROLE
openGauss=# \dn
List of schemas
Name | Owner
-----------------+--------
blockchain | omm
cstore | omm
db4ai | omm
dbe_perf | omm
dbe_pldebugger | omm
dbe_pldeveloper | omm
jeames | jeames
mesdb | mesdb
pkg_service | omm
public | omm
snapshot | omm
sqladvisor | omm
tpcds1 | omm
(13 rows)
openGauss=# ALTER SCHEMA tpcds1 OWNER TO jeames;
ALTER SCHEMA
openGauss=# \dn+ tpcds1
List of schemas
Name | Owner | Access privileges | Description | WithBlockChain
--------+--------+-------------------+-------------+----------------
tpcds1 | jeames | | | f

案例3:在模式tpcds1中建表customer、插入記錄和查詢記錄
openGauss=# create table tpcds1.customer(id int, name char(30)); CREATE TABLE openGauss=# insert into tpcds1.customer values(1 ,'xxxx'); INSERT 0 1 openGauss=# select * from tpcds1.customer; id | name ----+-------------------------------- 1 | xxxx
? 2.3 刪除SCHEMA
語法:DROP SCHEMA schema_name; 注:不要隨意刪除pg_temp或pg_toast_temp開頭的模式,這些模式是系統(tǒng)內(nèi)部使用的
DROP SCHEMA tpcds1 CASCADE;

?????? 補充
要查看屬于某Schema下的表列表,請查詢系統(tǒng)視圖PG_TABLES: openGauss=# SELECT distinct(tablename),schemaname from pg_tables where schemaname = 'pg_catalog';

?? 3.Schema賦權
普通schema的create權限grant給其他用戶后,其他用戶即可以創(chuàng)建對象。
分別創(chuàng)建schema teacher_zhao2、teacher_zhao3(指定AUTHORIZATION),并把create權限賦給teacher_li:
openGauss=# create SCHEMA teacher_zhao2;
CREATE SCHEMA
openGauss=# \dn+ teacher_zhao2
List of schemas
Name | Owner | Access privileges | Description | WithBlockChain
---------------+-------+-------------------+-------------+----------------
teacher_zhao2 | omm | | | f
openGauss=# CREATE SCHEMA teacher_zhao3 AUTHORIZATION jeames;
CREATE SCHEMA
openGauss=# \dn+ teacher_zhao3
List of schemas
Name | Owner | Access privileges | Description | WithBlockChain
---------------+--------+-------------------+-------------+----------------
teacher_zhao3 | jeames | | | f
可以發(fā)現(xiàn)沒有指定AUTHORIZATION的情況下,默認創(chuàng)建用戶是clouduser(系統(tǒng)管理員),
而指定情況下,則創(chuàng)建用戶就是指定的用戶。
下面嘗試用jeames登錄,創(chuàng)建數(shù)據(jù)庫對象teacher_zhao2.t2:
openGauss=# grant create on SCHEMA teacher_zhao2 to jeames; openGauss=# grant all on all tables in schema teacher_zhao2 to jeames; openGauss=# grant select on teacher_zhao2.t2 to jeames; openGauss=> create table teacher_zhao2.t2(a int); openGauss=# \c postgres jeames openGauss=> create table teacher_zhao2.t2(a int);

以上就是openGauss的SCHEMA的原理及管理介紹的詳細內(nèi)容,更多關于openGauss SCHEMA管理的資料請關注腳本之家其它相關文章!
相關文章
詳解IDEA中便捷內(nèi)存數(shù)據(jù)庫H2的最簡使用方式
這篇文章主要介紹了詳解IDEA中便捷內(nèi)存數(shù)據(jù)庫H2的最簡使用方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
面向云服務的GaussDB全密態(tài)數(shù)據(jù)庫現(xiàn)狀及問題小結
全密態(tài)數(shù)據(jù)庫,顧名思義與大家所理解的流數(shù)據(jù)庫、圖數(shù)據(jù)庫一樣,就是專門處理密文數(shù)據(jù)的數(shù)據(jù)庫系統(tǒng),這篇文章主要介紹了面向云服務的GaussDB全密態(tài)數(shù)據(jù)庫,未來GaussDB會將該能力逐步開源到openGauss,與社區(qū)共同推進和完善全密態(tài)數(shù)據(jù)庫解決方案,一起打造數(shù)據(jù)庫安全生態(tài)2024-02-02
淺談為什么數(shù)據(jù)庫字段建議設置為NOT NULL
本文主要介紹了MySQL數(shù)據(jù)庫中將字段設置為NOT NULL的性能和優(yōu)缺點,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-12-12
SQLServer與Oracle常用函數(shù)實例對比匯總
這篇文章主要介紹了SQLServer與Oracle常用函數(shù)對比,需要的朋友可以參考下2014-06-06
postgres 數(shù)據(jù)庫中的數(shù)據(jù)轉換
postgres8.3以后,字段數(shù)據(jù)之間的默認轉換取消了。如果需要進行數(shù)據(jù)變換的話,在postgres數(shù)據(jù)庫中,我們可以用"::"來進行字段數(shù)據(jù)的類型轉換。2009-07-07

