데이터 베이스 MySQL 튜토리얼 mysql中的触发器以及存储过程的基础了解_MySQL

mysql中的触发器以及存储过程的基础了解_MySQL

Jun 01, 2016 pm 01:41 PM
방아쇠

bitsCN.com mysql中的触发器以及存储过程的基础了解 触发器的基础了解: 01触发器(trigger):一触即发,就是当进行某种操作之后(或者之前),附加的一种操作就马上执行。02作用:监视某种情况并触发某种操作;03注意事项:04能监视的事件:增,删,改;能触发的事件:增,删,改;05 06监视地点:table07监视事件:insert/update/delete08触发时间:after/before09触发事件:insert/update/delete10 11#建立商品表:12    create table goods(goodsId int, name varchar(10), num int)charset utf8$13 14#建立订单表:15create table orders(ordersId int, goodsId int, num int)charset utf8$16 17#插入数据18insert into goods values(1,'猪',22),(2,'羊',19),(3,'狗',12),(4,'猫',8)$19 20#买三只羊21insert into orders values(1, 2, 3);22 23#减少羊的库存24update goods set num = num -3 where goodsId = 2;25 26#开始学着使用触发器27#监视地点:orders28#监视操作:insert29#触发操作:update30#触发时间:after31 32#创建第一个触发器33create trigger t134after insert on orders35for each row36begin37    update goods set num = num -3 where goodsId = 2;38    end$39 40#删除触发器41drop trigger t1$42 43#创建第二个触发器44create trigger t245after insert on orders46for each row47begin48    update goods set num = num - new.num where goodsId = new.goodsId;49end$50 51#创建第三个触发器52create trigger t353after delete on orders54for each row55begin    56    update goods set num = num + old.num where goodsId = old.goodsId;57end$58 59#创建第四个触发器60create trigger t461after update on orders62for each row63begin64    update goods set num = num + old.num - new.num where goodsId = old.goodsId;65end$存储过程的基础了解: 01存储过程的含义:02存储过程类似于函数,就是把一段代码封装起来,当要执行这一段代码的时候,可以通过调用该存储过程来实现。在封装的语句体里面,可以使用if/else, case, while等控制结构。这里面就有所谓的sql编程。03#测试表的建立及表的操作:04create table users( num int, name varchar(20), password varchar(20));05    insert into users values(1,'zwh','zwh');06insert into users values(2, 'lhy', 'lhy');07insert into users values(3, 'test', 'test');08insert into users values(4, 'test', 'test');09insert into users values(5, 'test', 'test');10insert into users values(6, 'test', 'test');11insert into users values(7, 'test', 'test');12insert into users values(8, 'test', 'test');13insert into users values(9, 'test', 'test');14insert into users values(10, 'test', 'test');15     16#查看存在的存储过程:17show procedure status;18 19#删除存在的存储过程:20drop procedure ***;21 22 23#这里先把定界符先改为$:24delimiter $25 26#建立第一个存储过程:体验一下"sql封装"27create procedure p1()28begin29    select * from users;30end$31 32#调用存储过程33call p1()$34 35#建立第二个存储过程:体验一下"参数"36create procedure p2( n int )37begin38    select * from users where num n;46    else47        select * from users where num

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

PowerPoint에서 클릭할 때까지 텍스트를 숨기는 방법 PowerPoint에서 클릭할 때까지 텍스트를 숨기는 방법 Apr 14, 2023 pm 04:40 PM

PowerPoint에서 클릭할 때까지 텍스트를 숨기는 방법

Oracle에 트리거를 추가하는 방법 Oracle에 트리거를 추가하는 방법 Dec 12, 2023 am 10:17 AM

Oracle에 트리거를 추가하는 방법

MySQL 트리거를 사용하여 데이터 자동 보관을 구현하는 방법 MySQL 트리거를 사용하여 데이터 자동 보관을 구현하는 방법 Aug 02, 2023 am 10:37 AM

MySQL 트리거를 사용하여 데이터 자동 보관을 구현하는 방법

mysql 트리거는 몇 레벨인가요? mysql 트리거는 몇 레벨인가요? Mar 30, 2023 pm 08:05 PM

mysql 트리거는 몇 레벨인가요?

C#을 사용하여 MySQL에서 사용자 정의 저장 프로시저, 트리거 및 함수를 작성하는 방법 C#을 사용하여 MySQL에서 사용자 정의 저장 프로시저, 트리거 및 함수를 작성하는 방법 Sep 20, 2023 pm 12:04 PM

C#을 사용하여 MySQL에서 사용자 정의 저장 프로시저, 트리거 및 함수를 작성하는 방법

PHP를 사용하여 MySQL에서 사용자 정의 트리거 및 저장 프로시저를 작성하는 방법 PHP를 사용하여 MySQL에서 사용자 정의 트리거 및 저장 프로시저를 작성하는 방법 Sep 20, 2023 am 11:25 AM

PHP를 사용하여 MySQL에서 사용자 정의 트리거 및 저장 프로시저를 작성하는 방법

PHP를 사용하여 MySQL에서 트리거를 작성하는 방법 PHP를 사용하여 MySQL에서 트리거를 작성하는 방법 Sep 21, 2023 am 08:16 AM

PHP를 사용하여 MySQL에서 트리거를 작성하는 방법

Python을 사용하여 MySQL에서 사용자 정의 트리거를 작성하는 방법 Python을 사용하여 MySQL에서 사용자 정의 트리거를 작성하는 방법 Sep 20, 2023 am 11:04 AM

Python을 사용하여 MySQL에서 사용자 정의 트리거를 작성하는 방법

See all articles