Solved

How to add mysql insert trigger

Posted on 2013-11-02
2
532 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 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
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
Comment Utility
Excellent!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now