Advertisement
weighter

Assignment 4 ch 8-9-10-11

Dec 13th, 2021
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 5.51 KB | None | 0 0
  1. -- Assignment 4
  2.  
  3. -- Chapter 8
  4. -- ===============================================================================================
  5. use my_guitar_shop;
  6.  
  7. -- Question 1
  8.  
  9. SELECT FORMAT(list_price,1),
  10.        CONVERT(list_price, SIGNED ),
  11.        CAST(list_price AS SIGNED)
  12. FROM products;
  13.  
  14. -- Question 2
  15. SELECT date_added,
  16.        CAST(date_added AS DATE),
  17.        CAST(date_added AS CHAR(7)),
  18.        CAST(date_added AS TIME)
  19. FROM products;
  20.  
  21.  
  22.  
  23.  
  24.  
  25. use my_guitar_shop;
  26.  
  27. -- Chapter 9
  28. -- ===============================================================================================
  29.  
  30. -- Question 1
  31.  
  32. SELECT list_price,
  33.        discount_percent,
  34.        ROUND((list_price*(discount_percent/100)), 2) AS discount_amount
  35. FROM products;
  36.  
  37.  
  38. -- Question 2
  39.  
  40. SELECT order_date,
  41.        DATE_FORMAT(order_date, '%Y') AS order_year,
  42.        DATE_FORMAT(order_date, '%b-%d-%Y') AS order_date_formatted,
  43.        DATE_FORMAT(order_date, '%l:%i %p') AS order_time,
  44.        DATE_FORMAT(order_date, '%m/%d/%y %h:%i') AS order_datetime
  45. FROM orders;
  46.  
  47. -- Question 3
  48.  
  49. SELECT card_number,
  50.        LENGTH(card_number) AS card_number_length,
  51.        RIGHT(card_number, 4) AS last_4_digits,
  52.        IF(LENGTH(card_number) = 16, CONCAT('XXXX-XXXX-XXXX', RIGHT(card_number, 4)), CONCAT('XXXX-XXXXXX-X', RIGHT(card_number, 4)))
  53. FROM orders;
  54.  
  55. -- Question 4
  56.  
  57. SELECT order_id,
  58.        order_date,
  59.        DATE_ADD(order_date, INTERVAL 2 DAY) AS approx_ship_date,
  60.        ship_date,
  61.        DATEDIFF(ship_date, order_date) AS days_to_ship
  62. FROM orders
  63. WHERE ship_date IS NOT NULL
  64. AND DATE_FORMAT(order_date, '%Y-%m') = '2018-03';
  65.  
  66.  
  67.  
  68. -- Chapter 11
  69.  
  70. -- Question 1
  71.  
  72. use my_guitar_shop;
  73.  
  74. CREATE INDEX index_zipcode ON addresses(zip_code);
  75.  
  76. -- Question 2
  77.  
  78. DROP DATABASE IF EXISTS my_web_db;
  79. CREATE DATABASE IF NOT EXISTS my_web_db;
  80. USE my_web_db;
  81.  
  82.  
  83. CREATE TABLE IF NOT EXISTS users
  84. (
  85. user_id int Primary key NOT NULL AUTO_INCREMENT,
  86. email_address varchar(100),
  87. first_name varchar(45),
  88. last_name varchar(45)
  89. );
  90.  
  91. CREATE TABLE IF NOT EXISTS products
  92. (
  93. product_id int Primary key NOT NULL AUTO_INCREMENT,
  94. product_name varchar(20)
  95. );
  96.  
  97. CREATE TABLE IF NOT EXISTS downloads
  98. (
  99. download_id int Primary key NOT NULL AUTO_INCREMENT,
  100. user_id int REFERENCES users(user_id),
  101. download_date date,
  102. filename varchar(50),
  103. product_id int REFERENCES products(product_id)
  104. );
  105.  
  106. -- Question 3
  107.  
  108. INSERT INTO users (email_address, first_name, last_name) values ('johnsmith@gmail.com', 'John', 'Smith');
  109. INSERT INTO users (email_address, first_name, last_name) values ('janedoe@yahoo.com', 'Jane', 'Doe');
  110. INSERT INTO products (product_name) VALUES ('Local Music Vol 1');
  111. INSERT INTO products (product_name) VALUES ('Local Music Vol 2');
  112.  
  113. INSERT INTO downloads (user_id, download_date, filename, product_id) VALUES (1, '2019-03-04', 'pedals_falling.mp3', 2);
  114. INSERT INTO downloads (user_id, download_date, filename, product_id) VALUES (2, '2019-03-05', 'turn_signal.mp3', 1);
  115. INSERT INTO downloads (user_id, download_date, filename, product_id) VALUES (2, '2019-03-06', 'one_horse_town.mp3', 2);
  116.  
  117. SELECT email_address,
  118.        first_name,
  119.        last_name,
  120.        download_date,
  121.        filename,
  122.        product_name
  123. FROM users
  124. JOIN downloads d on users.user_id = d.user_id
  125. JOIN products p on d.product_id = p.product_id;
  126.  
  127.  
  128. -- Question 4
  129.  
  130. ALTER TABLE products
  131. ADD product_price DECIMAL(3,2) DEFAULT 9.99,
  132. ADD date_added DATETIME;
  133.  
  134. -- Question 5
  135.  
  136. ALTER TABLE users
  137. MODIFY first_name VARCHAR(20) NOT NULL;
  138.  
  139. UPDATE users
  140. SET first_name = NULL
  141. WHERE  user_id = 1;
  142.  
  143. UPDATE users
  144. SET first_name = 'thisIsNotGoingToWorkBecauseItIsTooLong'
  145. WHERE  user_id = 1;
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153. USE my_guitar_shop;
  154.  
  155. -- ===============================================================================
  156. -- Chapter 12
  157.  
  158. -- Question 1
  159.  
  160. CREATE OR REPLACE VIEW customer_addresses AS
  161. SELECT c.customer_id, email_address, last_name, first_name,
  162.        bill.line1 AS bill_line1,
  163.        bill.line2 AS bill_line2,
  164.        bill.city AS bill_city,
  165.        bill.state AS bill_state,
  166.        bill.zip_code AS bill_zip,
  167.        ship.line1 AS ship_line1,
  168.        ship.line2 AS ship_line2,
  169.        ship.city AS ship_city,
  170.        ship.state AS ship_state,
  171.        ship.zip_code AS ship_zip
  172. FROM customers c
  173.     JOIN addresses bill ON c.customer_id = bill.customer_id AND c.billing_address_id = bill.address_id
  174.     JOIN addresses ship ON c.customer_id = ship.customer_id AND c.shipping_address_id = ship.address_id;
  175.  
  176.  
  177. -- Question 2
  178.  
  179. SELECT customer_id,
  180.        last_name,
  181.        first_name,
  182.        bill_line1
  183. FROM customer_addresses;
  184.  
  185. -- Question 3
  186.  
  187. UPDATE customer_addresses
  188. SET ship_line1 = '1990 Westwood Blvd.'
  189. WHERE customer_id = 8;
  190.  
  191. -- Question 4
  192.  
  193. CREATE OR REPLACE VIEW order_item_products AS
  194. SELECT o.order_id,
  195.        o.order_date,
  196.        o.tax_amount,
  197.        o.ship_date,
  198.        p.product_name,
  199.        oi.item_price,
  200.        oi.discount_amount,
  201.        (oi.item_price - oi.discount_amount) AS final_price,
  202.        oi.quantity,
  203.        (oi.item_price - oi.discount_amount)*quantity AS item_total
  204. FROM orders o
  205. JOIN order_items oi on o.order_id = oi.order_id
  206. JOIN products p on p.product_id = oi.product_id;
  207.  
  208.  
  209. -- CREATE OR REPLACE VIEW full_monty AS
  210. SELECT c.customer_id,
  211.        c.email_address,
  212.        c.last_name,
  213.        ba.line1 as bill_line1,
  214.        ba.line2 as bill_line2,
  215.        sa.line1 as shipp_line1,
  216.        sa.line2 as shipp_line2
  217. FROM customers c
  218.     JOIN addresses ba ON c.billing_address_id  = ba.address_id
  219.     JOIN addresses sa ON c.shipping_address_id = sa.address_id;
  220.  
  221.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement