Advertisement
Guest User

asfasfsafsafsa faslkdsak

a guest
May 5th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.20 KB | None | 0 0
  1. a.After insert in any table, count the number of rows and display the results as follows: “After successful insertion, number of rows of this table is now 23, where number 23 represent the number of rows in the table.
  2. TABLE: Committee, Audience, Band, Schedule, Registration, Stage, Interview, Magazine.
  3. ASSIGNED TO: Fa’iq Raihan
  4. -----------------------------------------------------------------------------------------------------------
  5.  
  6.  
  7. CREATE TRIGGER trg_CountRow_afterUpdate
  8. AFTER INSERT ON [Table] FOR EACH ROW
  9. DECLARE
  10. numofrow INT(10);
  11. BEGIN
  12.     Select count(*) FROM [Table] INTO numofrow
  13.     FROM [Table];
  14. dbms_output.put_line(After successful insertion, number of rows of this table is now  ‘ || numofrow);
  15. END;
  16.  
  17.  
  18.  
  19. b.  Create a trigger that will implement a referential integrity on a primary key table and the foreign key table in a way that if a value of PK is deleted it will nullify the value in the foreign key table as well.
  20. TABLE:                           
  21. ASSIGNED TO: Fa’iq Raihan
  22. -----------------------------------------------------------------------------------------------------------
  23.  
  24. CREATE TRIGGER trg_IntegrityCheck_onDelete
  25. AFTER DELETE ON [PrimaryTable] FOR EACH ROW
  26.  
  27. BEGIN
  28. DELETE FROM [ForeignTable]
  29.     WHERE ForeignTable.pid = PrimaryTable.id;
  30.  
  31. END;
  32.  
  33.  
  34.  
  35. c.  Create another trigger that would update the records of the PK table to the FK table as well. A proper response should be generated once the update is successful.
  36. TABLE:                       
  37. ASSIGNED TO: Fa’iq Raihan
  38. ---------------------------------------------------------------------------------------------------------------------
  39.  
  40. CREATE TRIGGER trg_ReferentialUpdate_afterUpdate
  41. AFTER UPDATE ON [PrimaryTable] FOR EACH ROW
  42.  
  43. BEGIN
  44.  
  45. UPDATE [ForeignTable]
  46. SET variable1 = (select variable1 FROM PrimaryTable WHERE ForeignTable.pid = PrimaryTable.id);
  47.  
  48. UPDATE [ForeignTable]
  49. SET variable2 = (select variable2 FROM PrimaryTable WHERE ForeignTable.pid = PrimaryTable.id);
  50.  
  51. UPDATE [ForeignTable]
  52. SET variable3 = (select variable3 FROM PrimaryTable WHERE ForeignTable.pid = PrimaryTable.id);
  53.  
  54. ...........
  55. dbms_output.put_line(Data from table [ForeignTable] have been updated following changes in table [PrimaryTable]);
  56.  
  57. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement