Skip to content

Instantly share code, notes, and snippets.

@forstie
Created September 24, 2019 01:37
Show Gist options
  • Save forstie/68307648a5feeff2d210897270d19acf to your computer and use it in GitHub Desktop.
Save forstie/68307648a5feeff2d210897270d19acf to your computer and use it in GitHub Desktop.
Ok movie buffs, here is a fictional before and after, with a happy ending. Use these techniques to bring database engineering to bear in your data center.
--
-- Existing file... not very user friendly
--
create schema gggr;
create table gggr.sales (
CL varchar(100),
PTY varchar(100),
SP varchar(30),
PG char(1)
);
insert into gggr.sales values('Penny Pincher',
'Woodhouse Lane - 1 Units',
'Dave',
'A');
insert into gggr.sales values('Nervous Nellie',
'Crestridge - 7 Units',
'Shelly',
'B');
insert into gggr.sales values('Investor Ike',
'Shoreview - 2 Units',
'Richard',
'C');
stop;
-- Table in its current form...
select * from gggr.sales;
stop;
-- Lets renovate using Database Engineering!
--
-- 1) Establish meaningful names for columns
-- 2) Assign a check constraint to prevent bad data
-- 3) Deploy business logic in a trigger to correct common mistakes
-- 4) Shift users and programs from the physical to a logical (view)
-- 5) Use SQL to improve the data externalized by the view
--
--
create or replace table gggr.sales_progress_pf for system name sales (
Client_Name for column CL varchar(100),
Property_Offered for column PTY varchar(100),
Sales_Person for column SP varchar(30),
Sales_Progress for column PG char(1),
constraint gggr.Sales_Progress_Check CHECK (Sales_Progress = 'A' OR
Sales_Progress = 'B' OR
Sales_Progress = 'C')
) on replace preserve rows
RCDFMT SALES;
create or replace trigger gggr.sales_progress_pg_trigger
before insert on gggr.sales_progress
referencing new as n for each row mode db2row
set option usrprf = *owner, dynusrprf = *owner
begin atomic
set n.Sales_Progress = upper(Sales_Progress);
end;
create or replace view gggr.sales_progress for system name salesp
as
select client_name, property_offered, sales_person,
case sales_progress
when 'A' then 'Always'
when 'B' then 'Be'
when 'C' then 'Closing'
else sp
end as sales_progress
from gggr.sales_progress_pf
RCDFMT SALES;
select * from gggr.sales_progress;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment