Triger Lab
Triger Lab
Databases Management
Semester-IV
2023-24
Lab-6
28 th March 2024
Objectives:
Triggers
Views
TRIGGERS
A trigger is a special type of stored procedure that automatically runs when an event occurs in the database.
Create a TRIGGER TO UPDATE THE INSURANCE COLUMN WHEN THE PRICE OF THE VEHICLE IS PROVIDED.
Write a trigger which updates the insurance if the Price of the vehicle is updated.
Delete table cars; //Clear all the rows in the Table Car.
Create a Trigger to insert into the Table C_Maintenance when Table Car is inserted.
CREATE TRIGGER INSERT_MAINTENANCE
AFTER INSERT
ON CARS
FOR EACH ROW
INSERT INTO C_MAINTENANCE (CAR_MID,MAKE,MODEL,MYEAR,COLOR,INSURANCE)
VALUES(NEW.CAR_ID,NEW.MAKE,NEW.MODEL,NEW.MYEAR,NEW.COLOR,NEW.INSURANCE);
Delete Trigger: Write a trigger to delete column in C_maintenance if the row is deleted in cars.
CREATE TRIGGER DEL_MAINTENANCE Write your observation here
AFTER DELETE
ON CARS Now the cars and maintenance table is triggered for
FOR EACH ROW deletion. If any row of cars is deleted the corresponding
DELETE FROM C_MAINTENANCE WHERE row that is mid of maintenance will be deleted. So there
CAR_MID=OLD.CAR_ID; is no mis match between the tables.
SELECT * FROM CARS; Here the car id 9 is deleted, so the corresponding row of
SELECT * FROM C_MAINTENANCE; Car maintenance table is also get deleted because of the
del_maintenance trigger. So finally, the table will only
show the remaining ids rows and not the row with Car id
= 9.
What is a View?
Syntax
1 Create a view named ‘car_view’ to view the details of create view car_view as
car_id, make, model from the table ‘cars’ select car_id, make, model from cars;
Display the values of the created view ‘car_view’ select * from car_view;
2 Create a view named ‘car_maintenance’ to view the details create view car_maintenance as
of car_mid, make, myear, insurance from the table select car_mid, make, myear, insurance from
‘c_maintenance’ c_maintenance;
Display the values of the created view ‘car_maintenance’ select * from car_maintenance;
Display the values of the created view ‘HighMileageCars’ select * from HighMileageCars;
Inserting the values, Deleting the values, Modifying the values, Altering the View and Dropping the View.
Updating the values of the view ‘car_view’ update car_view set make='Century' where car_id=1;
WHAT WILL HAPPEN TO THE DATA OF MAIN TABLE ‘CARS’ WHEN VIEW ‘CAR_VIEW’ was Updated?
Display the original table from which View select * from cars;
Table was made
UPDATE operation on a view, might indirectly affect the underlying tables depending on various factors:
Simple Views: If the view is based on a single table updating the view might directly modify the underlying table
Complex Views: If the view is based on multiple tables or contains aggregate functions, joins or other complex
operations, updates will not be entertained, if entertained then it do not reflect on the underlying tables
Complex Views: Views involving two or more tables from a Database:
1 Create a view named ‘CarView1’ with car_id, Mileage, create view CarView1 as
MYear, and Insurance details from ‘Cars’ and select c.car_id, c.mileage, m.myear, m.insurance
‘C_Maintenance’ Tables from cars c, c_maintenance m
where c.car_id=m.car_mid;
2 Create a view called ‘CombinedCar’ which provides details create view CombinedCar as
of Make, Model, Myear and Insurance cost by combining select
both ‘Cars’ and C_Maintenance Tables c.MAKE AS CarMake,
c.MODEL AS CarModel,
m.MYEAR AS MaintenanceYear,
m.INSURANCE AS MaintenanceInsurance
from
Cars c
JOIN
C_Maintenance m ON c.Make = m.Make AND
c.Model = m.Model AND c.MYear = m.MYear;
insert into CarView1(car_id, mileage, MYEAR, insurance) What is your Observation while trying to Insert values
values (19, 21, 2023, 14500); to this complex view ‘CarView1’?
2. What will happen to the Data of the View Table Write down your Observation.
when the contents of the Underlying Table /
Main Table are Updated or Inserted?
3. What will happen to the Structure of the Write down your Observation.
Underlying Tables when the Structure of Simple
View is Altered using Alter Command?
4. What will happen to the Structure of the Simple Write down your Observation.
View when the Structure of its Underlying Table
is Altered using Alter Command?
A view can be updated under certain conditions which are given below: