1 / 18

Use the USER_TRIGGERS data dictionary view to display information about the RESTRICT_SAL trigger.

Example: create a trigger on employee table called EMP to add rows on the table called AUDIT_EMP_TABLE to trace any update operation on the table EMP. The trigger should store the old and new values of the different columns.

Download Presentation

Use the USER_TRIGGERS data dictionary view to display information about the RESTRICT_SAL trigger.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Example: create a trigger on employee table called EMP to add rows on the table calledAUDIT_EMP_TABLE to trace any update operation on the table EMP. The trigger should store the old and new values of the different columns.

  2. Use the USER_TRIGGERS data dictionary view to display information about the RESTRICT_SAL trigger. SQL> SELECT trigger_name,trigger_type, 2 triggering_event, table_name, referencing_names, when_clause, status, trigger_body 4 FROM user_triggers 5 WHERE trigger_name = 'RESTRICT_SAL';

  3. You can create a BEFORE statement trigger in order to prevent the triggering operation from succeeding if a certain condition is violated.Create a trigger to restrict inserts into the EMP table to certain business hours, Monday through Friday.If a user attempts to insert a row into the EMP table on Saturday, for example, the user sees the message, the trigger fails, and the triggering statement is rolled back. RAISE_APPLICATION_ERROR is a server-side built-in procedure that returns an error to the user and causes the PL/SQL block to fail. SQL> CREATE OR REPLACE TRIGGER secure_emp 2 BEFORE INSERT ON emp 3 BEGIN 4 IF (TO_CHAR (sysdate,'DY') IN ('SAT','SUN')) OR 5 (TO_CHAR(sysdate,'HH24') NOT BETWEEN 6 '08' AND '18') 7 THEN RAISE_APPLICATION_ERROR (-20500, 8 'You may only insert into EMP during business hours.'); 9 END IF; 10 END;

  4. Creating Statement Triggers Using Procedure Builder

  5. Using Conditional Predicates CREATE OR REPLACE TRIGGER secure_emp BEFORE INSERT OR UPDATE OR DELETE ON emp BEGIN IF (TO_CHAR (sysdate,'DY') IN ('SAT','SUN')) OR (TO_CHAR (sysdate, 'HH24') NOT BETWEEN '08' AND '18') THEN IF DELETING THEN RAISE_APPLICATION_ERROR (-20502, 'You may only delete from EMP during normal hours.'); ELSIF INSERTING THEN RAISE_APPLICATION_ERROR (-20500, 'You may only insert into EMP during normal hours.'); ELSIF UPDATING ('SAL') THEN RAISE_APPLICATION_ERROR (-20503, 'You may only update SAL during normal hours.'); ELSE RAISE_APPLICATION_ERROR (-20504, 'You may only update EMP during normal hours.'); END IF; END IF; END;

  6. ExamplesA triger to return back to the first record if the pointer reach the last record. • The trigger on the level of : BLOCK type : KEY-DOWN IF :SYSTEM.LAST-RECORD =’TRUE’ THEN FIRST-RECORD;ELSE NEXT_RECORD;END IF;

  7. Trigger To show the time on a form • trigger on the level of form type : when _new form_instance DECLARE T TIMER ;BEGIN T:=CREATE _TIMER ('TI',1000,REPEAT);END;there shoulb be another TRIGGER to finish the first :level of TRIGGER : form type : WHEN_TIMER_EXPIRED :CLOCK :=TO_CHAR(SYSDATE,'HH:MI:SS');where the name : CLOCK is DISPLAY ITEM

  8. the trigger will maximize the window on run time level of trigger is form • type =when_new_form instance beginset_window_property(forms_mdi_window,title,'مرحبا بك في قسم الحاسوب ');set_window_property(forms_mdi_window,window_state,maximize);set_window_property('window1',window_state,maximize);end;

  9. A Trigger To Run A Report From A Form • type : when_button_pressed level : on button declarepl_id number;beginrun_product(reports,'emp.rep',synchronous,runtime,filesystem,pl_id,null);end;whereemp =report name

  10. وظيفة التريغر : وضع وصف لفورين كي foreign key بعد الاستعلاماسم التريغير : post-queryالمستوى : blockلنفترض عندنا داتا بلوك اسمه customers يحتوي على اسم الزبون و معلومات خاصة عنه ورقم المدينة التي يقطن بها (city_number) foreign keyو يوجد هناك جدول (master table) خاص بأرقام المدن و أسمائها ، cities_table (city_no, city_name) المطلوب : نريد عرض اسم المدينة مع الرقم في بلوك الزبائن عند كل استعلامأولا : ننشئ display item داخل بلوك الزباين و نسميه c_name و ندخل على خصائصه و نعمل database item = Noثم نكتب التريغر post-query DECLARE CURSOR c1 IS SELECT city_name FROM cities_table WHERE city_no = :customers.city_number BEGIN OPEN c1; FETCH c1 INTO :customers.c_name; IF c1%NOTFOUND THEN :customers.c_name := 'not available' ; END IF; CLOSE c1; END; ملاحظات1) جدول الزباين يكون به فقط رقم المدينة و لا يكون به اسم المدينة2) عند اضافة زبون لا يتم عرض اسم المدينة من خلال هذا التريغر ، و لكن يجب استخدام طريقة أخرى مثل list of value أو when-validate-item trigger3) نستطيع استخدام نفس الكود في تريغر when_validate_item على مستوى : :customers.city_number و ذلك لعرض اسم المدينة عند اضافة زبون أو عند تغيير رقم المدينة لزبون موجود أصلا4) نستطيع الاستغناء عن post-query & when-validate-item باستخدام تريغر واحد فقط يقوم بعمل التريغرين السابقين في نفس الوقت و هو :post-change ، و لكن جماعة أوراكل لا ينصحون باستخدامه حيث أنه موجود لغاية الان لغرض التوافق مع برامج أوراكل القديمة، حيث يوجد عليه قيود.

  11. سوف اكتب تريغر عن شاشة فواتير وبها جزء رئيسي وتفصيلي وهنا عند بيع كمية من صنف معين في الفاتورة سوف يطرح تلقائيا من جدول الاصنافالجدول التفصيلي للفواتير =inv_dالجدول للاصناف =items على سبيل المثال • وظيفة التريغر =طرح الكمية المباعة من جدول الاصناف تلقائيانوعه = pre_insertمستواه= data block • declare a number ;begin select items.qty into a from item where items.item_no=:inv_d.item_no;if :inv_d.qty>a then message('الكمية في المخزن لاتكفي ');raise form_trigger_failure;else update items set qty=qty-:inv_d.qty where item_no=:inv_d.item_no ;end if;end

  12. أقدم لكم في هذا الرد درس PASING PARAMETER BETWEEN FORMSنريد أن نعمل شاشة للجدول DEPT بحيث أنه أذا عرضنا البيانات اللتي بالدائرة رقم 20 ونقرنا على زر ينتقل بنا إلى شاشةEMPوبها أسماء لموظفين اللذين يعملون في هذه الدائرةننشئ شاشة للــDEPT ومن ثم نضع بها زر ومن ثم نظغط على F3ونذهب ألى PARAMETERSونعمل له أنشاءثم نغير أسم الباراميتر ألى NO ثم نغير PARAMETER DATA TYPE ألىNUMBER لأن الباراميتر هنا هو الحقل DEPTNO وكما هو معروف فأن هذا الحقل نوعه رقم .نغلق الشاشة نحفظها بأسم DEPT ثم ننشئ شاشة EMP ونعملها TABULAR بعد أن عملناها نحفظها بأسم EMP .. نعود ألى الشاشة لأولى ثم نذهب ألى الزر ونختار الحدث WHEN_BUTTON_PREESED ونكتب الكود التاليDECLAREA PARAMLIST;BEGINA:=CREATE_PARAMETER_LIST('THA');ADD_PARAMETER(A,'NO',TEXT_ITEM,PARAMETER,:DEPTNO);RUN_PRODUCT(FORMS,'EMP',ASYNCHRONOUS,RUNTIME,FILESYSTEM,A,'');EXIT_FORM;END;طبعا هذا الكود مشروح في المرفقات.. ثم نغلق الشاشةونعود إلى الشاشة الأخرى ونذهب ألى TRIGEER على مستوى الفورم زنختار الحدث التالي WHEN_NEW_FORM_INSTANCE ونكتب الكود التالي :DECLAREB BLOCK;BEGINB:=FIND_BLOCK('EMP');IF(:PARAMETER.NO IS NOT NULL)THENSET_BLOCK_PROPERTY(B,DEFAULT_WHERE,'DEPTNO='||:PARAMETER.NO);GO_BLOCK('EMP');EXECUTE_QUERY;END IF;END;

  13. هل تود ان تشغل اي ملف exe من داخل الفورم ، والكثير يستخدم لملف الالة الحاسبة في بعض الشاشاتhost('"c:\winnt\notepad.exe")

  14. كيف يمكن ان الغلى الشاشه الخاصه بإدخالالusername/password عند فتح ال fmx بحيث ان يتم الإدخال تلقائى . • في التريجر (ON-LOGON) على مستوى الفورمضع الكود التاليمثال:LOGON('SCOTT','TIGER@SERVICE_NAEM');

  15. You can create this trigger to monitor how often you log on and off, or you may want to write a report on how long you are logged on for. When you specify ON SCHEMA,the trigger fires for the specific user. If you specify ON DATABASE, the trigger fires for all users

  16. Sample LOG_TRIG_TABLE: SQL> describe log_trig_table Name Null? Type ------------------------------- -------- ---- USER_ID VARCHAR2(30) LOG_DATE DATE ACTION VARCHAR2(30) SQL> select * from log_trig_table; USER_ID LOG_DATE ACTION -------------------------- --------- --------------------- A_USER 23-MAR-99 Logging off A_USER 23-MAR-99 Logging on

  17. The following example creates a logon statistics table and a LOGON and LOGOFF database trigger to capture the time when a user connects/disconnects to/from the database. CREATE TABLE session_logon_statistics (user_logged VARCHAR2(30), start_time DATE, end_time DATE); CREATE OR REPLACE TRIGGER logon_log_trigger AFTER LOGON ON DATABASE BEGIN INSERT INTO session_logon_statistics (user_logged, start_time) VALUES (USER, SYSDATE); END; / CREATE OR REPLACE TRIGGER logoff_log_trigger BEFORE LOGOFF ON DATABASE BEGIN UPDATE session_logon_statistics SET end_time = SYSDATE WHERE user_logged = USER AND end_time IS NULL; END;

  18. The following script retrieves the information from the session_logon_statistics table. SELECT user_logged, TO_CHAR(start_time, 'MM/DD/YYYY HH24:MI:SS') "START TIME", TO_CHAR(end_time, 'MM/DD/YYYY HH24:MI:SS') "END TIME" FROM session_logon_statistics order by user_logged, start_time;

More Related