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 :: how do you insert boolean to postgresql 
Sql :: SQL Greater Than Operator 
Sql :: how to link java and mysql 
Sql :: SQLSTATE[42S02]: Base table or view not found: 1146 Tabl 
Sql :: mysql procedure 
Sql :: sql basic commands 
Sql :: mysql login 
Sql :: sql table contains 
Sql :: Oracle Procedure ex2 
Sql :: restore backupfile discourse 
Sql :: why mongodb is better than sql 
Sql :: how to use 3 fields as primary key in sql tables? 
Sql :: tipos da linguagem SQL 
Sql :: mysql create index lost connection 
Sql :: oracle sql winter time change 
Sql :: mysql remote connection macos 
Sql :: formatting code with SQL Developer 
Sql :: oracle single row functions 
Sql :: db2 foreign keys 
Sql :: <connectionStrings <add name="MainDB" connectionString="Data Source=multidc02.its.com.pk,47328;Initial Catalog=ITSGeneralApplications;User ID=ITSGeneralAdmin;Password=TDsHn6TTyJohXCe"/ </connectionStrings 
Sql :: error-expression-select-list-not-group-by-nonaggregated-column/ 
Sql :: horizontal join sqlite 
Sql :: how to count with except in psql 
Sql :: pgsql commit rollback example 
Sql :: SQL Combining Multiple Operators 
Sql :: Second Step in installing SQL workbench 
Sql :: sub blocks in sql 
Sql :: SQL SERVER xquery count child nodes 
Sql :: jdbc:sqlserver://localhost;username=MyUsername;password={pass";{}}word}; 
Sql :: druid sql list all tables 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =