NoNameCracksInc icon

SQL trigger

NoNameCracksInc | PRO | 01/16/13 04:31:28 PM UTC | 0 ⭐ | 406 👁️ | Never ⏰ | []
text |

2.27 KB

|

None

|

0 👍

/

0 👎

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.
DESCRIBTION IS UNDER TRIGGER DOWN THERE :-)
 TABLES:
 create table test1(
input decimal(10,0) not null
);
create table test2(
A varchar(50) not null,
B varchar(50) not null,
STATUS varchar(50) not null,
TIME datetime not null
);
 create table test3(
ERROR_TYPE varchar(20),
TIME datetime not null,
USER varchar(20) not null
);
 SCRIPT:
  DELIMITER $$
DROP TRIGGER IF EXISTS restrictive_trigger;
CREATE TRIGGER restrictive_trigger
AFTER INSERT ON test1
FOR EACH ROW
BEGIN
IF NEW.input <> 1000
AND
IF (NEW.input > 1000, 1,0)=0
AND
IF (NEW.input < 1000, 1,0)=0
THEN
INSERT INTO test2() VALUES 
('EMPTY','EMPTY','NOT_ALLOWED',NOW());
END IF;
IF NEW.input IN 
('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')
THEN
INSERT INTO test2() VALUES 
('EMPTY','EMPTY','EMPTY','EMPTY');
END IF;
IF NEW.input = '0'
THEN
DELETE FROM test2 WHERE A LIKE 'EMPTY';
END IF;
IF NEW.input = '0'
THEN
INSERT INTO test3() VALUES 
('LETTER_INSERTED',NOW(), USER());
END IF;
IF NEW.input > 1000 
THEN
INSERT INTO test2() VALUES
('MORE_THAN_THOUSAND',new.input,'OKAY',NOW());
END IF;
IF NEW.input = 1000
THEN
INSERT INTO test2() VALUES 
('ONE_THOUSAND',NEW.input,'OKAY',NOW());
END IF;
IF NEW.input < 1000
AND
IF (NEW.input <> 0, 1,0)=1
THEN
INSERT INTO test2() VALUES 
('LESS_THAN_THOUSAND',NEW.input,'OKAY',NOW());
END IF;
END;
$$
 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.
Our table test2 is based on not-accepting any letters or number 0.
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.

Comments