ASSIGNMENT 2 Dbms
ASSIGNMENT 2 Dbms
questions:
1. Find the names of customers who have purchased no item. Set the default value of
Cust_balance as 0 for such customers.
UPDATE CUSTOMER
SET cust_balance = 0
WHERE cust_num NOT IN (
SELECT cust_num FROM INVOICE
);
2. Write a trigger to update the CUST_BALANCE in the CUSTOMER table when a new invoice
record is entered for the customer.
CREATE TRIGGER update_cust_balance
AFTER INSERT ON INVOICE
FOR EACH ROW
BEGIN
UPDATE CUSTOMER
SET cust_balance = cust_balance - NEW.inv_amount
WHERE cust_num = NEW.cust_num;
END;
3. Find the customers who have purchased more than three units of a product on a day.\
SELECT c.cust_fname, c.cust_lname, i.inv_date, i.unit_sold
FROM CUSTOMER c
JOIN INVOICE i ON c.cust_num = i.cust_num
WHERE i.unit_sold > 3;
4. Write a query to illustrate Left Outer, Right Outer, and Full Outer Join.
Left Outer Join:
SELECT c.cust_fname, i.inv_num
FROM CUSTOMER c
LEFT JOIN INVOICE i ON c.cust_num = i.cust_num;
UNION
6. As soon as customer balance becomes greater than Rs. 100,000, copy the customer_num in
a new table called GOLD_CUSTOMER.
Create GOLD_CUSTOMER table:
CREATE TABLE GOLD_CUSTOMER (
cust_num INT PRIMARY KEY
);
Create trigger:
CREATE TRIGGER check_gold_customer
AFTER UPDATE ON CUSTOMER
FOR EACH ROW
BEGIN
IF NEW.cust_balance > 100000 THEN
INSERT INTO GOLD_CUSTOMER (cust_num)
VALUES (NEW.cust_num)
ON DUPLICATE KEY UPDATE cust_num = NEW.cust_num;
END IF;
END;