Przejdź do głównej zawartości

SQL - Trigger - wyzwalacz - SQL podstawy

Wyzwalacz (ang. trigger) – procedura wykonywana automatycznie jako reakcja na pewne zdarzenia w tabeli bazy danych. Wyzwalacze mogą ograniczać dostęp do pewnych danych, rejestrować zmiany danych lub nadzorować modyfikacje danych



CREATE TABLE trigger_test(
message VARCHAR (100)
);


DELIMITER $$
CREATE
      TRIGGER my_trigger BEFORE INSERT
      ON employee
      FOR EACH ROW BEGIN
         INSERT INTO trigger_test VALUES ('added new employee');
      END $$
DELIMITER;

Before anything new item will be added to employee table than insert into trigger value "Added new employee" 

DELIMITER change ; into $$ because ; need to be use in TRIGGER what cause end of function



DELIMITER $$
CREATE
    TRIGGER my_trigger1 BEFORE INSERT 
    ON employee
    FOR EACH ROW BEGIN
        IF NEW.sex='M' THEN
            INSERT INTO trigger_test VALUES ('added male employee');
        ELSEIF NEW.sex = 'F' THEN
            INSERT INTO trigger_test VALUES ('added female');
        ELSE
            INSERT INTO trigger_test VALUES ('added other employee');
        END IF;    
    END $$
DELIMITER;   

INSERT INTO employee
VALUES(179, 'Oscar', 'Test', '1968-02-11', 'F', 6900, 106, 3);
INSERT INTO employee
VALUES(180, 'Oscar', 'Bob', '1968-02-11', 'M', 6900, 106, 3);

SELECT * FROM trigger_test;  

http://www.mysqltutorial.org/create-the-first-trigger-in-mysql.aspx

SHOW TRIGGERS;

Komentarze

Popularne posty z tego bloga

Stored Procedures - JDBC Java SQL

Stored Procedures: group of SQL statements that perform a particular task  Normally created by DBA Can have any combination of input, output, and input/output parameters Benefits: Performance  - compiled once but executable more times Productivity and Ease of Use  - avoid redundant code, extend SQL Database functionality  Scalability   Maintainability Interoperability Replication Security To call stored procedure from Java The JDBC API provides the CallableStatement CallableStatement myCall = myConn.prepareCall("{ call some_stored_procedure() }"); ... myCall.execute(); JDBC API parameter types IN default INOUT OUT Stored Procedure can return result sets EXAMPLE: Created stored procedure on MySQL side - NAME:  increase_salaries_for_department DELIMITER $$ DROP PROCEDURE IF EXISTS ` increase_salaries_for_department `$$ CREATE DEFINER=`student`@`localhost` PROCEDURE `increase_salaries_for_departme...

Java - variables , data types

Data Types Data types in Java are classified in to types: Primitive - which include Integer, Character, Boolean and Floating Point Non-primitive - which includes Classes, Interfaces and Arrays Integer : byte 1 byte short 2 bytes int 4 bytes long 8 bytes Floating Point : number + fractional parts  float double Character : stores character Boolean : hold true false value Variables  Instance Variables (non-Static Fields) -  Objects store their individual states in “non-static fields”, that is, fields declared without the static keyword. Class Variables - Static Fields - any field with static modifier says that there is only one copy of this variable in existence static int myClassVariable = 6;  Local Variables - a method stores it's temporary state of local variables int count=0; Parameters - variables which will be passed to the methods of a class

Skrócony zapis if - instrukcja warunkowa java

Instrukcja warunkowa - warunek i rezultat. if (warunek) { jesli spełniony wykonań operacje i zwróć wynik; } warunek nie spełniony Możliwości skrócenia kodu instrukcji warunkowej if (i < 0) ? i-- : i++; Jeżeli i mniejsze od zera to i-- jezeli false to i++ if (i < 0) {     i--; } else {     i++; } Skrócony zapis instrukcji warunkowej else if (i < 0) ? i--;  inna_zmienna=4; : i++; if (i < 0) {     i--; } else {     i++;     inna_zmienna = 4; } Skrócony zapis if