Demo Day - 9
Demo Day - 9
Demo Day - 9
-------------------
triggers are special type of procedures that are called by system automatically.
insert
delete
update
after
before
insert ...before
operators ---new
old
before insert
-------------
delimiter //
create trigger check_sal
before insert
on empdemo
for each row
begin
if new.sal is null
then
set new.sal=10000;
end if;
end //
delimiter ;
product(pid,pname,price,qty)
product_log(pid,old_amt,new_amt)
amt =price * qty
Write a trigger which will calculate amt every time any update is done in the price
of an item.
delimiter //
after insert
------------
order_master(oid,pname,qyt,price);
order_log(oid,ord_date,amt)
delimiter //
create trigger demo_trigger1
after insert
on order_master
for each row
begin
declare a decimal(7,2);
set a= new.qty*new.price;
insert into order_log
values(new.oid,curdate(),a);
end //
delimiter ;
----------------------------------
before delete
delimiter //
create trigger trigger_demo2
before delete
on empdemo
for each row
begin
insert into emp_log
values(old.empno,old.ename,old.job);
end //
delimiter ;
delete
from empdemo
where empno=1234;
--------------------------
before update
emp_old_data(empno,job,sal,pro_date)
create table emp_old_data
(
empno int,
job varchar(12),
sal decimal(7,2),
pro_date date
);
delimiter //
create trigger trigger_demo3
before update
on empdemo
for each row
begin
insert into emp_old_data values(old.empno,old.job,old.sal,curdate(),new.sal);
end //
delimiter ;
update empdemo
set sal=2000
where ename='allen';
---------------------------------
normalization
-is implemented to control data redundancy
9 forms of normalization
-4NF
1NF
there can be only 1 entry in the column of table.
i.e no multiple entries in column of the table
student
sname course marks fees
------------------
amit c,RDBMS 90,78 5000,10000
sheetal wp,linux
rajesh Linux
solution 1
student pk=sid
-------
sid sname course1 course2
1 amit c rdbms
2 sheetal wp linux
3 ajay java null
solution 3 PK=sid+course
solution 3
student
sid sname
----------------------
s1 amit
course
cid cname fees
----------------
c1 c 5000
c2 wp 20000
c3 advjava 15000
studet_course pk=id
--------------
id sid cid
1 s1 c1
2 s1 c3
3 s3 c1
(A+B)----C
C should depend fully on A+B
and not on either A or B
customer(cid,cname,ph_no,reg_no,)pk=cid
store(store_id,sname,loc,add) pk=store_id
cust_store(cid,sid) pk=cid+sid
emp(empno,ename)
dept(deptno,dname)
person(ppno,pid,name,submit_date,ah_no)
ppno=fk,pid=pk dependent
passport(ppno,name,add,date,exp_date)
ppno=pk base table
or
person(pid,name,submit_date,ah_no)
pid=pk base table
passport(pid,ppno,name,add,date,exp_date)
pid=fk
ppno=pk dependent table
book_mem
--------
bkid mid
ER-Diagram
----------
entity relationship diagram