|
1、概述 一個OushuDB集群管理著多個數(shù)據(jù)庫(database),每個數(shù)據(jù)庫又包含多個模式(schema), 一個模式包含多個對象(表,視圖,函數(shù)等),所以這些對象之間的層級結(jié)構(gòu)為: database -> schema -> (tables, functions, views) 每個模式,表,視圖,函數(shù)等只屬于一個database。本章主要介紹每一種對象的常見用法。具體使用語法可以參見參考手冊。 2、數(shù)據(jù)庫 OushuDB在初始化完成后,會默認生成三個數(shù)據(jù)庫,可以使用l命令查看,或者查看 postgres=# l List of databases Name | Owner | Encoding | Access privileges -----------+----------+----------+------------------- postgres | ChangLei | UTF8 | template0 | ChangLei | UTF8 | template1 | ChangLei | UTF8 | (4 rows) 其中template0和template1為模版數(shù)據(jù)庫。template1為系統(tǒng)默認用來創(chuàng)建新數(shù)據(jù)庫的模版數(shù)據(jù)庫,用戶可以修改。template0默認不接受連接,所以不可更改,目的是始終保存一個干凈的模版數(shù)據(jù)庫。 創(chuàng)建一個數(shù)據(jù)庫的時候,可以指定一個數(shù)據(jù)庫的模版數(shù)據(jù)庫。缺省為template1,現(xiàn)在OushuDB只支持以template0,template1和postgres數(shù)據(jù)庫為模版數(shù)據(jù)庫。例如: postgres=# create database tdb; # 創(chuàng)建一個新數(shù)據(jù)庫,默認以template0為模版 CREATE DATABASE postgres=#c postgres # 連接postgres postgres=# create table test(i int); # 在postgres數(shù)據(jù)庫中創(chuàng)建表test CREATE TABLE postgres=# create table test_orc(i int) with (appendonly=true, orientation=orc); # 在postgres數(shù)據(jù)庫中創(chuàng)建ORC格式表 CREATE TABLE postgres=# create database dbnew template postgres; CREATE DATABASE postgres=#c dbnew # 連接dbnew 可以看到,dbnew中現(xiàn)在包含test表 dbnew=#d List of relations Schema | Name | Type | Owner | Storage --------+------+-------+----------+------------- public | test | table | ChangLei | append only (1 row) 3、模式 一個數(shù)據(jù)庫包含多個模式(schema),而一個模式可以包含多種命名對象,比如表,數(shù)據(jù)類型,函數(shù),操作符等。同一個對象名字可以用在不同的模式中而不產(chǎn)生沖突。比如schema1中可以包含表test,schema2中也可以同時包含名字為test的表。從這個意義上,模式很像一個命名空間(namespace)。 當創(chuàng)建一個對象時,默認被放置在public模式中。下面是系統(tǒng)默認創(chuàng)建的schema。 template1=# dn List of schemas Name | Owner --------------------+---------- hawq_toolkit | ChangLei information_schema | ChangLei pg_aoseg | ChangLei pg_bitmapindex | ChangLei pg_catalog | ChangLei pg_toast | ChangLei public | ChangLei (7 rows) 通常在這樣幾個場景下,用戶需要使用模式: 允許多個用戶同時使用一個數(shù)據(jù)庫,而不產(chǎn)生名字沖突。 把數(shù)據(jù)庫對象組織成多個schema,好像是多個命名空間一樣 第三方應(yīng)用可以把它們的對象放到一個單獨的schema中,而不和其他對象產(chǎn)生從圖。 注意:schema不可以嵌套,也就是說,schema中不可以再包含schema。 下面是創(chuàng)建schema的例子。 create schema myschema; 創(chuàng)建或者存取一個schema中的對象,可以使用{schema}.{object}形式,例如: create table myschema.test(i int); select * from myschema.test; 刪除一個空的schema,可以使用: drop schema myschame; 刪除不空的schema,可以使用cascade關(guān)鍵詞: drop schema myschema cascade; 使用{schema}.{object}形式,通常用起來不是很方便??梢酝ㄟ^設(shè)置schema搜索路徑來簡化?!盨HOW search_path”命令可以給出當前的schema搜索路徑?!盨ET search_path TO schema-name1, schema-name2”可以設(shè)置schema搜索路徑。例如: postgres=# show search_path; search_path ---------------- "$user",public (1 row) postgres=# create schema myschema; CREATE SCHEMA postgres=# set search_path = public, myschema; SET postgres=# show search_path; search_path ------------------ public, myschema (1 row) |
|
|
來自: 北漂二號 > 《數(shù)據(jù)庫》