- COMP9311 Database Systems 传送门
- COMP9311 Database Systems WEEK8
- COMP9311 Database Systems WEEK4
- COMP9311 Database Systems WEEK2
- COMP9311 Database Systems WEEK3
- COMP9311 Database Systems WEEK1
- COMP9311 Database Systems Lab1
- COMP9311 Database Systems WEEK6
- COMP9311 Database Systems WEEK12
- COMP9311 Database Systems Lab6
1. Database Programming
在Procedural Language中操作数据库时,往往包含三个步骤:
(1)code in a programming language(PHP比较常用)
(2)SQL query/update statments(用来获得数据库的信息)
(3)code to map between tuples and PL data(连接PL和database)
从网页调取数据的时候,除了sql, PHP外,往往还需要用到html, css和javascript。
2. Catalogs
Catalogs是关于databse的数据,比如有多少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 练习1
访问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 练习2
访问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 练习3
写一个plpgsql函数实现以下功能:
--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
创建用户:
CREATE USER Name IDENTIFIED BY 'Password'
ALTER USER Name IDENTIFIED BY 'NewPassword'
ALTER USER Name WITH Capabilities
ALTER USER Name SET ConfigParameter = ...
创建用户分组:
CREATE GROUP Name
ALTER GROUP Name ADD USER User1, User2, ...
ALTER GROUP Name DROP USER User1, User2, ...
设置用户权限:
CREATE ROLE UserName Options
-- where Options include ...
PASSWORD 'Password'
CREATEDB | NOCREATEDB
CREATEUSER | NOCREATEUSER
IN GROUP GroupName
VALID UNTIL 'TimeStamp'
网友评论