Triggers
Triggers
Types of Triggers –
We can define 6 types of triggers for each table:
1. AFTER INSERT: activated after data is inserted into the table.
2. AFTER UPDATE: activated after data in the table is modified.
3. AFTER DELETE: activated after data is deleted/removed from the
table.
4. BEFORE INSERT: activated before data is inserted into the table.
5. BEFORE UPDATE: activated before data in the table is modified.
6. BEFORE DELETE: activated before data is deleted/removed from the
table.
Syntax:
Example1:
delimiter $$
CREATE TRIGGER Check_age BEFORE INSERT ON employee
FOR EACH ROW
BEGIN
IF NEW. age < 25 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'ERROR:
AGE MUST BE ATLEAST 25 YEARS!';
END IF;
END; $$
delimiter;
Views in SQL
Views in SQL are kind of virtual tables. A view also has rows and columns
as they are in a real table in the database. We can create a view by selecting
fields from one or more tables present in the database. A View can either
have all the rows of a table or specific rows based on certain condition.
Now, we will learn about creating , deleting and updating Views.
Sample Tables:
StudentDetails
StudentMarks
CREATING VIEWS
Syntax:
Examples:
● Creating View from a single table:
●
● In this example we will create a View named DetailsView from the
table StudentDetails.
Query:
● CREATE VIEW DetailsView AS
UPDATING VIEWS
There are certain conditions needed to be satisfied to update a view. If any
one of these conditions is not met, then we will not be allowed to update the
view.
1. The SELECT statement which is used to create the view should not
include GROUP BY clause or ORDER BY clause.
2. The SELECT statement should not have the DISTINCT keyword.
3. The View should have all NOT NULL values.
4. The view should not be created using nested queries or complex queries.
5. The view should be created from a single table. If the view is created
using multiple tables then we will not be allowed to update the view.
● We can use the CREATE OR REPLACE VIEW statement to add or
remove fields from a view.
Syntax:
● CREATE OR REPLACE VIEW view_name AS
● SELECT column1,coulmn2,..
● FROM table_name
● WHERE condition;
For example, if we want to update the view MarksView and add the
field AGE to this View from StudentMarks Table, we can do this as:
Syntax:
● INSERT INTO view_name(column1, column2 , column3,..)
● VALUES(value1, value2, value3..);
Example:
In the below example we will insert a new row in the View DetailsView
which we have created above in the example of “creating views from a
single table”.
●
● Deleting a row from a View:
Deleting rows from a view is also as simple as deleting rows from a
table. We can use the DELETE statement of SQL to delete rows from a
view. Also deleting a row from a view first delete the row from the
actual table and the change is then reflected in the view.Syntax:
● DELETE FROM view_name
● WHERE condition;
●
● view_name:Name of view from where we want to delete rows
● condition: Condition to select rows
Example:
In this example we will delete the last row from the view DetailsView
which we just added in the above example of inserting rows.
DELETE FROM DetailsView
WHERE NAME="Suresh";
If we fetch all the data from DetailsView now as,
SELECT * FROM DetailsView;
Output:
For example,though the View is updatable but then also the below query for
this View is not valid:
INSERT INTO SampleView(S_ID)
VALUES(6);
NOTE: The default value of NAME column is null.
Uses of a View :