peri hokiperihokiclarustologisticsrimashaop.comangelpaintingtools.co.iningin cuan cepat joki profesional gates of olympus perihokijalan perihoki pasti cuan dari spin scatter hitam mahjong ways 2 pgsoftmahjong wins 3 sukses dengan teknik pancingan gacor perihkibanyak jalan alternatif baccarat online di perihokikemenangan substansial mahjong ways 2 keuntungan nyata perihokiscatter hitam mahjong wins 3 spin perihoki pasti jeperahasia rtp live pola pragmatic gates of olympus duta76trik duta7 pgsoft mahjong ways 2 modal receh maxwingampang meraih keberuntungan di mahjong wins 3 duta76cara dapat saldo lewat jalur pintas dadu sicbo duta76spin mahjong ways 2 pgsoft scatter hitam maxwin duta76duta76 berikan kemenangan konsisten di mahjong wins 3mahjong wins bantu ekonomi keluargaabc1131 bahas rtp live mahjong pgsoftmomen menarik mahjong wins yang harus anda ketahuipesona scatter wild mahjong ways2spin pelan mahjong ways penghasil bonus harianstrategi unik kakek royteknik memancing scatter hitam untuk meraih cuaninovasi mahjong wins untuk pemain barumenggugah dewa naga mahjong ways munculkan scatter hitammenikmati pola mahjong ways sambil bersantaimeningkatkan peluang dengan jeda spin mahjong wayspeluang kaya dalam scatter wild

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

After Insert Trigger trong MySQL - Freetuts
VIEW
STORED PROCEDURE
INDEX
FULL TEXT SEARCH
TRIGGER
TIPS
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

After Insert Trigger trong MySQL

Trong bài này chúng ta sẽ tìm hiểu After Insert Trigger trong MySQL, đây là loại trigger được gọi sau khi dữ liệu được insert vào table.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Như thường lệ, chúng ta sẽ học cú pháp của nó trước, sau đó sẽ làm một vài ví dụ với các bước test trigger kỹ càng trước khi public vào dự án.

1. Cú pháp After Insert Trigger trong MySQL

After Insert Trigger sẽ được tự động gọi sau khi sự kiện insert xảy ra và hoàn thành.

Cú pháp dưới đây sẽ giúp bạn tạo được After Insert Trigger.

Bài viết này được đăng tại [free tuts .net]

CREATE TRIGGER trigger_name
    AFTER INSERT
    ON table_name FOR EACH ROW
        trigger_body

Trong cú pháp này thì bạn chú ý lệnh AFTER INSERT, nó giúp MySQL phân biệt được đây là loại After Insert Trigger.

Nếu bạn có nhiều dòng lệnh trong phần body thì hãy đặt nó trong cặp BEGIN .. BODY.

DELIMITER $$

CREATE TRIGGER trigger_name
    AFTER INSERT
    ON table_name FOR EACH ROW
BEGIN
    -- statements
END$$    

DELIMITER ;

Với loại trigger này bạn có thể truy xuất được giá trị của NEW nhưng không thay đổi được chúng, bởi bản chất nó đã insert xong rồi. Riêng đối với OLD thì không được vì nó không hỗ trợ trong loại INSERT Trigger.

MySQL AFTER INSERT Trigger png

2. Ví dụ After Insert Trigger trong MySQL

Chúng ta sẽ làm các bước tương tự như bài trước, nhưng sẽ tạo một một số table mới và thao tác trên đó.

Tạo database

Bạn hãy chạy lệnh SQL dưới đây để tạo bảng members.

DROP TABLE IF EXISTS members;

CREATE TABLE members (
    id INT AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(255),
    birthDate DATE,
    PRIMARY KEY (id)
);

Tiếp theo tạo thêm bảng reminders để lưu trữ tin nhắn nhắc nhở cho các thành viên.

DROP TABLE IF EXISTS reminders;

CREATE TABLE reminders (
    id INT AUTO_INCREMENT,
    memberId INT,
    message VARCHAR(255) NOT NULL,
    PRIMARY KEY (id , memberId)
);

Tạo trigger

Đoạn code trigger dưới đây sẽ thêm một tin nhắn vào bảng reminders nếu người dùng không nhập dữ liệu cho field birthDate.

DELIMITER $$

CREATE TRIGGER after_members_insert
AFTER INSERT
ON members FOR EACH ROW
BEGIN
    IF NEW.birthDate IS NULL THEN
        INSERT INTO reminders(memberId, message)
        VALUES(new.id,CONCAT('Hi ', NEW.name, ', please update your date of birth.'));
    END IF;
END$$

DELIMITER ;

Mình có sử dụng lệnh if else để kiểm tra giá trị NEW.birthDate có null hay không, nếu null thì thêm một dòng tin nhắn cho member đó.

Test Trigger

Ok, bước cuối cùng và quan trọng nhất là test xem trigger hoạt động có ổn định hay không. Khá đơn giản, ta chỉ cần thêm một vài dòng dữ liệu và xem data của 2 table là được.

Mình sẽ thêm 2 dòng dữ liệu, trong đó có 1 dòng ngày sinh có giá trị null.

INSERT INTO members(name, email, birthDate)
VALUES
    ('John Doe', 'john.doe@example.com', NULL),
    ('Jane Doe', 'jane.doe@example.com','2000-01-01');

Dữ liệu lúc này trong bảng members sẽ như sau:

MySQL AFTER INSERT Trigger example png

Nếu trigger hoạt động thì trong bảng reminders phải sẽ được thêm một record tin nhắn, và đó chính là dòng đầu tiên.

SELECT * FROM reminders;    

Dữ liệu trả về là:

MySQL AFTER INSERT Trigger Output png

Như vậy là trigger đã hoạt động đúng, bạn không còn lo lắng gì khi public nó cho dự án của mình.

Như vậy trong bài này mình đã hướng dẫn xong cách sử dụng After Insert Trigger trong MySQL. Sẽ rất đơn giản cho những bài sau khi bạn thực sự hiểu nguyên tắc hoạt động của bài này và bài trước.

Cùng chuyên mục:

Hàm ENCRYPT trong MySQL

Hàm ENCRYPT trong MySQL

Cách sử dụng ENCRYPT trong MySQL

Hàm MD5 trong MySQL

Hàm MD5 trong MySQL

Cách sử dụng MD5 trong MySQL

Hàm OLD_PASSWORD trong MySQL

Hàm OLD_PASSWORD trong MySQL

Cách sử dụng OLD_PASSWORD trong MySQL

Hàm PASSWORD trong MySQL

Hàm PASSWORD trong MySQL

Cách sử dụng PASSWORD trong MySQL

Hàm ASCII trong MySQL

Hàm ASCII trong MySQL

Cách sử dụng ASCII trong MySQL

Hàm CHAR_LENGTH trong MySQL

Hàm CHAR_LENGTH trong MySQL

Cách sử dụng CHAR_LENGTH trong MySQL

Hàm CHARACTER_LENGTH trong MySQL

Hàm CHARACTER_LENGTH trong MySQL

Cách sử dụng CHARACTER_LENGTH trong MySQL

Hàm CONCAT trong MySQL

Hàm CONCAT trong MySQL

Cách sử dụng CONCAT trong MySQL

Hàm CONCAT_WS trong MySQL

Hàm CONCAT_WS trong MySQL

Cách sử dụng CONCAT_WS trong MySQL

Hàm FIELD trong MySQL

Hàm FIELD trong MySQL

Cách sử dụng FIELD trong MySQL

Hàm FIND_IN_SET trong MySQL

Hàm FIND_IN_SET trong MySQL

Cách sử dụng FIND_IN_SET trong MySQL

Hàm FORMAT trong MySQL

Hàm FORMAT trong MySQL

Cách sử dụng FORMAT trong MySQL

Hàm INSERT trong MySQL

Hàm INSERT trong MySQL

Cách sử dụng INSERT trong MySQL

Hàm INSTR trong MySQL

Hàm INSTR trong MySQL

Cách sử dụng INSTR trong MySQL

Hàm LCASE trong MySQL

Hàm LCASE trong MySQL

Cách sử dụng LCASE trong MySQL

Hàm LEFT trong MySQL

Hàm LEFT trong MySQL

Cách sử dụng LEFT trong MySQL

Hàm LENGTH trong MySQL

Hàm LENGTH trong MySQL

Cách sử dụng LENGTH trong MySQL

Hàm LOCATE trong MySQL

Hàm LOCATE trong MySQL

Cách sử dụng LOCATE trong MySQL

Hàm LOWER trong MySQL

Hàm LOWER trong MySQL

Cách sử dụng LOWER trong MySQL

Hàm LPAD trong MySQL

Hàm LPAD trong MySQL

Cách sử dụng LPAD trong MySQL

Top