oracle - Database design: status of one table based on another -
i have odd business requirement has me stumped. background: basically, have 2 tables tracking program enhancements: enhancement , bug. relationship enhancement->bug 1:m, , bug table has foreign key column enhancementid.
both tables have "status" column, gets tricky. requirement status of enhancement dependent on related bugs. example, if have 3 bugs enhancementid's of 100 , statuses of "in testing", enhancement 100's status should automatically set "in testing." there several status rules this.
this database shared few applications, first thought use "on update" trigger on bug table. because trigger had select statement in triggering table, received "mutating table" error (i have query statuses of bugs specified enhancementid when trigger fires). now, trying implement 3 trigger solution outlined here: http://asktom.oracle.com/pls/asktom/asktom.download_file?p_file=6551198119097816936 becoming wary of putting logic within database triggers.
so question is: approaching problem reasonably? there better way suggest? perhaps using view enhancement's status?
use view.
there no simple way synchronize data accross rows/tables. you've found triggers lead mutating errors , source of uncountable bugs. if want avoid mutating errors, @ workaround tom kyte (this understand why triggers not best tool in case).
you use application procedures or pl/sql api note work if use them time (which means never ever issue single direct update these tables). single developer forgets use api desynchronize data. consider api if status complex compute on fly view not acceptable performance-wise.
since redundant information (the status of enhancement can deducted entirely status of bugs), don't need store in database column.
if can express rule in sql query, straightforward, instance:
create or replace view enhancement_with_status_v select e.*, case when count(decode(b.status, 't', 1)) >= 1 't' when ... else ... end status enhancement e, left join bugs b on b.enhancement_id = e.enhancement_id group e...
if rules complex, can write pl/sql function , call function sql.
the view has advantage when rules change (as rules do), not need update whole table.
Comments
Post a Comment