runtime
60 / 88

lesson 60 Database engineering · advanced

DML: INSERT, UPDATE & DELETE

Insert, update and delete records safely.

the idea

DML changes rows: INSERT adds, UPDATE modifies matching rows, DELETE removes matching rows. Every UPDATE and DELETE should have a WHERE clause that names exactly the rows you mean; run the same condition as a SELECT first to see what it will touch.

Dataset: Retail

The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.

products6 columns · 5,304 rows
stock_codetext · unique iddescriptiontextcategory_idnumber · links to categorieslist_pricedecimalfirst_sold_atdate & timeis_merchandisenumber
10002INFLATABLE POLITICAL GLOBE170.852009-12-01 09:08:001
10002RROBOT PENCIL SHARPNER85.172009-12-02 14:43:001
10080GROOVY CACTUS INFLATABLE170.392009-12-02 16:02:001
10109BENDY COLOUR PENCILS80.422009-12-03 12:31:001
10120DOGGY RUBBER170.212009-12-01 16:17:001
10123CHEARTS WRAPPING TAPE80.652009-12-01 14:28:001
stock_codetext · unique id
10002
descriptiontext
INFLATABLE POLITICAL GLOBE
category_idnumber · links to categories
17
list_pricedecimal
0.85
first_sold_atdate & time
2009-12-01 09:08:00
is_merchandisenumber
1
stock_codetext · unique id
10002R
descriptiontext
ROBOT PENCIL SHARPNER
category_idnumber · links to categories
8
list_pricedecimal
5.17
first_sold_atdate & time
2009-12-02 14:43:00
is_merchandisenumber
1
stock_codetext · unique id
10080
descriptiontext
GROOVY CACTUS INFLATABLE
category_idnumber · links to categories
17
list_pricedecimal
0.39
first_sold_atdate & time
2009-12-02 16:02:00
is_merchandisenumber
1
stock_codetext · unique id
10109
descriptiontext
BENDY COLOUR PENCILS
category_idnumber · links to categories
8
list_pricedecimal
0.42
first_sold_atdate & time
2009-12-03 12:31:00
is_merchandisenumber
1
stock_codetext · unique id
10120
descriptiontext
DOGGY RUBBER
category_idnumber · links to categories
17
list_pricedecimal
0.21
first_sold_atdate & time
2009-12-01 16:17:00
is_merchandisenumber
1
stock_codetext · unique id
10123C
descriptiontext
HEARTS WRAPPING TAPE
category_idnumber · links to categories
8
list_pricedecimal
0.65
first_sold_atdate & time
2009-12-01 14:28:00
is_merchandisenumber
1

first 6 of 5,304 rows · your queries use all of them

worked example

Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not count toward progress.

  1. Preview rows before you change them. Run the same WHERE you will use in UPDATE as a SELECT first. You see every Pets product (category 13) before anything is modified.the pattern

    example.sql
    SELECT stock_code, description, list_price
    FROM products
    WHERE category_id = 13
    ORDER BY stock_code;
  2. Update prices and check the result. The script bumps cheap Pets lines by £1, then SELECTs the category again so you can compare before and after in one disposable run.

    step-2.sql
    UPDATE products
    SET list_price = list_price + 1
    WHERE category_id = 13 AND list_price < 2;
    SELECT stock_code, list_price
    FROM products
    WHERE category_id = 13
    ORDER BY stock_code;

common mistake

UPDATE products SET list_price = list_price * 1.1 without WHERE reprices the entire catalog.

challenge 1

In category 13 (Pets): insert product 'PET001' described 'CAT TOY MOUSE' in category 13 priced 1.95 with first_sold_at '2011-12-10 09:00:00' and is_merchandise 1; raise the price of every Pets product priced below 5 by 0.50; delete product '20725' if it is in category 13.

Return stock_code, description and list_price for category 13.

what to return

Columns
stock_codedescriptionlist_price in this order
Rows
any order
Also checked
TEMP / table changes from your script
challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
DML: INSERT, UPDATE & DELETE — Database engineering · Runtime