×
          
              
          
      
      Clear all filters including search bar
          
        Valeri Tandilashvili's Personal Professional Blog
      
    note and adds several indexes (ind_note_id, ind_title_description) at the same timeCREATE TABLE note (
    id int AUTO_INCREMENT,
    note_id int,
    title varchar(50),
    description varchar(50),
    PRIMARY KEY(id, note_id),
    INDEX ind_note_id (note_id),
    INDEX ind_title_description (title, description)
);INDEX with several fields for table notes.
Combines the two fields title, description to create one indexCREATE TABLE note (
    id int AUTO_INCREMENT,
    note_id int,
    title varchar(50),
    description varchar(50),
    PRIMARY KEY(id, note_id),
    INDEX ind_title_description (title, description)
);
If we need to filter the result with both fields (title, description) at the same time, then ind_title_description is a great indexDROP PROCEDURE IF EXISTS printIncomeLevel;
DELIMITER //
CREATE PROCEDURE printIncomeLevel (IN monthly_value INT)
BEGIN
   DECLARE income_level varchar(20);
   IF monthly_value <= 4000 THEN
        SET income_level = 'Low Income';
   ELSEIF monthly_value > 4000 AND monthly_value <= 7000 THEN
        SET income_level = 'Avg Income';
   ELSE
        SET income_level = 'High Income';
   END IF;
   INSERT INTO log (description)
   VALUES ('getIncomeLevel procedure was called');
   SELECT income_level;
END;
After creating the procedure, we can call it using CALL keyword:CALL printIncomeLevel(450)DROP FUNCTION IF EXISTS getIncomeLevel;
DELIMITER //
CREATE FUNCTION getIncomeLevel ( monthly_value INT )
RETURNS varchar(20)
BEGIN
   DECLARE income_level varchar(20);
   IF monthly_value <= 4000 THEN
        SET income_level = 'Low Income';
   ELSEIF monthly_value > 4000 AND monthly_value <= 7000 THEN
        SET income_level = 'Avg Income';
   ELSE
        SET income_level = 'High Income';
   END IF;
   INSERT INTO log (description)
   VALUES ('getIncomeLevel function was called');
   RETURN income_level;
END; //
DELIMITER ;
After creating the function we can call it:SELECT getIncomeLevel(450)CASCADE - deletes or updates appropriate rows in the child table
SET NULL - sets the foreign key column of the child table to NULL
RESTRICT - rejects the operation for the parent table
NO ACTION - equivalent to RESTRICT
Note: RESTRICT is the same as omitting the ON DELETE or ON UPDATE clause, which means that it's default actionbefore insert and checks & replaces gender valuesDROP TRIGGER IF EXISTS before_student_insert;
DELIMITER $$
    CREATE TRIGGER before_student_insert BEFORE INSERT 
    ON students
    FOR EACH ROW 
    BEGIN
    INSERT INTO log (description)
    VALUES ('One student will be inserted');
    
    IF (NEW.gender = 'Female') THEN
        SET NEW.gender = 2;
    ELSE
        SET NEW.gender = 1;
    END IF;
    END$$
DELIMITER ;before or after some database event. 
The event can be insert, update or delete.
It means there are six types of triggers:
before / after insert
before / after update
before / after deleteFOREIGN KEY constraint  with student_id column and references it to students.id ALTER TABLE notes 
ADD CONSTRAINT fk_student_note
    FOREIGN KEY (student_id)  
    REFERENCES students(id) 
    ON UPDATE CASCADE 
    ON DELETE RESTRICT
We can omit CONSTRAINT fk_student_note sub-clause.
The difference is that if we omit the sub-clause, MySQL names the constraint automaticallyALTER TABLE notes 
ADD FOREIGN KEY (student_id)  
    REFERENCES students(id) 
    ON UPDATE CASCADE 
    ON DELETE RESTRICTALTER TABLE notes 
DROP FOREIGN KEY fk_student_note`;NO ACTION is equivalent to RESTRICT.  
The MySQL Server rejects the delete and update operations of the parent table if there is a related foreign key value in the referenced table