NoNameCracksInc

SQL trigger

Jan 16th, 2013
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. Hey guys.So today I prepared kinda easier script.It's trigger connecting main table(test2) with error table test3 and inserting table test3 :).It may look difficult but its pretty simple script.
  2. DESCRIBTION IS UNDER TRIGGER DOWN THERE :-)
  3.  
  4. TABLES:
  5.  
  6. create table test1(
  7. input decimal(10,0) not null
  8. );
  9. create table test2(
  10. A varchar(50) not null,
  11. B varchar(50) not null,
  12. STATUS varchar(50) not null,
  13. TIME datetime not null
  14. );
  15.  
  16. create table test3(
  17. ERROR_TYPE varchar(20),
  18. TIME datetime not null,
  19. USER varchar(20) not null
  20. );
  21.  
  22. SCRIPT:
  23.  
  24.  
  25. DELIMITER $$
  26. DROP TRIGGER IF EXISTS restrictive_trigger;
  27. CREATE TRIGGER restrictive_trigger
  28. AFTER INSERT ON test1
  29. FOR EACH ROW
  30. BEGIN
  31. IF NEW.input <> 1000
  32. AND
  33. IF (NEW.input > 1000, 1,0)=0
  34. AND
  35. IF (NEW.input < 1000, 1,0)=0
  36. THEN
  37. INSERT INTO test2() VALUES
  38. ('EMPTY','EMPTY','NOT_ALLOWED',NOW());
  39. END IF;
  40. IF NEW.input IN
  41. ('q','w','e','r','t','y','u','i','o','p','l','k','j','h','g','f','d','s','a','z','x','c','v','b','n','m')
  42. THEN
  43. INSERT INTO test2() VALUES
  44. ('EMPTY','EMPTY','EMPTY','EMPTY');
  45. END IF;
  46. IF NEW.input = '0'
  47. THEN
  48. DELETE FROM test2 WHERE A LIKE 'EMPTY';
  49. END IF;
  50. IF NEW.input = '0'
  51. THEN
  52. INSERT INTO test3() VALUES
  53. ('LETTER_INSERTED',NOW(), USER());
  54. END IF;
  55. IF NEW.input > 1000
  56. THEN
  57. INSERT INTO test2() VALUES
  58. ('MORE_THAN_THOUSAND',new.input,'OKAY',NOW());
  59. END IF;
  60. IF NEW.input = 1000
  61. THEN
  62. INSERT INTO test2() VALUES
  63. ('ONE_THOUSAND',NEW.input,'OKAY',NOW());
  64. END IF;
  65. IF NEW.input < 1000
  66. AND
  67. IF (NEW.input <> 0, 1,0)=1
  68. THEN
  69. INSERT INTO test2() VALUES
  70. ('LESS_THAN_THOUSAND',NEW.input,'OKAY',NOW());
  71. END IF;
  72. END;
  73. $$
  74.  
  75. Table test1 is used just for inserting characters, test2 to see informations about characters inserted and it also deletes wrong characters (all letters and number 0) from itself.Test3 shows you informations: who and when inserted wrong character into table2.
  76. Our table test2 is based on not-accepting any letters or number 0.
  77. So if you enter any number (Except 0) into test1 table, in test2 appear informations about number and when the number was inserted.Test3 is basically error table and entering any number except 0 into test1 table will not call error.If you enter any letter or number 0 into test1, in test2 will not appear any information, and in test3 will appear error message.
Advertisement
Add Comment
Please, Sign In to add comment