[英]How to make a trigger in ORACLE PL/SQL to not allow double values?
[英]Oracle PL SQL Trigger - To make data consistent
ORACLE / PL SQL觸發器我有一個處理住宅屬性的表,稱為unit_tbl。
主要的組合鍵是(unit_num,complex_num和owner_num),因此許多所有者可以在同一組合物中擁有相同的單元。
其他列包括num_of_bedrooms(即4、3、2、1)和property_type(即房屋,復式公寓,公寓,公寓)。
假設輸入了以下語句:
INSERT INTO unit_tbl
(unit_id, complex_id, owner_id, num_beds, property_type)
VALUES
(001, 1000, 010, 3, 'apartment');
我想提出一個錯誤,以便為另一個所有者(具有相同屬性)輸入相同的unit_id和complex_id,如果num_beds與先前的條目不匹配,或者屬性類型與先前的條目不匹配。
例如,如果有人要插入或更新以下內容,則會引發錯誤:
INSERT INTO unit_tbl
(unit_id, complex_id, owner_id, num_beds, property_type)
VALUES
(001, 1000, 011, 2, 'apartment'); -- num_beds here does not match the same property previously entered.
我嘗試創建觸發器:
CREATE OR REPLACE TRIGGER unit_consist_check
BEFORE INSERT OR UPDATE ON unit_tbl
FOR EACH ROW
DECLARE
BEGIN
IF :NEW.unit_id = :OLD.unit_id AND :NEW.complex_id=:OLD.complex_id AND ( :NEW.num_beds <> :OLD.num_beds OR :NEW.property_type <> :OLD.property_type) THEN
raise_application_error (-20002, 'nconsistent data on bedroom size or property type. Please make sure this data is identical to previously entered data for this specific unit_id and complex_id');
END IF;
END;
/
我還嘗試了DECLARING變量並執行SELECT INTO變量,但這似乎在獲取太多行時產生了錯誤。
我是PL / SQL的新手,所以非常感謝您的幫助和耐心。
您正在使用一種不良習慣-觸發器-來補償另一種不良習慣-非規范化。
添加一個新表以單獨存儲單元,並使用unit_owner表將其連接到所有者表,您將不再需要觸發器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.