• Qo’llanilishi
  • CREATE TRIGGER trigger_name trigger_time trigger_event




    Download 117,21 Kb.
    bet3/4
    Sana03.12.2023
    Hajmi117,21 Kb.
    #110281
    1   2   3   4

    CREATE TRIGGER trigger_name trigger_time trigger_event

    ON tbl_name FOR EACH ROW trigger_stmt* Ushbu kod manba kodi bilan ta'kidlangan


    trigger_name — trigerni nomi
    trigger_time — trigerni ishlaash vaqti BEFORE — Bajarishdan oldin. AFTER — bajarishdan keyin.
    trigger_event — Hodisa:
    insert — Hodisa insert, data load, replace operatorlar bilan amalga oshadi.
    update — Hodisa update operatori bilan amalga oshadi.
    delete — Hodisa delete, replace operatorlar bilan amalga oshaditbl_name — jadval nomi.
    trigger_stmt trigger aktivlashganda bajariladigan ifoda
    Quyidagi hodisalar uchun triggerlar yaratish mumkin:



    BEFORE INSERT va AFTER INSERT
    BEFORE UPDATE va AFTER UPDATE
    BEFORE DELETE va AFTER DELETE

    Qo’llanilishi


    CREATE TABLE Hisob (H_id INT, Middori DECIMAL(10,2));

    CREATE TRIGGER summa BEFORE INSERT ON hisob

    FOR EACH ROW SET @sum = @sum + NEW.Middori;


    O'zgaruvchidan oldingi @ belgisi global o'zgaruvchini anglatadi. Uning mysql serveri triggerdan chiqqandan keyin eslab qoladi va uni keyinchalik, masalan, bu kabi ham chaqirish mumkin.

    select @sum;


    New degani biz eski qiymatni emas, balki yangi qiymatni olishimizni anglatadi (OLD eskisi uchun ishlatiladi).


    FOR EACH ROW- bu siklga o'xshash narsa, ya'ni trigger biriktirilgan jadvalning har bir qatori uchun keyingi nuqta-vergulga o'tiladi.

    mysql> set @sum=0;

    Query OK, 0 rows affected (0.08 sec)


    mysql> INSERT INTO hisob VALUES(137,14.98),(141,1937.50),(97,-100.00);

    Query OK, 3 rows affected (0.29 sec)

    Records: 3 Duplicates: 0 Warnings: 0



    Download 117,21 Kb.
    1   2   3   4




    Download 117,21 Kb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    CREATE TRIGGER trigger_name trigger_time trigger_event

    Download 117,21 Kb.