richardp

stack-overflow_virtual-column-discussion

Jun 18th, 2014
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Stack Overflow Test Plan for:
  2. -- http://stackoverflow.com/questions/24276153/oracle-pl-sql-with-automatic-values
  3. -- (prepared by Richard Pascual)
  4.  
  5. DROP TABLE "PROCESSED_PRODUCT_WEIGHT";
  6.  
  7. CREATE TABLE "PROCESSED_PRODUCT_WEIGHT" (
  8.     "PRODUCT_NAME"  VARCHAR2(40) NOT NULL,
  9.     "PROCESS1"      NUMBER,
  10.     "PROCESS2"      NUMBER,
  11.     "WEIGHT"        NUMBER,
  12.     "TOTAL_WEIGHT"  NUMBER GENERATED ALWAYS AS ((PROCESS1 + PROCESS2)*WEIGHT) VIRTUAL,
  13.     "RECORDED_DATE" DATE,
  14.     CONSTRAINT  "PROCESSED_PRODUCT_WEIGHT_PK" PRIMARY KEY ("PRODUCT_NAME", "RECORDED_DATE")
  15. )
  16. /
  17.  
  18. -- Test Case "Initialize"
  19. INSERT INTO PROCESSED_PRODUCT_WEIGHT ("PRODUCT_NAME", "PROCESS1", "PROCESS2", "WEIGHT", "RECORDED_DATE")
  20.        VALUES ('ITEM1', 0, 0, 10, '01/01/2014')
  21. INSERT INTO PROCESSED_PRODUCT_WEIGHT ("PRODUCT_NAME", "PROCESS1", "PROCESS2", "WEIGHT", "RECORDED_DATE")
  22.        VALUES ('ITEM2', 1, 1, 10, '01/01/2014')
  23. INSERT INTO PROCESSED_PRODUCT_WEIGHT ("PRODUCT_NAME", "PROCESS1", "PROCESS2", "WEIGHT", "RECORDED_DATE")
  24.        VALUES ('ITEM3', 1, 1, 15, '01/01/2014')
  25.  
  26. -- Test Case ONE
  27. INSERT INTO PROCESSED_PRODUCT_WEIGHT ("PRODUCT_NAME", "PROCESS1", "PROCESS2", "WEIGHT", "RECORDED_DATE")
  28.        VALUES ('ITEM1', 2, 5, 10, '02/14/2014')
  29. INSERT INTO PROCESSED_PRODUCT_WEIGHT ("PRODUCT_NAME", "PROCESS1", "PROCESS2", "WEIGHT", "RECORDED_DATE")
  30.        VALUES ('ITEM2', 3, 1, 10, '02/14/2014')
  31.  
  32. -- Test Case TWO
  33. INSERT INTO PROCESSED_PRODUCT_WEIGHT ("PRODUCT_NAME", "PROCESS1", "PROCESS2", "WEIGHT", "RECORDED_DATE")
  34.        VALUES ('ITEM1', 2, 5, 55, '03/14/2014')
  35.  
  36. -- Test Case THREE
  37. INSERT INTO PROCESSED_PRODUCT_WEIGHT ("PRODUCT_NAME", "PROCESS1", "PROCESS2", "WEIGHT", "RECORDED_DATE")
  38.        VALUES ('ITEM3', 2, 1, 20, '04/01/2014')
  39.  
  40. /
  41.  
  42.  
  43.  
  44. -- Below is the initial response to the Stack Overflow question referenced:
  45.  
  46. #Declaring VIRTUAL (Computed) COLUMNS IN Oracle TABLE Design
  47.  
  48. I'll agree with most of what has been said so far with some additional elaboration.  My starting table design looks similar but it too is also inaccurate for some use cases as explained below.
  49.  
  50. <!-- language:SQL -->
  51.  
  52.        CREATE TABLE "PROCESSED_PRODUCT_WEIGHT" (
  53.           "PRODUCT_NAME"  VARCHAR2(40) NOT NULL,
  54.           "PROCESS1"      NUMBER,
  55.           "PROCESS2"      NUMBER,
  56.           "WEIGHT"        NUMBER,
  57.           "TOTAL_WEIGHT"  NUMBER GENERATED ALWAYS AS
  58.                           ((PROCESS1 + PROCESS2)*WEIGHT) VIRTUAL,
  59.           "RECORDED_DATE" DATE,
  60.           CONSTRAINT  "PROCESSED_PRODUCT_WEIGHT_PK"
  61.              PRIMARY KEY ("PRODUCT_NAME", "RECORDED_DATE")
  62.           )
  63.           /  
  64.  
  65.  
  66. ##Previous Suggestions and Assumptions
  67.  
  68. ***Table Bound Attribute Properties***:  The table construct used by [@Bob Jarvis](http://stackoverflow.com/users/213136/bob-jarvis) is also known as a `VIRTUAL COLUMN`.  It works well because the definition of `TOTAL_WEIGHT` is entirely dependent on other values contained within the same table.
  69.  
  70. ***SQL Query Associated Calculation:*** On the other hand, [@Nishanthi Grashia](http://stackoverflow.com/users/3492139/nishanthi-grashia) and [@OldProgrammer](http://stackoverflow.com/users/1745544/oldprogrammer) both recommend modifying the value within each SQL query executed against the database.
  71.  
  72. *BOTH* Cases may work assuming that the mass per unit of the product does not change during the lifetime of the production cycle.  
  73.  
  74. > An example where this assumption is not flexible is if the products consist of units of varying volume.
  75. >
  76.  + Since it was not mentioned in the OP, consider this possibility:  
  77.  + Products ITEM1, ITEM2 and ITEM3 have variable weights per unit.  
  78.  + They are all produced in a coffee packaging plant.  
  79.  + Each item can be a type of coffee bean and its source.  
  80.  + "Processes" could be bean "treatments" such as decaffeination, roasting type or flavor infusion.  
  81.  
  82. The "units" could be packaging of varying sizes.  This would mean that package volumes would have a direct effect on the mass (called "weight") per product unit counted.
  83.  
  84.  
  85. ##Test Cases for Identifying the Effect of Changing Unit Sizes
  86.  
  87. Each test case shows how a virtual column does not satisfy the possibility of variations in the unit sizes and masses of each product over time.
  88.  
  89. ![Initial Table Population][1]  
  90.  
  91. **Test Case One:**
  92.  
  93. ![Test Case One:  Change Process Quantites for Two Different Item Types][2]
  94.  
  95. *For production observations made 2/14/2015*
  96.  
  97. **Test Case Two:**
  98.  
  99. ![Test Case Two:  No Process Quantities Counted/Incremented][3]
  100.  
  101. *The mass per unit processed on 3/14/2014 is increased only, skewing the total mass produced since the item quantities made previously are multiplied by a larger value through the virtual column definition.*
  102.  
  103. **Test Case Three:**
  104.  
  105. ![Test Case Three:  Process Quantity and Mass per Unit Changes][4]
  106.  
  107.  
  108. ##Data Output and Results
  109.  
  110. ![Result Snapshots for each production period][5]
  111.  
  112. *Above are the test results associated with all three test cases.*  the resulting values are not correct for the use cases created.  They demonstrate that for a changing weight value, the virtual/calculated column formula and approach gives incorrect results.
  113.  
  114.  
  115. ##A Discussion of Alternate Solutions
  116.  
  117. 1. The trigger approach may work for maintaining calculated values for `TOTAL_WEIGHT`.  Incremental changes (updates) are appended to the current, existing value as each component varies.
  118.  
  119. 2. Force all DML through a single DML operation contained in a CRUD package.  The problem with defining an embedded SQL statement to enforce requirements is that other processes and their developers will need to be familiar with what your isolated PHP form/page does within your app in order to duplicate it for their own operation.  
  120.  
  121. 3. If there is a concern about overhead or possible locking of the main table, then consider introducing a composite key: `PRODUCT_NAME` + `WEIGHT` where the system that UPDATES data is already aware of the unit weight of the new count of items it is adding.  
  122.  
  123.  
  124.  [1]: http://i.stack.imgur.com/3HeDr.png
  125.  [2]: http://i.stack.imgur.com/g2dOP.png
  126.  [3]: http://i.stack.imgur.com/4AIF2.png
  127.  [4]: http://i.stack.imgur.com/qh8Cv.png
  128.  [5]: http://i.stack.imgur.com/yJz8x.png
Advertisement