Professional Documents
Culture Documents
desc students;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| rollno | varchar(20) | NO | PRI | | |
| lastname | varchar(20) | YES | | NULL | |
| firstname | varchar(20) | YES | | NULL | |
| email | varchar(20) | NO | PRI | | |
| classyear | int(10) | YES | | NULL | |
| major | varchar(20) | YES | | NULL | |
| phoneno | varchar(20) | NO | PRI | | |
+-----------+-------------+------+-----+---------+-------+
+--------+----------+-----------+---------+-----------+-------+------------+
| rollno | lastname | firstname | email | classyear | major | phoneno |
+--------+----------+-----------+---------+-----------+-------+------------+
| 101 | Kumar | Manish | a@b.com | 2012 | CSE | 9716982366 |
| 102 | A | K | a@k.com | 2012 | CSE | 9716982367 |
| 103 | B | G | b@g.com | 2014 | EEE | 9716982387 |
| 104 | s | K | s@k.com | 2013 | ME | 9988577206 |
+--------+----------+-----------+---------+-----------+-------+------------+
1. Create trigger t1 after insert on students for each row set @count=@count+1;
create trigger t1 after insert on students for each row set @count=(select COUNT(*) from
students);
OUTPUT:
select @count as 'TOTAL_COUNT';
+-------------+
| TOTAL_COUNT |
+-------------+
| 8 |
+-------------+
2.Create a trigger that displays a message prior to an insert operation on the students table
OUTPUT:
insert into students values("114","x","e","x@e.com",2013,"ECE","9781630440");|
ERROR 1644 (45000): INSERTING
OUTPUT:
select @message AS INFORMATION;|
+--------------------------------+
| INFORMATION |
+--------------------------------+
| INSERTING INTO STUDENTS TABLE |
+--------------------------------+
3. Create a Trigger that adds “+91” to all Phone numbers in the students table.Test and see if
the Trigger works properly by inserting and updating some data in the table.
create trigger t1 before insert on students for each row
-> begin
-> set new.phoneno = concat("+91",new.phoneno);
-> end; |
create trigger t2 before update on students for each row
-> begin
-> set new.phoneno = concat("+91",new.phoneno);
-> end; |
4. Modify the structure/create new by splitting the column name to ( First name and Last name)
and copy the values appropriately into it.
5. Create a trigger that whenever an insert, update, or delete operation occurs on the students
table, a row is added to the studentlog table recording the date, user, and action.
desc studentlog;|
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| date | date | YES | | NULL | |
| user | varchar(20) | YES | | NULL | |
| action | varchar(20) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
OUTPUT:
update students set rollno=200 where phone='9780630440';
insert into students values ("119","sa","df","sa@df.com",2016,"CSE","9785630440");|
delete from students where rollno= 115;|
select * from studentlog;
+------------+----------------+-----------+
| date | user | action |
+------------+----------------+-----------+
| 2017-02-28 | root@localhost | updating |
| 2017-02-28 | root@localhost | inserting |
| 2017-02-28 | root@localhost | deleting |
| 2017-02-28 | root@localhost | deleting |
+------------+----------------+-----------+
6. Create a trigger to insert student details into student table only if classyear<2015.
OUTPUT:
insert into students values ("118","fa","ff","fa@ff.com",2015,"CSE","9786630440");|
ERROR 1644 (45000): CLASS YEAR < 2015 REQUIRED
insert into students values ("118","fa","ff","fa@ff.com",2014,"CSE","9786630440");|
Query OK, 1 row affected (0.00 sec)
7. Create a trigger to prevent any student named John to be inserted into the table.
OUTPUT:
insert into students values ("118","John","ff","fa@ff.com",2015,"CSE","9786630440");|
ERROR 1644 (45000): John cannot be inserted
mysql> insert into students values ("118","fa","John","fa@ff.com",2015,"CSE","9786630440");|
ERROR 1644 (45000): John cannot be inserted
mysql> insert into students values ("118","fa","af","fa@af.com",2014,"CE","9786730440");|
Query OK, 1 row affected (0.00 sec)
OUTPUT:
update students set rollno='1' where phoneno='9780630440'; |
ERROR 1644 (45000): Rollno cannot be changed
9. Create a trigger when someone tried to insert a value into a students table values are
inserted into views
Can't update table 'txt' in stored function/trigger because it is already used by statement
which invoked this stored function/trigger.