Search
 
SCRIPT & CODE EXAMPLE
 

SQL

trigger in mysql syntax

CREATE TRIGGER trigger_name
{BEFORE | AFTER} {INSERT | UPDATE| DELETE }
ON table_name FOR EACH ROW
trigger_body;
Comment

mysql trigger

CREATE TABLE test1(a1 INT);
CREATE TABLE test2(a2 INT);
CREATE TABLE test3(a3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
CREATE TABLE test4(
  a4 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  b4 INT DEFAULT 0
);

delimiter //
CREATE TRIGGER name_of_trigger [BEFORE|AFTER] [INSERT|UPDATE|DELETE] 
ON test1
FOR EACH ROW
BEGIN
	/* 
    Examples of code to write 
    */
	[INSERT|DELETE|UPDATE|IF|ELSEIF|END IF];
    /* 
    NEW.a1 meanse the new value that will be added with INSERT into test1
    */
    INSERT INTO test2 SET a2 = NEW.a1;
    DELETE FROM test3 WHERE a3 = NEW.a1;
    UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
    IF NEW.a1 < 0 THEN
    	SET NEW.a1 = 0;
    ELSEIF NEW.a1 > 100 THEN
    	SET NEW.a1 = 100;
    END IF;
END//
delimiter;
Comment

trigger in mysql

CREATE TRIGGER Product_Details_tr 
BEFORE INSERT ON Product_Details 
FOR EACH ROW 
SET NEW.User_ID = CURRENT_USER();
Comment

mysql Trigger

select trigger_schema, trigger_name, action_statement
from information_schema.triggers
Comment

Mysql create trigger

CREATE DEFINER=`root`@`localhost` TRIGGER increment_animal
  AFTER INSERT ON animals FOR EACH ROW
    UPDATE animal_count SET animal_count.animals = animal_count.animals+1;
ERROR 1359 (HY000): Trigger already exists

CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER increment_animal
  AFTER INSERT ON animals  FOR EACH ROW
    UPDATE animal_count SET animal_count.animals = animal_count.animals+1;
Query OK, 0 rows affected (0.12 sec)

CREATE DEFINER=`root`@`localhost` TRIGGER IF NOT EXISTS increment_animal
  AFTER INSERT ON animals FOR EACH ROW
    UPDATE animal_count SET animal_count.animals = animal_count.animals+1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql [localhost] {msandbox} (test) > SHOW WARNINGS;
+-------+------+------------------------+
| Level | Code | Message                |
+-------+------+------------------------+
| Note  | 1359 | Trigger already exists |
+-------+------+------------------------+
1 row in set (0.00 sec)
Comment

PREVIOUS NEXT
Code Example
Sql :: enum in sql server 
Sql :: mysql sort asc numeric 
Sql :: what data type to use for phone number in sql 
Sql :: sql or 
Sql :: delete insert record in sql server 
Sql :: create-table 
Sql :: sql change column name based on value 
Sql :: update join 
Sql :: dump sql file to database postgres 
Sql :: TRIGGER AFTER 
Sql :: devilbox mysqldump 
Sql :: how to add more columns to a table in mysql 
Sql :: sqlite create record 
Sql :: querry mysql by 2 columns 
Sql :: mysql check all tables 
Sql :: postgres show table schema 
Sql :: How to take sum of column with same id and different table in SQL? 
Sql :: compression of dabatase mysqldumo 
Sql :: sql recursive query 
Sql :: how to use select union and loop 
Sql :: DIFFERENCE BETWEEN 2 COLN IN SQL 
Sql :: number(10 2) in sql means 
Sql :: what is subquery in sql 
Sql :: create procedure 
Sql :: timestamp to date sql server 
Sql :: rename view mysql 
Sql :: select query in sql 
Sql :: PG::ForeignKeyViolation: ERROR: update or delete on table violates foreign key constraint 
Sql :: how to print some string in mysql 
Sql :: soql last year 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =