postgresql 建立分割槽表,sql優化之postgresql table partitioning
postgresql中使用動態sql-實現自動按時間建立表分割槽
declare
createon varchar(32);
createyy varchar(4);
tablename varchar(48);
i***ist int;
r record;
sqlstr text;
sqldel text;
sqlin text;
begin
//通過以下語句給createon變數賦值
select start_time into createon from tech_trial_t where start_time <>'' order by start_time asc limit 1;
createyy := substr(createon, 1,4);
select count(*) into i***ist from pg_class where relname = 'tech_trial_t_y' || createyy;
if (i***ist=0) then
sqlstr='create table if not exists tech_trial_t_y' || createyy || ' (like tech_trial_t_y2017 including indexes);';
execute sqlstr;
end if;
sqlstr := 'select relname from pg_class where relname like ''tech_trial_t_y20%'' and relname not like ''%_idx'';';
//必須排除索引,因為relname 是**,索引,檢視等的名稱
//for 語句迴圈取值賦值
for r in execute sqlstr loop
sqldel := 'truncate table ' || r.relname;
execute sqldel;
//直接寫 truncate table r.relname; 不能執行,貌似不能知道r.relname是什麼東東。
所以寫成字串語句執行字串語句。
sqlin := 'insert into ' || r.relname || ' select * from tech_trial_t_view_year tv where tv.start_time >= substr( ''' ||r.relname || ''',15,4) ||''-01-01 00:00:00'' and tv.start_time <= substr('''|| r.relname ||''',15,4) ||''-12-31 23:59:59'';';
execute sqlin;
end loop;
return null;
end
declare start_text text;
declare insert_statement text;
begin
start_text := substr(new.start_time, 1 ,4);
insert_statement := 'insert into tech_trial_t_'
|| start_text
||' values ($1.*)';
execute insert_statement using new;
return null;
exception
when undefined_table
then
execute
'create table if not exists tech_trial_t_'
|| start_text
|| '(check (start_time >= '''
|| start_text
|| '-01-01 00:00:00'' and start_time < ''' || start_text || '-12-31 23:59:59'')) inherits (tech_trial_t_year)';
raise notice 'create non-existant table tech_trial_t_year_%', start_text;
execute
'create index tech_trial_t_key_'
|| start_text
|| ' on tech_trial_t_'
|| start_text
|| '(start_time)';
execute insert_statement using new;
return null;
end;
//必須保證每條插入的start_time(分割槽關鍵字)非空。
new表示新插入的記錄
postgresql 觸發器寫法
create or replace function delete fdr returns trigger as delete fdr begin delete from object classes where object classes dataroom id old dataroom id ...
Postgresql 函式 觸發器寫法
1 資料庫環境 table 學生分數表 create table stu score stuno serial not null,學生編號 major character varying 16 專業課程 score integer 分數 with oids false alter table stu...
觸發器 mysql觸發器
觸發器是一種特殊的儲存過程,它在插入 刪除或修改特定表中的資料時觸發執行,它比資料庫本身標準的功能有更精細和更複雜的資料控制能力。和儲存過程一樣,很少使用。1 觸發器的作用 2 建立觸發器 建立測試環境 mysql create database test db query ok,1 row aff...