Advertisement
Gaudenz

PBA

Sep 19th, 2024 (edited)
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.40 KB | Cybersecurity | 0 0
  1. CREATE DATABASE business;
  2.  
  3. USE business;
  4.  
  5. -- STEP 1
  6. CREATE TABLE IF NOT EXISTS customers(
  7.     customer_id INT AUTO_INCREMENT PRIMARY KEY,
  8.     name VARCHAR(50) NOT NULL,
  9.     address VARCHAR(100)
  10. );
  11.  
  12. CREATE TABLE IF NOT EXISTS products(
  13.     product_id INT AUTO_INCREMENT PRIMARY KEY,
  14.     product_name VARCHAR(30),
  15.     category VARCHAR(30),
  16.     price DECIMAL(8,2),
  17.     stock INT
  18. );
  19.  
  20. CREATE TABLE IF NOT EXISTS orders(
  21.     order_id INT AUTO_INCREMENT PRIMARY KEY,
  22.     customer_id INT,
  23.     product_id INT,
  24.     order_date DATE,
  25.     quantity INT,
  26. -- STEP 2
  27.     CONSTRAINT fk_customers FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE CASCADE,
  28.     CONSTRAINT fk_products FOREIGN KEY (product_id) REFERENCES products(product_id) ON DELETE CASCADE
  29. );
  30.  
  31. -- STEP 3
  32. INSERT INTO customers (name, address)
  33. VALUES
  34.     ('credo', 'SSV Blk 45 lot 108, general trias'),
  35.     ('khier', 'Heneral Uno'),
  36.     ('santiago', 'Buenavista II'),
  37.     ('barron', 'Tiera Grande homes'),
  38.     ('azer','blk 19 Lot 32 KAIA homes');
  39.    
  40. INSERT INTO products (product_name, category, price, stock)
  41. VALUES
  42.     ('Milky white', 'Soap', 20.00, 60),
  43.     ('PenPenSis', 'School Supply', 15.25, 13),
  44.     ('Stand Fan', 'Home Appliance', 3000.00, 6),
  45.     ('Boy Bawang', 'Snack', 12.99, 300),
  46.     ('Piatos', 'Snack', 15.00, 200);
  47.    
  48. INSERT INTO orders (customer_id, product_id, order_date, quantity)
  49. VALUES
  50.     (1, 3, '2024-09-18', 2),
  51.     (2, 2, '2024-04-19', 4),
  52.     (3, 4, '2024-05-10', 40),
  53.     (4, 1, '2024-01-11', 1),
  54.     (5, 5, '2024-02-24', 2);
  55.  
  56. -- UPDATE STEP 4
  57. UPDATE products
  58. SET price = 35.00
  59. WHERE product_id = 1;
  60.  
  61. UPDATE customers
  62. SET address = 'Natania Homes PK2'
  63. WHERE customer_id = 3;
  64.  
  65.  
  66. -- STEP 5 DELETE
  67. ALTER TABLE orders
  68. DROP FOREIGN KEY fk_products;
  69.  
  70. DELETE FROM products WHERE product_id = 3;
  71.  
  72. ALTER TABLE orders
  73. ADD CONSTRAINT fk_products FOREIGN KEY (product_id) REFERENCES products(product_id) ON DELETE CASCADE;
  74.  
  75. -- STEP 6 generate a report
  76. SELECT
  77.     customers.name,
  78.     customers.address,
  79.     products.product_name,
  80.     products.category,
  81.     products.price,
  82.     products.stock,
  83.     orders.order_date
  84. FROM orders
  85. JOIN customers ON orders.customer_id = customers.customer_id
  86. JOIN products ON orders.product_id = products.product_id;
  87.  
  88.  
  89. -- STEP 7 display the total sales revenue
  90. SELECT SUM(products.price * orders.quantity) AS 'Total Revenue'
  91. FROM orders
  92. JOIN products ON orders.product_id = products.product_id;
  93.  
  94.  
Tags: sql
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement