COMP9311 Database Systems WEEK8

1. Database Programming

在Procedural Language中操作數(shù)據(jù)庫(kù)時(shí)返吻,往往包含三個(gè)步驟:
(1)code in a programming language(PHP比較常用)
(2)SQL query/update statments(用來(lái)獲得數(shù)據(jù)庫(kù)的信息)
(3)code to map between tuples and PL data(連接PL和database)
從網(wǎng)頁(yè)調(diào)取數(shù)據(jù)的時(shí)候,除了sql, PHP外风喇,往往還需要用到html, css和javascript柱衔。

2. Catalogs

Catalogs是關(guān)于databse的數(shù)據(jù)樊破,比如有多少table,多少trigger唆铐,多少fuction等哲戚。system catalog也被叫做data dictionary或者system view。
SQL:2003 standard metadata: INFORMATION_SCHEMA.
INFORMATION_SCHEMA is available globally and includes:
--Schemata(catalog_name, schema_name, schema_owner, ...)
--Tables(table_catalog, table_schema, table_name, table_type, ...)
--Columns(table_catalog, table_schema, table_name, column_name, ordinal_position, column_default, data_type, ...)
--Views(table_catalog, table_schema, table_name, view_definition, ...) Table_Constraints(..., constraint_name, ..., constraint_type, ...)

2.1 練習(xí)1

訪問(wèn)Catalog獲取databse的table信息:

create or replace view myTables
as
select table_name
from   information_schema.tables
where  table_schema = 'public' and table_type = 'BASE TABLE'
order  by table_name;

2.2 練習(xí)2

訪問(wèn)Catalog獲取table和attributes信息:

create or replace view myTableColumns
as
select t.table_name, c.column_name
from   information_schema.tables t
    join information_schema.columns c
    on (t.table_name = c.table_name)
where  t.table_schema = 'public' and t.table_type = 'BASE TABLE'
order  by t.table_name, c.ordinal_position;

create or replace view mySchema("table","attributes")
as
select table_name, concat(column_name)
from   myTableColumns
group  by table_name
order  by table_name;

2.3 練習(xí)3

寫一個(gè)plpgsql函數(shù)實(shí)現(xiàn)以下功能:
--whose argument is a table name (from the public schema)
--whose result is a CREATE TABLE statement to build the table
--only handle constraints mentioned in the columns table

create type ColumnRecord as (
    "table" text,
    "column" text,
    "type" text,
    "constraint" text
);

create or replace function
    colDataType(_col information_schema.columns) returns text
as $$
declare
    _out text;
begin
    _out := _col.data_type;
    _out := _out||coalesce(' '||_col.character_maximum_length::text,'');
    -- if (_col.character_maximum_length is not null) then
    --    _out = _out||(_col.character_maximum_length::text);
    -- end if;
    _out := replace(_out,'character varying','varchar');
    if (_out like 'timestamp%') then _out := 'timestamp'; end if;
    return _out;
end;
$$ language plpgsql;

create or replace function
    colConstraints(_col information_schema.columns) returns text
as $$
declare
    _rec record;
    _out text;
begin
    _out := '';
    if (_col.is_nullable = 'NO') then
        _out := _out||','||'NOT NULL';
    end if;
    for _rec in
        select tc.constraint_type
        from   information_schema.constraint_column_usage ccu
            join information_schema.table_constraints tc
                on (ccu.constraint_name = tc.constraint_name)
        where  tc.table_name = _col.table_name
            and ccu.column_name = _col.column_name
    loop
        _out := _out||','||_rec.constraint_type;
    end loop;
    return substring(_out,2,length(_out));
end;
$$ language plpgsql;

create or replace function
    schema() returns setof ColumnRecord
as $$
declare
    _col information_schema.columns;
    _c ColumnRecord;
begin
    for _col in
        select c.*
        from information_schema.tables t
            join information_schema.columns c
            on (t.table_name = c.table_name)
        where t.table_schema='public' and t.table_type = 'BASE TABLE'
    loop
        _c."table"      := _col.table_name;
        _c."column"     := _col.column_name;
        _c."type"       := colDataType(_col);
        _c."constraint" := colConstraints(_col);
        return next _c;
    end loop;
    return;
end;
$$ language plpgsql;

3. Security, Privilege, Authorisation

3.1 Access Control

創(chuàng)建用戶:

CREATE USER Name IDENTIFIED BY 'Password'
ALTER USER Name IDENTIFIED BY 'NewPassword'
ALTER USER Name WITH Capabilities
ALTER USER Name SET ConfigParameter = ...

創(chuàng)建用戶分組:

CREATE GROUP Name
ALTER GROUP Name ADD USER User1, User2, ...
ALTER GROUP Name DROP USER User1, User2, ...

設(shè)置用戶權(quán)限:

CREATE ROLE UserName Options
-- where Options include ...
PASSWORD 'Password'
CREATEDB | NOCREATEDB
CREATEUSER | NOCREATEUSER
IN GROUP GroupName
VALID UNTIL 'TimeStamp'
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子榆纽,更是在濱河造成了極大的恐慌敬特,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件窑多,死亡現(xiàn)場(chǎng)離奇詭異碎赢,居然都是意外死亡最易,警方通過(guò)查閱死者的電腦和手機(jī)腕窥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門粒没,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人簇爆,你說(shuō)我怎么就攤上這事∷觯” “怎么了入蛆?”我有些...
    開封第一講書人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)硕勿。 經(jīng)常有香客問(wèn)我哨毁,道長(zhǎng),這世上最難降的妖魔是什么源武? 我笑而不...
    開封第一講書人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任扼褪,我火速辦了婚禮,結(jié)果婚禮上粱栖,老公的妹妹穿的比我還像新娘话浇。我一直安慰自己,他們只是感情好闹究,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開白布幔崖。 她就那樣靜靜地躺著,像睡著了一般渣淤。 火紅的嫁衣襯著肌膚如雪赏寇。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,482評(píng)論 1 302
  • 那天价认,我揣著相機(jī)與錄音嗅定,去河邊找鬼。 笑死用踩,一個(gè)胖子當(dāng)著我的面吹牛渠退,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播捶箱,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼智什,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了丁屎?” 一聲冷哼從身側(cè)響起荠锭,我...
    開封第一講書人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎晨川,沒想到半個(gè)月后证九,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體删豺,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年愧怜,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了呀页。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡拥坛,死狀恐怖蓬蝶,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情猜惋,我是刑警寧澤丸氛,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站著摔,受9級(jí)特大地震影響缓窜,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜谍咆,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一禾锤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧摹察,春花似錦恩掷、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至查坪,卻和暖如春寸宏,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背偿曙。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工氮凝, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人望忆。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓罩阵,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親启摄。 傳聞我的和親對(duì)象是個(gè)殘疾皇子稿壁,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容