MySQL Program Authoring Example

Source: Internet
Author: User

DELIMITER $$

Drop trigger if exists ' updata_depart_lead_name ';
--New trigger--
--function: In the Update department leader number is, automatically fill in the Department leader name--
Create TRIGGER ' updata_depart_lead_name ' before update on ' Department '
For each row BEGIN
Set @lead_name = ';

If new.lead_id is null or new.lead_id = "Then
Set @lead_name = null;
ELSE
Select name into @lead_name from the staff where id = new.lead_id;
End IF;

Set new.lead_name = @lead_name;
END

$$

————————————————————————————————————————————————————————————————————————————————

Delimiter $$

Drop trigger if exists ' insert_depart_lead_name ';
--New trigger--
--function: When the new department, automatically fill in the name of the department leader-
Create TRIGGER ' insert_depart_lead_name ' before insert on ' department '
For each row BEGIN
Set @lead_name = ';
If new.lead_id is null or new.lead_id = "Then
Set @lead_name = null;
ELSE
Select name into @lead_name from the staff where id = new.lead_id;
End If;
Set new.lead_name = @lead_name;
End

$$

—————————————————————————————————————————————————————————————————————————————————————

Delimiter $$

Drop trigger if EXISTS ' remove_depart_lead_on_delete_staff ';
--when deleting the staff, clear the leadership information in department
Create TRIGGER ' remove_depart_lead_on_delete_staff ' after delete in ' staff '
For each row BEGIN
Update department Set lead_id = null, Lead_name = NULL where lead_id = Old.id;
End
$$

————————————————————————————————————————————————————————————————————————————————————————

Delimiter $$

Drop trigger if exists ' trigger_cursor ';

Create TRIGGER ' trigger_cursor ' after insert in ' staff '
For each row BEGIN
declare staff_id int;
DECLARE done int default false;
DECLARE Cur_test CURSOR for
Select ID from the staff;
Declare CONTINUE HANDLER for don't FOUND set done = true;
Open cur_test;
Read_loop:loop
Fetch cur_test into staff_id;
If do then
LEAVE Read_loop;
ELSE
INSERT into message values (NULL, ' 1 ');
End If;
End Loop;
END
$$

MySQL Program Authoring Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.