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

  • Home
  • SEARCH
  • 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 8162725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:49:33+00:00 2026-06-06T18:49:33+00:00

Let’s say an Order has many Line items and we’re storing the total cost

  • 0

Let’s say an Order has many Line items and we’re storing the total cost of an order (based on the sum of prices on order lines) in the orders table.

--------------
orders
--------------
id
ref
total_cost
--------------

--------------
lines
--------------
id
order_id
price
--------------

In a simple application, the order and line are created during the same step of the checkout process. So this means

INSERT INTO orders .... 

-- Get ID of inserted order record
INSERT into lines VALUES(null, order_id, ...), ...

where we get the order ID after creating the order record.

The problem I’m having is trying to figure out the best way to store the total cost of an order. I don’t want to have to

  1. create an order
  2. create lines on an order
  3. calculate cost on order based on lines then update record created in 1. in orders table

This would mean a nullable total_cost field on orders for starters…

My solution thus far is to have an order_totals table with a 1:1 relationship to the orders table. But I think it’s redundant. Ideally, since everything required to calculate total costs (lines on an order) is in the database, I would work out the value every time I need it, but this is very expensive.

What are your thoughts?

  • 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-06-06T18:49:36+00:00Added an answer on June 6, 2026 at 6:49 pm

    I will echo the other answers and say that, unless it’s unacceptably expensive to do so, I’d calculate the total cost from the lines table as-and-when required.

    Alternatively, one could define triggers that update orders.total_cost as appropriate. However, one would need to define triggers after INSERT, UPDATE and DELETE on lines:

    CREATE TRIGGER after_insert_lines AFTER INSERT ON lines FOR EACH ROW
      UPDATE orders SET total_cost = total_cost + NEW.price;
    
    CREATE TRIGGER after_update_lines AFTER UPDATE ON lines FOR EACH ROW
      UPDATE orders SET total_cost = total_cost - OLD.price + NEW.price;
    
    CREATE TRIGGER after_delete_lines AFTER DELETE ON lines FOR EACH ROW
      UPDATE orders SET total_cost = total_cost - OLD.price;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that each Product has a category. I want to ask the Users
Let's say I have a Job Scheduler which has 4 consumers A, B, C
Let's say an Owner has a collection of Watch(es). I am trying to create
Let's say I have one class User, and it has a property of type
Let's say you have a class library project that has any number of supplemental
Let's say I'm creating an OpenGL game in C++ that will have many objects
Let's say I have two objects, Master and Slave . Slave has a method
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say I'm outputting a post title and in our database, it's Hello Y’all
Let's say I have a statement which is several lines long: NSString *urlString =

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.