例如,像這樣的事情,將會像前面的觸發器一樣將order_line
和product
表連接在一起來計算小計。據推測,你想要加入一些額外的屬性(即訂單號,訂單行號等)
CREATE OR REPLACE VIEW order_line_with_subtotal(product_no,
quantity,
price,
subtotal)
AS
SELECT ol.product_no,
ol.quantity,
p.price,
(ol.quantity * p.price) subtotal
FROM order_line ol
JOIN product p ON (ol.product_no = p.product_no)
這有許多相同的數據一致性問題,由於基於觸發器的解決方案必須當前價格是從product
表中引用的,而不是當前存儲在order_line
表中的當前價格。如果你改變了數據模型,使order_line
表的存儲量和當前價格,認爲會得到更簡單,因爲它不再需要如果你是11g上加入到product
表
CREATE OR REPLACE VIEW order_line_with_subtotal(product_no,
quantity,
price,
subtotal)
AS
SELECT ol.product_no,
ol.quantity,
ol.price,
(ol.quantity * ol.price) subtotal
FROM order_line ol
,你還可以在您的表格定義中創建一個虛擬列,
CREATE TABLE order_line (
order_line_no NUMBER PRIMARY KEY,
order_no NUMBER REFERENCES order(order_no),
price NUMBER,
quantity NUMBER,
subtotal NUMBER GENERATED ALWAYS AS (price*quantity) VIRTUAL
);