•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 374,006 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,766 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MySQL advertiser:
Views: 738 | Replies: 5
![]() |
•
•
Join Date: Apr 2006
Posts: 62
Reputation:
Rep Power: 3
Solved Threads: 0
Hi,
When i add new product into "Products" table, a trigger will add a record into "stocks" table. Example; stock=10,productId=99
How can i do that? Thanks
When i add new product into "Products" table, a trigger will add a record into "stocks" table. Example; stock=10,productId=99
How can i do that? Thanks
sql Syntax (Toggle Plain Text)
CREATE TABLE products( productId INT(10) NOT NULL AUTO_INCREMENT, productName VARCHAR(10), PRIMARY KEY (productId)); CREATE TABLE stocks( stock INT(10), productId INT(10), FOREIGN KEY (productId) REFERENCES products(productId));
Last edited by peter_budo : Apr 4th, 2008 at 2:15 pm. Reason: Keep It Organized - please use [code] tags
•
•
Join Date: Apr 2006
Posts: 62
Reputation:
Rep Power: 3
Solved Threads: 0
sql Syntax (Toggle Plain Text)
CREATE TRIGGER update_detail BEFORE UPDATE ON product FOR EACH ROW SET @id = SELECT id FROM product; SET @remain = SELECT amount FROM product; INSERT INTO detail (id,remain) VALUES (@id ,@remain );
Above code doesn't work. It says "id" cannot be null. Either SET doesn't set or insert line has problem. ?????
Last edited by peter_budo : Apr 6th, 2008 at 2:15 pm. Reason: Closing tag is [/code] not [icode]
•
•
Join Date: Apr 2006
Posts: 62
Reputation:
Rep Power: 3
Solved Threads: 0
sql Syntax (Toggle Plain Text)
CREATE TRIGGER update_detail BEFORE UPDATE ON product FOR EACH ROW IF OLD.amount<NEW.amount THEN SET NEW.girdi = NEW.amount - OLD.amount ; ELSEIF OLD.amount>NEW.amount THEN SET NEW.cikti = OLD.amount - NEW.amount ; END IF; INSERT INTO DETAIL (id, oldAmount, newAmount, girdi, cikti, kalan) VALUES (OLD.id, OLD.amount, NEW.amount, NEW.girdi, NEW.cikti, NEW.amount);
Error is on this line
ET NEW.girdi = NEW.amount - OLD.amount ;What is wrong?
Last edited by peter_budo : Apr 6th, 2008 at 2:15 pm. Reason: Closing tag is [/code] not [icode]
•
•
Join Date: Apr 2005
Location: Old Hampshire, Old England (LOL)
Posts: 11,937
Reputation:
Rep Power: 30
Solved Threads: 263
keep it organised!
Last edited by peter_budo : Apr 6th, 2008 at 2:30 pm. Reason: Just cleaning after merge
TRY MY SUGGESTIONS AT YOUR OWN RISK!
james.bennet1@ntlworld.com
james.bennet1@ntlworld.com
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 1,907
Reputation:
Rep Power: 9
Solved Threads: 216
@Fenerbahce please take your time to read forum policies as you are creating havoc around. Once you created post and your next question is related to original post just post new comment bellow the original post
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb MySQL Marketplace
Similar Threads
- Trigger help (MS SQL)
- problem with trigger (MS SQL)
- SQL trigger based on a true/false field value (MS SQL)
- Computed column in view VS trigger on update (MS SQL)
- Hi - how do you trigger a macro to run when a cell is updated in Excel? (Windows Software)
- Trigger onChange manually (HTML and CSS)
Other Threads in the MySQL Forum
- Previous Thread: database structure theory question?
- Next Thread: how to write PHP



Linear Mode