Question

In: Computer Science

1) Write an INSERT statement that adds this row to the Invoices table: i nvoice_id: The...

1) Write an INSERT statement that adds this row to the Invoices table: i

nvoice_id: The next automatically generated ID

vendor_id : 32

invoice_number: AX-014-027

invoice_date: 8/1/2014

invoice_total: $434.58

payment_total: $0.0

credit_total: $0.0

terms_id: 2

invoice_due_date: 8/31/2014

payment_date: null

Write this statement without using a column list.: Use DEFAULT to insert the automatically generated ID.

2) Write an INSERT statement that adds these rows to the Invoice_line_Items table:

invoice_sequence: 1 2

account_number: 160 527

line_item_amount: $180.23 $254.35

line_item_description: Hard drive Exchange Server update

Set the invoice_id of these two rows to the invoice ID that was generated by MySQL for the invoice you added in question 1

3) Write an UPDATE statement that modifies the invoice you added in question 1. This statement should change the credit_total column so its 10% of the invoice_total column, and it should change the payment_total column so that the sum of the payment_total and credit_total are equal to the invoice_total column.

4) Write a DELETE statement that deletes the row that you added to the Invoices table in question 1. When you execute this statement, it will produce an error since the invoice has related rows in the Invoice_Line_Items table. To fix that, precede the DELETE statement with another DELETE statement that deletes the line items for this invoice. (Remember that to code two or more statements in a script, you must end each statement with a semicolon.

Solutions

Expert Solution

  • INSERT statement to add specified row in Invoices table

Explanation: I have specified 2 options to insert the data. This is because I am unsure if the type of columns invoice_total, payment_total, credit_total is to store decimal values only or do we need to store '$' symbol as well. You may select the right insert statement based on data type of those columns and if you want to store '$' symbol.

-- Option 1 - If the columns invoice_total, payment_total and credit_total are of type decimal
INSERT INTO Invoices 
VALUES (DEFAULT, 32, 'AX-014-027', STR_TO_DATE('8/1/2014', '%m/%d/%Y'), 434.58, 0.0, 0.0, 2, STR_TO_DATE('8/31/2014', '%m/%d/%Y'), null);

-- Option 2 - If you have to store '$' sign in columns for invoice_total, payment_total and credit_total
INSERT INTO Invoices 
VALUES (DEFAULT, 32, 'AX-014-027', STR_TO_DATE('8/1/2014', '%m/%d/%Y'), '$434.58', '$0.0', '$0.0', 2, STR_TO_DATE('8/31/2014', '%m/%d/%Y'), null);
  • INSERT statement to add 2 rows to Invoice_line_Items table

Explanation: I have specified 2 options to insert the data. This is because I am unsure if the type of column line_item_amount is to store decimal values or do we need to store '$' symbol as well. You may select the right insert statement based on data type of the column and if you want to store '$' symbol.

In addition, I have written single INSERT statement to insert multiple rows. You may split them into multiple INSERT statements if you like.

Important note: LAST_INSERT_ID() function provides value of automatically generated ID. In INSERT statement on Invoices table, we have used automatically generated ID for invoice_id, the value of this will be available in function LAST_INSERT_ID()

--Option 1 - If column line_item_amount is of type decimal
INSERT INTO Invoice_line_items 
VALUES (1, 160, 180.23, 'Hard drive', LAST_INSERT_ID()),
       (2, 527, 254.35, 'Exchange Server update', LAST_INSERT_ID());

--Option 2 - If you want to store '$' symbol in column line_item_amount
INSERT INTO Invoice_line_items 
VALUES (1, 160, '$180.23', 'Hard drive', LAST_INSERT_ID()),
       (2, 527, '$254.35', 'Exchange Server update', LAST_INSERT_ID());
  • UPDATE statement to modify credit_total and invoice_total

Explanation: I have specified 2 options to insert the data. This is because I am unsure if the type of columns invoice_total, payment_total, credit_total is to store decimal values only or do we need to store '$' symbol as well. You may select the right insert statement based on data type of those columns and if you want to store '$' symbol.

The second option seems more complicated as you need to remove '$' sign first, convert the remaining amount to decimal format and then concat '$' symbol back before updating the value in table.

Here we again use LAST_INSERT_ID() function to get the invoice_id added in first INSERT statement.

--Option 1 - If the columns invoice_total, payment_total and credit_total are of type decimal
UPDATE INVOICES
SET credit_total = 0.1 * invoice_total,
payment_total = invoice_total - credit_total
WHERE invoice_id = LAST_INSERT_ID();

--Option 2 - If you have to store '$' sign in columns for invoice_total, payment_total and credit_total
UPDATE INVOICES
SET credit_total = concat('$', 0.1 * convert(replace(invoice_total,'$',''),decimal(10,2))),
payment_total = concat('$', convert(replace(invoice_total,'$',''),decimal(10,2)) - convert(replace(credit_total,'$',''),decimal(10,2)))
WHERE invoice_id = LAST_INSERT_ID();
  • DELETE row from from invoice_line_items and invoices tables for invoice_id added in first INSERT statement

Explanation: We first delete data from child table (Invoice_line_items) as it has rows related to data being deleted from parent table (Invoices). We use LAST_INSERT_ID() function to get the invoice_id inserted using first INSERT statement.

DELETE FROM Invoice_line_items
WHERE invoice_id = LAST_INSERT_ID();

DELETE FROM INVOICES
WHERE invoice_id = LAST_INSERT_ID();

Related Solutions

This assignment refer to the “om” database (or Schema).      1.Write an INSERT statement that adds this...
This assignment refer to the “om” database (or Schema).      1.Write an INSERT statement that adds this row to the Items table:   a.       Artist:               Newly Added b.       Title:                      Assignment 3 c.        unit_price:         0.0 d.       ID:                          12 2.       Write an UPDATE statement that modifies the row you just added to the Items table. This statement should change the artist column to “RockOn”, and it should use the ID column to identify the row.    3.       Write a DELETE statement that deletes the row you...
Question 1. Write the statement to increase the invoice_total by 25% using the INVOICES table for...
Question 1. Write the statement to increase the invoice_total by 25% using the INVOICES table for all vendors whose vendor_id is less than 100. Question 2. Write the statement to answer the following question: What is the total of the invoice_total amounts after the update. I need your statement and the result stated here. Question 3. Write the statements to list the customer ID and Order ID from the ORDERS table for all orders with order id greater than 700....
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table as...
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table as if it were being added to MySQL (please give explanations for each line of SQL code and a copy of the code as it would be entered into the query by itself: Customer PK Customer ID Text Phone Number int name text address ID int email text FK vendor ID int Vendor is the name of the table the FK comes from.
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table: Customer...
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table: Customer PK Customer ID Text Phone Number int name text address ID int email text FK vendor ID int Vendor is the name of the table the FK comes from.
1. Write a query to: a. select data from INVOICES table as follows: Invoice date in...
1. Write a query to: a. select data from INVOICES table as follows: Invoice date in MM/DD/YYYY format Invoice Date in DD-Mon-YYYY format Invoice Total rounded to the nearest dollar Note: you can alias columns as you sit fit b. select data from VENDORS table as follows: Vendor Name Concatenate Vendor Name with the string ‘s Address Concatenate Vendor City, Vendor State and Vendor Zip Code (alias this) Your output should look like this (this is just an example of...
Question 1 – Inserting data to the CITY table Using the following DML statement: INSERT INTO...
Question 1 – Inserting data to the CITY table Using the following DML statement: INSERT INTO CITY (id, name, countrycode, district, population) VALUES (7000, ‘Guelph’, ‘CAN’, ‘Ontario’, 131794); Query the CITY table to ensure this row was inserted correctly. Provide screenshot of your ‘SELECT statement’ and resultset pane here                Insert another row into the CITY table with the following values:                               Id                           7002                               Name                   Montebello                               Countrycode       CAN                               District                 Quebec                               Population          983 Provide screenshot...
/* 1. Fix the CREATE and INSERT statements below to create the SHIPMENT table and insert...
/* 1. Fix the CREATE and INSERT statements below to create the SHIPMENT table and insert its data in DB Fiddle*/ CREATE TABLE SHIPMENT ( ShipmentID Int NOT NULL, ShipperName Char(35) NOT NULL, ShipperInvoiceNumber Int NOT NULL DepartureDate Date NULL, ArrivalDate Date NULL, InsuredValue Numeric(12,2) NOT NULL, CONSTRAINT Shipment_PK PRIMARY KEY (ShipmentID)) ); INSERT INTO SHIPMENT VALUES (1,'ABC Trans-Oceanic', 2008651, '10-Dec-14', '15-Mar-18', 15000.00); INSERT INTO SHIPMENT VALUES (2,'ABC Trans-Oceanic', 2009012, '10-Jan-18', '20-Mar-18', 12000.00); INSERT INTO SHIPMENT VALUES (3,'Worldwide', 49100300, '05-May-18',...
Python linked lists ● Insert: this method takes a value as a parameter, and adds a...
Python linked lists ● Insert: this method takes a value as a parameter, and adds a node which contains the value to the end of the linked list ● Delete: this method deletes a node from the linked list. If an index is passed as a parameter, then the method should delete the node at this index. If no index is passed, then delete the first item in the list ● Find: this method takes a value as a parameter,...
Insert: this method takes a value as a parameter and adds a node which contains the...
Insert: this method takes a value as a parameter and adds a node which contains the value to the end of the linked list Delete: This method deletes a node from the linked list. If an index is passed as a parameter, then the method should delete the node at this index. If no index is passed, then delete the first item in the list Find: this method takes a value as a parameter, and returns the index of the...
Write a program to insert the following elements into a hash table of size 17. The...
Write a program to insert the following elements into a hash table of size 17. The hash function is X mod 17 where X is the input element.   6, 12, 34, 29, 28, 11, 23, 7, 0, 33, 30, 45 Use linear probing to resolve any collisions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT