我現在有一個分配給uni的地方,我需要編寫一個數據庫變更腳本,然後是一個回滾腳本。我也應該做一些簡單的檢查,看是否已完成更改。我花了很多時間編寫腳本,因爲我對plsql不熟練。該prodcut是在這裏:編寫一個PL SQL變更腳本(sql developer/oracle)
-- the check could be more extensive, e.g. checking the type of the column
declare
titleExists number;
begin
select count(*) into titleExists
from user_tab_columns
where table_name = 'TITLE'
and column_name = 'TITLE';
if titleExists > 0 then
execute immediate 'alter table title rename column title to name';
end if;
end;
/
declare
typeExists number;
begin
select count(*) into typeExists
from user_tab_columns
where table_name = 'TITLE'
and column_name = 'TYPE'
and data_type = 'CHAR';
if typeExists > 0 then
execute immediate 'alter table title add (new_type varchar2(12) check (new_type in (''business'', ''mod_cook'', ''psychology'', ''popular_comp'', ''trad_cook'')))';
execute immediate 'update title set new_type = trim(type)';
execute immediate 'alter table title modify (new_type not null)';
execute immediate 'alter table title drop column type';
execute immediate 'alter table title rename column new_type to type';
end if;
end;
/
第一部分重命名的列和所述第二部分改變列型,並增加了支票,基本上變成char列到枚舉。
我真的很想知道,我需要把每一個變化都立即執行。有沒有更簡單的方式來寫這個?
對於頂級標記不要忘記添加異常處理;) –
哈哈,螺絲異常處理。我只會在那裏添加註釋,例如「應該有異常處理」。此外這項任務沒有被標記,它是要麼完成或不是:) – Santhos