Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7064031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:47:04+00:00 2026-05-28T04:47:04+00:00

I am just learning about foreign keys. I am simplifying my tables to this:

  • 0

I am just learning about foreign keys. I am simplifying my tables to this:

Table:  item
Columns:  item_id (PK), qty

Table:  item_change_log
Columns:  id (PK), item_id (indexed), change_value

Both tables are INNODB.

Everytime a change in the item levels is made, the quantity changed is logged in the item_change_log.change_value. I want the change_value to automatically add/subtract item.qty.

The intended behavior:

User creates Item X. item.item_id = X, qty = 0. No record is made in item_change_log yet.
User changes quantity of X on hand to 2. item_change_log: id = 1, item_id = X, change_value = 2. This change_value automatically increases item.qty = 2 via foreign keys.

I know how to make the foreign keys for the item id’s see each other, but I don’t know how to add the “math” element, or if that is possible.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:47:05+00:00Added an answer on May 28, 2026 at 4:47 am

    What you need is a trigger, but which?

    • Since it is the item_change_log table your user actually modifies,
      you need a trigger on item_change_log
    • Since you allways insert into item_change_log, you need an INSERT
      trigger
    • Since You only want to update item only, if the insert to item_change_log
      really works, you want an AFTER INSERT trigger

    So here we go

    CREATE TRIGGER update_item_table AFTER INSERT ON item_change_log
      FOR EACH ROW BEGIN
        UPDATE item SET qty=qty+NEW.qty WHERE item_id=NEW.item_id;
      END;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning about POSIX threads right now, but I guess this is just a
I've just started learning about thread safety. This is making me code a lot
Hey guys, just learning about composition of classes and ran into this error. Gradebook.h
Very confused over this. I've just started learning about pointers and have now decided
I am just learning about Android Development so excuse me if this is a
I am just learning about app.config in respect of creating custom sections. I have
I'm just learning about SVG, and it seems great but I'm not sure about
I am just learning about computer vision and C#. It seems like two prominent
I am just learning about LINQ to XML in all its glory and frailty,
I'm after some good tips for fluent interfaces in C#. I'm just learning about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.