Practice Exercise - Views - Types of Views - MySQL #24
Replies: 1 comment
-
Solution ApproachUpdatable View: First, we create the view. CREATE VIEW customer_view AS
SELECT customerNumber, customerName, contactLastName, contactFirstName
FROM customers;Then, to update a specific record, we do something like: UPDATE customer_view
SET contactFirstName = 'NewFirstName'
WHERE customerNumber = some_number;Read-Only View: Here's how you can create a read-only view. CREATE VIEW order_product_view AS
SELECT od.orderNumber, p.productName, od.quantityOrdered
FROM orderdetails od
JOIN products p ON od.productCode = p.productCode;If you try to update this view, you'll get an error because it involves multiple base tables. Inline View: Here's a query with an inline view. SELECT iv.customerNumber, COUNT(iv.orderNumber) as total_orders
FROM (
SELECT customerNumber, orderNumber
FROM orders
) as iv
GROUP BY iv.customerNumber;Materialized View: You'd need to create a stored procedure and a trigger. Here's the stored procedure to create the table: CREATE PROCEDURE refresh_materialized_view()
BEGIN
DROP TABLE IF EXISTS materialized_view;
CREATE TABLE materialized_view AS
SELECT p.productName, SUM(od.quantityOrdered) as totalQuantityOrdered
FROM orderdetails od
JOIN products p ON od.productCode = p.productCode
GROUP BY p.productName;
END;Here's the trigger to refresh the "materialized view" after each insert: CREATE TRIGGER orderdetails_after_insert
AFTER INSERT ON orderdetails
FOR EACH ROW
CALL refresh_materialized_view();Every time a row is inserted into Remember to adjust these SQL statements according to the rules and standards in your database schema. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Updatable View:
customerNumber,customerName,contactLastName, andcontactFirstNamefrom thecustomerstable. Then, try to update thecontactFirstNamefor a specificcustomerNumber.Read-Only View:
orderdetailstable and theproductstable onproductCodeand includesorderNumber,productName, andquantityOrdered. Try to update thequantityOrderedfor a specificorderNumberand see what happens.Inline View:
customerNumberandorderNumberfrom theorderstable. The main query should then group bycustomerNumber.Materialized View:
productNameandtotalQuantityOrdered(this total should be aggregated from theorderdetailstable). Then, create anAFTER INSERTtrigger on theorderdetailstable that calls this stored procedure to update the table (acting as a materialized view) whenever a new order detail is inserted.Remember, these exercises are for practice purposes. The structure of the database and business requirements would dictate whether these views are needed or if they would be created in this exact way. Also, always be careful when updating data in a database. If this is a live database or contains important data, you should backup the database before attempting to update or delete data.
Beta Was this translation helpful? Give feedback.
All reactions