User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Apr 2006
Posts: 62
Reputation: Fenerbahce is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Fenerbahce Fenerbahce is offline Offline
Junior Poster in Training

Trigger Help

  #1  
Apr 4th, 2008
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

  1. CREATE TABLE products(
  2. productId INT(10) NOT NULL AUTO_INCREMENT,
  3. productName VARCHAR(10),
  4. PRIMARY KEY (productId));
  5.  
  6. CREATE TABLE stocks(
  7. stock INT(10),
  8. productId INT(10),
  9. 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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2006
Posts: 62
Reputation: Fenerbahce is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Fenerbahce Fenerbahce is offline Offline
Junior Poster in Training

Trigger

  #2  
Apr 6th, 2008
  1.  
  2. CREATE TRIGGER update_detail BEFORE UPDATE ON product
  3. FOR EACH ROW
  4.  
  5. SET @id = SELECT id FROM product;
  6. SET @remain = SELECT amount FROM product;
  7.  
  8. INSERT INTO detail (id,remain) VALUES (@id ,@remain );
  9.  

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]
Reply With Quote  
Join Date: Apr 2006
Posts: 62
Reputation: Fenerbahce is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Fenerbahce Fenerbahce is offline Offline
Junior Poster in Training

trigger 2

  #3  
Apr 6th, 2008
  1.  
  2. CREATE TRIGGER update_detail
  3. BEFORE UPDATE ON product
  4. FOR EACH ROW
  5.  
  6. IF OLD.amount<NEW.amount THEN
  7. SET NEW.girdi = NEW.amount - OLD.amount ;
  8. ELSEIF OLD.amount>NEW.amount THEN
  9. SET NEW.cikti = OLD.amount - NEW.amount ;
  10. END IF;
  11.  
  12. INSERT INTO DETAIL
  13. (id, oldAmount, newAmount, girdi, cikti, kalan)
  14. VALUES
  15. (OLD.id, OLD.amount, NEW.amount, NEW.girdi, NEW.cikti, NEW.amount);
  16.  

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]
Reply With Quote  
Join Date: Apr 2005
Location: Old Hampshire, Old England (LOL)
Posts: 11,937
Reputation: jbennet is a jewel in the rough jbennet is a jewel in the rough jbennet is a jewel in the rough jbennet is a jewel in the rough 
Rep Power: 30
Solved Threads: 263
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Microsoft Fanboy

Re: Trigger

  #4  
Apr 6th, 2008
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
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 1,907
Reputation: peter_budo has a spectacular aura about peter_budo has a spectacular aura about peter_budo has a spectacular aura about 
Rep Power: 9
Solved Threads: 216
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Posting Virtuoso

Re: Trigger

  #5  
Apr 6th, 2008
@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.
Reply With Quote  
Join Date: Apr 2006
Posts: 62
Reputation: Fenerbahce is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Fenerbahce Fenerbahce is offline Offline
Junior Poster in Training

Re: Trigger

  #6  
Apr 7th, 2008
sorry
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MySQL Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the MySQL Forum

All times are GMT -4. The time now is 10:23 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC