Create Trigger in phpMyAdmin code example

Example 1: Create Trigger in phpMyAdmin

DROP TRIGGER IF EXISTS testTrigger;
CREATE TRIGGER testTrigger 
BEFORE INSERT ON tbl_table1 
FOR EACH ROW 
BEGIN 
  INSERT INTO tbl_table2 VALUES (NEW.id,NEW.name);
END;

Example 2: Create Trigger in phpMyAdmin

DELIMITER &&
 
CREATE OR REPLACE TRIGGER tr_ajoutHotelResponsable
BEFORE INSERT ON cpoa.hotels
FOR EACH ROW
BEGIN
    IF(
        EXISTS(
            SELECT *
            FROM cpao.responsables
            WHERE numResponsable = NEW.numResponsable
        )
    )
    THEN
        UPDATE cpoa.responsables
        SET nbHotel = nbHotel + 1
        WHERE numResponsable = NEW.numResponsable;
    ELSE
        INSERT INTO cpao.responsables ( nom, prenom, nbHotels )
        VALUES ( 'nouveau', 'nouveau', 1 );
         
        SET NEW.numResponsable = LAST_INSERT_ID();
    END IF;
END&&
 
DELIMITER ;

Example 3: Create Trigger in phpMyAdmin

CREATE TRIGGER testTrigger 
BEFORE INSERT ON tbl_table1 
FOR EACH ROW 
BEGIN 
    INSERT INTO tbl_table2 (id,name) values (NEW.id,NEW.name); 
END

Example 4: Create Trigger in phpMyAdmin

CREATE TRIGGER after_update
       AFTER UPDATE ON hodm
       FOR EACH ROW 

       INSERT INTO hodm_log
       SET log_action = 'update',
        user_name = NEW.user_name,
           log_timestamp = NOW();

Example 5: Create Trigger in phpMyAdmin

delimiter $$
CREATE TRIGGER copy_pid AFTER INSERT on upload FOR EACH ROW 

BEGIN

INSERT INTO rating (uid,pid) VALUES (NEW.username,NEW.id);

END$$
delimiter ;

Example 6: Create Trigger in phpMyAdmin

DELIMITER //

CREATE TRIGGER contacts_after_insert
AFTER INSERT
   ON contacts FOR EACH ROW

BEGIN

   DECLARE vUser varchar(50);

   -- Find username of person performing the INSERT into table
   SELECT USER() INTO vUser;

   -- Insert record into audit table
   INSERT INTO contacts_audit
   ( contact_id,
     created_date,
     created_by)
   VALUES
   ( NEW.contact_id,
     SYSDATE(),
     vUser );

END; //

DELIMITER ;

Example 7: Create Trigger in phpMyAdmin

CREATE TRIGGER `before_delete` BEFORE DELETE ON `barcode_master` 
FOR EACH ROW BEGIN 
UPDATE library_emp_master SET job_type = 1 WHERE barcode_no =   OLD.barcode_no; 
END;

Example 8: Create Trigger in phpMyAdmin

CREATE TABLE contacts
( contact_id INT(11) NOT NULL AUTO_INCREMENT,
  last_name VARCHAR(30) NOT NULL,
  first_name VARCHAR(25),
  birthday DATE,
  CONSTRAINT contacts_pk PRIMARY KEY (contact_id)
);

Example 9: Create Trigger in phpMyAdmin

delimiter //
CREATE TRIGGER before_insert_money BEFORE INSERT ON money
FOR EACH
ROW
BEGIN
    UPDATE accounts SET balance=10.0;
END;
//
delimiter ;

Example 10: Create Trigger in phpMyAdmin

CREATE TRIGGER nomCltInsert
BEFORE INSERT ON client
FOR EACH ROW
    UPDATE client
    SET
        NEW.NomClient = UPPER(NEW.NomClient),
        NEW.PrenomClient = CONCAT( UPPER( LEFT(NEW.PrenomClient,1) ) , LOWER( SUBSTR( NEW.PrenomClient,2 ) ) );
 
CREATE TRIGGER nomCltUpdate
BEFORE UPDATE ON client
FOR EACH ROW
    UPDATE client
    SET
        NEW.NomClient = UPPER(NEW.NomClient),
        NEW.PrenomClient = CONCAT( UPPER( LEFT(NEW.PrenomClient,1) ) , LOWER( SUBSTR( NEW.PrenomClient,2 ) ) );

Tags:

Sql Example