DML (for MyShop)
-
Insert a Product:
INSERT INTO myshop.products (name, quantity) VALUES ('Laptop', 15);INSERT INTO: Adds new rows to a table.- Inserts a product named 'Laptop' with a quantity of 15 into the
productstable.
-
Insert Another Product:
INSERT INTO myshop.products (name, quantity) VALUES ('Smartphone', 30);- Inserts a product named 'Smartphone' with a quantity of 30 into the
productstable.
- Inserts a product named 'Smartphone' with a quantity of 30 into the
-
Insert a Product with Default Quantity:
INSERT INTO myshop.products (name) VALUES ('Headphones');- Inserts a product named 'Headphones' with the default quantity of 0 into the
productstable.
- Inserts a product named 'Headphones' with the default quantity of 0 into the
-
Insert a Category:
INSERT INTO myshop.categories (name) VALUES ('Electronics');- Inserts a category named 'Electronics' into the
categoriestable.
- Inserts a category named 'Electronics' into the
-
Insert Another Category:
INSERT INTO myshop.categories (name) VALUES ('Accessories');- Inserts a category named 'Accessories' into the
categoriestable.
- Inserts a category named 'Accessories' into the
-
Insert a Client:
INSERT INTO myshop.clients (name) VALUES ('Mykola');- Inserts a client named 'Mykola' into the
clientstable.
- Inserts a client named 'Mykola' into the
-
Insert Another Client:
INSERT INTO myshop.clients (name) VALUES ('Patron');- Inserts a client named 'Patron' into the
clientstable.
- Inserts a client named 'Patron' into the
-
Insert an Order Using a SELECT to Get Client ID:
INSERT INTO myshop.orders (order_id, client_id)
SELECT 1, client_id FROM myshop.clients WHERE name = 'Patron';SELECT: Retrieves data from a table.- Inserts an order with
order_id1 for the client named 'Patron' into theorderstable.
-
Insert an Order Using a Known Client ID:
INSERT INTO myshop.orders (order_id, client_id)
VALUES (2, 1);- Inserts an order with
order_id2 for the client withclient_id1 into theorderstable.
- Inserts an order with
-
Insert Product-Category Association Using a SELECT:
INSERT INTO myshop.product_category (product_id, category_id)
SELECT p.product_id, c.category_id
FROM myshop.products AS p, myshop.categories AS c
WHERE p.name = 'Laptop' AND c.name = 'Electronics';- Inserts an association between the product 'Laptop' and the category 'Electronics' into the
product_categorytable.
- Inserts an association between the product 'Laptop' and the category 'Electronics' into the
-
Insert Product-Category Association Using Known IDs:
INSERT INTO myshop.product_category (product_id, category_id) VALUES (2, 2);- Inserts an association between the product with
product_id2 and the category withcategory_id2 into theproduct_categorytable.
- Inserts an association between the product with
-
Insert Ordered Product Using a SELECT:
INSERT INTO myshop.ordered_products (product_id, order_id, quantity)
SELECT product_id, 1, 10 FROM myshop.products WHERE name = 'Smartphone';- Inserts an ordered product with
product_idfor 'Smartphone',order_id1, and quantity 10 into theordered_productstable.
- Inserts an ordered product with
-
Insert Ordered Product Using Known IDs:
INSERT INTO myshop.ordered_products (product_id, order_id, quantity)
VALUES (1, 1, 5);- Inserts an ordered product with
product_id1,order_id1, and quantity 5 into theordered_productstable.
- Inserts an ordered product with
-
Update Product Quantity:
UPDATE myshop.products SET quantity = quantity + 10 WHERE name = 'Laptop';UPDATE: Modifies existing rows in a table.- Increases the quantity of the product 'Laptop' by 10 in the
productstable.
-
Change Product Name:
UPDATE myshop.products SET name = 'Portable Speaker' WHERE name = 'Headphones';- Changes the name of the product 'Headphones' to 'Portable Speaker' in the
productstable.
- Changes the name of the product 'Headphones' to 'Portable Speaker' in the
-
Change Category Name:
UPDATE myshop.categories SET name = 'Electronic Devices' WHERE name = 'Electronics';- Changes the name of the category 'Electronics' to 'Electronic Devices' in the
categoriestable.
- Changes the name of the category 'Electronics' to 'Electronic Devices' in the
-
Update Client Name:
UPDATE myshop.clients SET name = 'Dog Patron' WHERE name = 'Patron';- Changes the name of the client 'Patron' to 'Dog Patron' in the
clientstable.
- Changes the name of the client 'Patron' to 'Dog Patron' in the
-
Change Client ID for an Order:
UPDATE myshop.orders SET client_id = (SELECT client_id FROM myshop.clients WHERE name = 'Mykola') WHERE order_id = 1;- Changes the
client_idof the order withorder_id1 to theclient_idof 'Mykola' in theorderstable.
- Changes the
-
Update Order Date:
UPDATE myshop.orders SET order_date = '2024-01-01' WHERE order_id = 2;- Changes the
order_dateof the order withorder_id2 to '2024-01-01' in theorderstable.
- Changes the
-
Change Category Associated with a Product:
UPDATE myshop.product_category
SET category_id = (SELECT category_id FROM myshop.categories WHERE name = 'Accessories')
WHERE product_id = (SELECT product_id FROM myshop.products WHERE name = 'Laptop');- Changes the
category_idassociated with the product 'Laptop' to thecategory_idof 'Accessories' in theproduct_categorytable.
- Changes the
-
Update Quantity of a Product in an Order:
UPDATE myshop.ordered_products SET quantity = 15 WHERE order_id = 1 AND product_id = (SELECT product_id FROM myshop.products WHERE name = 'Smartphone');- Updates the quantity to 15 for the product 'Smartphone' in the order with
order_id1 in theordered_productstable.
- Updates the quantity to 15 for the product 'Smartphone' in the order with
-
Link a Different Product to an Order:
UPDATE myshop.ordered_products
SET product_id = (SELECT product_id FROM myshop.products WHERE name = 'Portable Speaker')
WHERE order_id = 1 AND product_id = (SELECT product_id FROM myshop.products WHERE name = 'Laptop');- Changes the
product_idfrom 'Laptop' to 'Portable Speaker' in the order withorder_id1 in theordered_productstable.
- Changes the