Muhammad al-xorazmiy nomidagi toshkent axborot texnologiyalari universiteti mustaqil-1-2-3-4-5


Download 131.16 Kb.
bet4/7
Sana31.01.2024
Hajmi131.16 Kb.
#1818316
1   2   3   4   5   6   7
Bog'liq
Afzalbek (1)

Triger Yaratish:

Triger yaratish uchun CREATE TRIGGER so'zini ishlatamiz. Triger nomi, tadbir (BEFORE, AFTER), tadbirni qanday vaqt amalga oshirish (INSERT, UPDATE, DELETE), jadval nomi, va amalga oshiriladigan tadbirni bajarish uchun SQL so'rovlari kiritiladi. Misol:
sqlCopy code
CREATE TRIGGER FoydalanuvchiQo'shilgandaTriger AFTER INSERT ON foydalanuvchilar FOR EACH ROW INSERT INTO xabarlar (foydalanuvchi_id, matn) VALUES (NEW.id, 'Yangi foydalanuvchi qo\'shildi');

Triggerlar Yaratilishi xar bir Customers uchun !!!!
CREATE FUNCTION
-- Create a trigger function for INSERT
CREATE OR REPLACE FUNCTION product_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO AuditLog (product_id, action_type, timestamp)
VALUES (NEW.product_id, 'INSERT', NOW());
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- Create a trigger for INSERT


CREATE TRIGGER product_insert_trigger
AFTER INSERT ON Products
FOR EACH ROW
EXECUTE FUNCTION product_insert_trigger();

-- Create a trigger function for UPDATE


CREATE OR REPLACE FUNCTION product_update_trigger()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO AuditLog (product_id, action_type, timestamp)
VALUES (NEW.product_id, 'UPDATE', NOW());
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- Create a trigger for UPDATE


CREATE TRIGGER product_update_trigger
AFTER UPDATE ON Products
FOR EACH ROW
EXECUTE FUNCTION product_update_trigger();

-- Create a trigger function for DELETE


CREATE OR REPLACE FUNCTION product_delete_trigger()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO AuditLog (product_id, action_type, timestamp)
VALUES (OLD.product_id, 'DELETE', NOW());
RETURN OLD;
END;
$$ LANGUAGE plpgsql;

-- Create a trigger for DELETE


CREATE TRIGGER product_delete_trigger
AFTER DELETE ON Products
FOR EACH ROW
EXECUTE FUNCTION product_delete_trigger();

PostgreSQLda "tasavvurlar" (schemas) va "sinonimlar" (synonyms) mavjud emas. Ammo, ularni o'rniga, schema va view (ko'rib chiqish)larni ishlatishingiz mumkin.




  1. Download 131.16 Kb.

    Do'stlaringiz bilan baham:
1   2   3   4   5   6   7




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling