Solved

How to add mysql insert trigger

Posted on 2013-11-02
2
545 Views
Last Modified: 2013-11-07
I need to create a a record in tblOrderStatus everytime a record is INSERTED in tblOrders.  I have a bit so far but my big question is how do I insert the tblOrder.id into tblOrderStatus with my trigger?


DELIMITER $$
CREATE TRIGGER after_orderPlaced
AFTER INSERT ON tblOrders
FOR EACH ROW
BEGIN

    INSERT INTO tblStatus
    SET tblLastTime = ADDTIME(Curtime(),'2:00:00'),
            tblLastDate = Curdate()
            tblOrderId = ??????????????
END
0
Comment
Question by:drhamel69
2 Comments
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39619740
You use the NEW keyword:

DELIMITER $$
CREATE TRIGGER after_orderPlaced
AFTER INSERT ON tblOrders
FOR EACH ROW BEGIN
   INSERT INTO tblStatus
   SET tblLastTime = ADDTIME(Curtime(),'2:00:00'),
   tblLastDate = CURDATE(),
   tblOrderId = NEW.id;
END$$
DELIMTER;

Open in new window

0
 
LVL 2

Author Closing Comment

by:drhamel69
ID: 39632106
Excellent!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to count the days a record spends in a step 21 51
awk and Pythagoras? 5 19
CLI command keep running after close 7 36
sql server query 12 25
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question