I'm creating a database that contains four different tables:
CUSTOMER (PK CUST_ID, CUST_NAME, CUST_ADDRESS)
INVOICE (PK INVOICE_ID, FK CUST_ID, INVOICE_AMOUNT)
PRODUCT (PK PRODUCT_ID, PRODUCT_NAME, PRODUCT_COST)
INVOICE_ITEM (PK FK INVOICE_ID, PK FK PRODUCT_ID,
INVOICE_ITEM_QUANTITY, INVOICE_ITEM_PRICE)
I need to create a transaction that 1). inserts data into a new CUSTOMER
, 2). inserts data into a new INVOICE
(invoice amount set to 0), 3). inserts data of two new INVOICE_ITEM
(one row has INVOICE_ITEM_QUANTITY
set to 2 and INVOICE_ITEM_PRICE
to 5.00, the other to 1 and 10.00).
I can do all of this without any issue using insert commands, but the next step is tripping me up:
'Update the INVOICE_AMOUNT to add the line items added in step 3 to the INVOICE_ITEM_PRICE field. You must do this with a query that would work regardless if there were existing INVOICE_ITEM rows for this INVOICE. Your query cannot hard code the amount.'
I can't figure out how I would go about doing this. I've been researching solutions, but haven't found anything that describes this problem, only ones that work if the column name is the same.
I could just insert the values directly into the INVOICE_AMOUNT
, but then it would be considered hard coding the answer, correct? So I've been trying to do something along the lines of
UPDATE INVOICE
SET INVOICE_ITEM.INVOICE_ITEM_PRICE = INVOICE.INVOICE_AMOUNT
WHERE INVOICE.INVOICE_ID = INVOICE_ITEM.INVOICE_ID
but I can't get any variation of this code to work. I'm sure there is some really simple answer that will make me feel stupid, but if anyone can help, I'd greatly appreciate it.