Vip Call Girls Noida âĄď¸ Delhi âĄď¸ 9999965857 No Advance 24HRS Live
Â
Oaktable World 2014 Toon Koppelaars: database constraints polite excuse
1. Why SQL DBMSâs Still Lack Full Declarative Constraint Support (A polite excuse)
Toon Koppelaars
2. Who am I?
â˘A database guy, relational dude
âDeveloper (with DBA experience)
â˘Oracle technology user since 1987
â˘Co-author of this book ď
â˘Todayâs talk = last chapter
3. Agenda
â˘SQL Assertions?
âA few examples
â˘Validation Execution Models
âMore efficient along the way
â˘Serializability
â Probably the biggest problem
â˘Conclusions
4. SQL Assertions?
â˘Examples
1.There is at most one president
2.One cannot manage more than two departments
3.Department with president and/or manager should have an administrator
ď¨ Data integrity constraints
âJust like: CHECK, PK, UK, FK
ââall the other onesâ
They constrain data allowed in our tables
They constrain data allowed in our tables
5. Syntax?
â˘Create assertion [some_name] as check([some_SQL_expression]);
â˘For over 25 years part of Ansi/Iso SQL standard
6. Example 1
â˘create assertion at_most_one_president as check (1 => (select count(*) from EMP e where e.JOB = âPRESIDENTâ ) );
â˘Task of DBMS: make sure itâs true at all times
- Closed SQL expression (no free variables)
- Evaluates to true or false
7. Example 2
â˘create assertion cannot_manage_more_than_2 as check (not exists (select âxâ from (select d.MGR ,count(*) as cnt from DEPT d group by d.MGR) where cnt > 2 ) );
8. Example 3
â˘create assertion admin_in_dept_with_vip as check (not exists (select âxâ from (select distinct e.DEPTNO from EMP e) e where exists (select âyâ from EMP e1 where e1.DEPTNO = e.DEPTNO and e1.JOB in (âPâ,âMâ)) and not exists (select âzâ from EMP e2 where e2.DEPTNO = e.DEPTNO and e2.JOB = âADMINâ) ) );
10. Imagine...
â˘If DBMS vendor would support these
âHow much less lines of code one would need to write in application development
âHow much less bugs this results into
âHow easy we could accomodate change requested by the business
âHow data quality would improve
11. Point to be made
â˘SQL assertions âcoverâ all the other declarative constraints available
âCHECK ď can be written as assertion
âPK/UK ď can be written as assertion
âFK ď can be written as assertion
â˘We just have shorthands for these, since these are so common in every database design
12. Check writen as assertion
â˘create assertion hire_only_18_plus as check (not exists (select âxâ from EMP e where e.HIRED - e.BORN < 18*365 ) ); CHECK((HIRED â BORN) >= 18*365)
13. PK/UK written as assertion
â˘create assertion empno_is_unique as check (not exists (select âxâ from EMP e1 ,EMP e2 where e1.EMPNO = e2.EMPNO and e1.rowid != e2.rowid ) ); PRIMARY KEY (EMPNO)
14. FK written as assertion
â˘create assertion work_in_known_dept as check (not exists (select âxâ from EMP e where not exists (select âyâ from DEPT d where d.deptno = e.deptno ) ) ); FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO)
15. Scientific Problem
â˘Why is CREATE ASSERTION not supported?
â˘Think about this:
âThe DBMS has our assertion expression but:
â˘Would you accept full evaluation of given expression for every insert/update/delete?
âNo.
â˘Performance would be horrible in real world dbâs
â˘You require a much more sophisticated execution model in the real world
16. Scientific Problem
â˘The challenge is:
âGiven: arbitrary complex assertion expression +
âGiven: arbitrary complex SQL statement ď¨
âWhat is the minimum required check that needs to be executed by the DBMS for the assertion to remain true?
â˘Can be ânothingâ if assertion is immune for the given DML
17. For instance...
â˘Department that employs president or manager should also employ administrator
âObviously only when DML operates on EMP
1.Insert into emp values(100,âSmithâ,âSALESREPâ ,â1/1/80â,â20/11/07â,7000,20);
2.Insert into emp values(:b0,:b1,:b2 ,:b3,:b4,:b5,:b6);
3.Insert into emp (select * from emp_loaded);
4.Update emp set msal=1.05*msal;
5.Delete from emp where empno=101;
19. Execution Model 1
â˘Evaluate every assertion on every DML statement
âEvaluating every <boolean expression with subqueries>
âOn every DML statement
â˘In after-statement table trigger
âWOULD BE *VERY* INEFFICIENT
â˘Letâs quickly forget this âEM1â
20. Execution Model 2
â˘Only evaluate the assertions that involve the table that is being âDML-ledâ
âFinding involved tables by parsing the assertion expression
âPer assertion 100% code generation of a (insert/update/delete) âafter statementâ table trigger
â˘Select from dual where [expression]
â˘Found? ď OK
â˘Not found ď raise_application_error
21. Execution Model 2
â˘Using examples 2 and 3:
âAdmin_in_dept_with_vip ď¨ EMP table
âCannot_manage_more_than_2 ď¨ DEPT table
22. Execution Model 2
create trigger EMP_AIUDS_EMP01
after insert or update or delete on EMP
declare pl_dummy varchar(40);
begin
--
select 'Constraint EMP01 is satisfied' into pl_dummy
from DUAL
where not exists(select âa violationâ
from (select distinct deptno from EMP) d
where exists(select e2.*
from EMP e2
where e2.DEPTNO = d.DEPTNO
and e2.JOB in ('PRESIDENT','MANAGER'))
and not exists(select e3.*
from EMP e3
where e3.DEPTNO = d.DEPTNO
and e3.JOB = 'ADMIN'));
--
exception when no_data_found then
raise_application_error(-20999,'Constraint EMP01 is violated.');
end;
Assertion
predicate
23. Execution Model 2
create trigger DEPT_AIUDS_DEPT01
after insert or update or delete on DEPT
declare pl_dummy varchar(1);
begin
--
select âa' into pl_dummy
from DUAL
where not exists (select âa violationâ from (select d.MGR ,count(*) as cnt from DEPT d group by d.MGR) where cnt > 2));
--
exception when no_data_found then
raise_application_error(-20999,'Constraint DEPT01 is violated.');
end;
EM2 could be supported declaratively
24. Execution Model 2
â˘Inefficiencies:
âEMP01 and DEPT01 are checked when updating columns that are not involved, for instance:
â˘Updating EMP.ENAME
â˘Updating DEPT.LOC
25. Execution Model 3
â˘For inserts + deletes: EM3 == EM2
â˘For updates: only evaluate assertions that involve *columns* being changed
âSimple parse will find columns
âAssumes âclean specificationâ
â˘Create trigger syntax allows specification of columns that are being changed
26. Execution Model 3
create trigger EMP_AUS_EMP01
after update of DEPTNO,JOB on EMP
declare pl_dummy varchar(40);
Begin
--
select 'Constraint EMP01 is satisfied' into pl_dummy
from DUAL
where not exists(select âdepartment in violationâ
from (select distinct deptno from EMP) d
where exists(select e2.*
from EMP e2
where e2.DEPTNO = d.DEPTNO
and e2.JOB in ('PRESIDENT','MANAGER'))
and not exists(select e3.*
from EMP e3
where e3.DEPTNO = d.DEPTNO
and e3.JOB = 'ADMIN'));
--
exception when no_data_found then
raise_application_error(-20999,'Constraint EMP01 is violated.');
end;
27. Execution Model 3
create trigger DEPT_AIUDS_DEPT01
after update of MGR on DEPT
declare pl_dummy varchar(40);
begin
--
select âa' into pl_dummy
from DUAL
where not exists (select âxâ from (select d.MGR ,count(*) as cnt from DEPT d group by d.MGR) where cnt > 2));
--
exception when no_data_found then
raise_application_error(-20999,'Constraint DEPT01 is violated.');
end;
EM3 could be supported declaratively
28. Execution Model 3
â˘Inefficiencies:
âSometimes inserts (e)or deletes can never violate a constraint
âFor Cannot_manage_more_than_2, deleting a department does not require re-validation
âFor Admin_in_dept_with_vip, both inserts and deletes do require re- validation
29. Execution Model 4
â˘For updates: EM4 = EM3
â˘For deletes and inserts EM4 drops unnecessary delete (e)or insert table triggers
âPolarity of a table for a given constraint
â˘Positive: inserts can violate
â˘Negative: deletes can violate
â˘Neutral: both can violate
â˘Undefined: table is not involved
Polarity of table for given constraint can be
computed via special parsing
30. Execution Model 4
â˘EM4 maintains:
âAll EM3 triggers, except for one:
â˘Drops:
âCannot_manage_more_than_2 delete trigger
EM4 could be supported declaratively
31. Execution Model 4
â˘Inefficiencies:
âAdmin_in_dept_with_vip: eg. inserting SALESMAN, or deleting TRAINER does not require re-validation
â˘Start involving literals mentioned in assertions
â˘If assertion does not have literals then next EM is same as EM4
âCannot_manage_more_than_2 has no literals
32. Execution Model 5
â˘How do we see:
âSalesman is inserted? Trainer deleted?
â˘Could parse the DML statement
âBut does not always work due to absence of literals
â˘Make use of column values of affected rows
âRequires âTransition Effectâ (TE) of a DML statement
â˘Common concept (aka. âdeltaâ-tables)
â˘Inserted_rows, Updated_rows, Deleted_rows
âMaintaining TE is straightforward (see book)
â˘EM5: Only check the assertion when a property holds in the TE
33. Execution Model 5
â˘TE property for Admin_in_dept_with_vip:
1.Inserted_rows holds a president or a manager or,
2.Deleted_rows holds an admininstrator
3.Updated rows shows that ...
34. Execution Model 5
create trigger EMP_AIS_EMP01
after insert on EMP
declare pl_dummy varchar(40);
begin
-- If this returns no rows, then EMP01 cannot be violated.
select 'EMP01 must be validated' into pl_dummy
from DUAL
where exists
(select 'A president or manager has just been inserted'
from inserted_rows
where JOB in ('PRESIDENT','MANAGER'));
--
begin
--
<same trigger code as EM4>
--
end;
exception when no_data_found then
-- No need to validate EMP01.
null;
--
end;
35. Execution Model 5
create trigger EMP_ADS_EMP01
after delete on EMP
declare pl_dummy varchar(40);
begin
-- If this returns no rows, then EMP01 cannot be violated.
select 'EMP01 must be validated' into pl_dummy
from DUAL
where exists
(select 'An administrator has just been deleted'
from deleted_rows
where JOB = 'ADMIN');
--
begin
--
<same trigger code as EM4>
--
end;
exception when no_data_found then
-- No need to validate EMP01.
null;
--
end;
36. Execution Model 5
â˘Update TE-property for EMP01:
select 'EMP01 is in need of validation'
from DUAL
where exists
(select 'Some department just won a president/
manager or just lost an administrator'
from updated_rows
where (n_job in ('PRESIDENT','MANAGER') and
o_job not in ('PRESIDENT','MANAGER')
or (o_job='ADMIN' and n_job<>'ADMIN')
or (o_deptno<>n_deptno and
(o_job='ADMIN' or n_job in
('PRESIDENT','MANAGER')))
â˘Can be deduced from insert + delete properties
â˘EM5 fully declarative too?
âHere it gets complex...
37. Execution Model 5
â˘Inefficiencies:
âAdmin_in_dept_with_vip: triggers validate all departments
â˘Unacceptable in real-world databases
â˘Only some require re-validation
âCannot_manage_more_than_2: triggers validate all department managers
â˘Unacceptable in real-world databases
â˘Only some require re-validation
38. Execution Model 6
â˘On TE-property + optimized validation query
âUse the TE-query to find:
â˘Which deptno-values require re-validation
â˘Which mgr-values require re-validation
âThen use these values in the assertion- expression
39. create trigger EMP_AIS_EMP01
after insert on EMP
declare pl_dummy varchar(40);
begin
--
for r in (select distinct deptno
from inserted_rows
where JOB in ('PRESIDENT','MANAGER'));
loop
begin
-- Note: this now uses r.deptno value from preceeding TE-query.
select 'Constraint EMP01 is satisfied' into pl_dummy
from DUAL
where not exists(select âdepartment in violationâ
from (select distinct deptno from EMP where deptno = r.deptno) d
where exists(select e2.*
from EMP e2
where e2.DEPTNO = d.DEPTNO
and e2.JOB in ('PRESIDENT','MANAGER'))
and not exists(select e3.*
from EMP e3
where e3.DEPTNO = d.DEPTNO
and e3.JOB = 'ADMIN'));
--
exception when no_data_found then
--
raise_application_error(-20999,
'Constraint EMP01 is violated for department '||to_char(r.deptno)||'.');
--
end;
end loop;
end;
40. Execution Model 6
â˘This requires:
âDetecting that the ASSERTION can be (re)written as a universal quantification
âCan sometimes be done in multiple ways
â˘Which to choose?
âEM6 fully declarative?
â˘Complexity introduced in EM5 further increases
â˘Remember: given any arbitrary complex assertion + dml-statement
41. Still not there yet...
â˘Then there is something else too...
âWhich is often overseen by database professionals
âAnd, which is neglected in every research paper (Iâve read...) that deals with generating constraint validation code
ď¨ Serializability
42. Concurrent Transactions
â˘Deptno 13 has two admins and one manager
âTX1 deletes an admin from 13
â˘Does not yet commit
âTX2 deletes the other admin 13
â˘Commits
âTX1 commits
â˘Constraint is violated for deptno 13!
âTX1 and TX2 must be serialized
43. Concurrent Transactions
â˘Note: this is *not* about locking rows of data, but rather: locking a constraint ď¨No two TXâs can validate at same time
â˘We can use DBMS_LOCK to serialize these transactions
âSee book for example code
â˘Again complexity further increases
44. Concurrent Transactions
â˘Concurrency impact of acquiring rule locks
âEM1: One TX at a time
âEM2: One TX per table at a time
âEM3, EM4, EM5 slowly relaxes
â˘Up to EM5: not acceptable
âEM6: Only if two TXâs actually validate *and* involve same deptno (EMP01 assertion)
â˘Acceptable
45. Another complicating factor
â˘Deferrabilty...
âInvolves temporarily storing violation cases for re-evaluation at commit time
âMore comments on that in chapter 11 of the book
46. The Polite Excuse
â˘Inefficient EMâs
âCould be supported, but:
â˘Are unacceptable wgt. Performance & transaction concurrency
â˘Efficient EMâs
âAligned with business requirements
â˘Performance and TX concurrency
But,
âNeed more research to determine if they could be declaratively supported