MySQL 필수 사항저장 프로세스

저장 프로세스
의미: 미리 컴파일된 sql 문장의 집합 장점:
1、   sql      ,           
2、     
3、       

분류:
1、     
2、   in  ,     
3、   out  ,     
4、  in  out,     
5、 inout,     
  :in、out、inout              

저장 프로세스 생성
구문:
create procedure      (in|out|inout          ,...)
begin
         

end

이와 유사한 방법:
            (        ,...){

       ;
}

주의
1、          
delimiter       
  :
delimiter $

CREATE PROCEDURE      (IN|OUT|INOUT          ,...)
BEGIN
    sql  1;
    sql  2;

END $

2、           sql  ,      sql  ,     begin end

3、          
in:          (         )
out:         (         )
inout:          

메모리 프로세스 호출
call      (    )

함수.
함수 만들기
배운 함수: LENGTH, SUBSTR, CONCAT 등의 구문:
CREATE FUNCTION    (        ,...) RETURNS     
BEGIN
       

END

호출 함수
SELECT    (    )

함수와 저장 과정의 차이
                                        
        FUNCTION    SELECT   ()                             ,           
        PROCEDURE   CALL     ()    0                  

#
 、   ★
create procedure      (             )
begin
             
end
  :
1.    :in、out、inout,  in    
2.         sql          

 、  
call      (    )
  :
  in     :call sp1(‘ ’);
  out     :set @name; call sp1(@name);select @name;
  inout     :set @name= ; call sp1(@name); select @name;
 、  
show create procedure      ;
 、  
drop procedure      ;

##################### 사례 프레젠테이션
#       
/*
       :   java    
  :
1、        
2、    



*/
#    
/*
  :        SQL     ,        
1、        
2、    
3、                        ,     



*/

# 、    

CREATE PROCEDURE      (    )
BEGIN

         (     SQL  )
END

#  :
/*
1、         
               
  :
in stuname varchar(20)

    :
in:         ,              
out:         ,             
inout:                 ,            ,      

2、              ,begin end    
         sql            。
            delimiter     
  :
delimiter     
  :
delimiter $
*/


# 、    

CALL      (    );

#--------------------------------    -----------------------------------
#1.    
#  :   admin      

SELECT * FROM admin;

DELIMITER $
CREATE PROCEDURE myp1()
BEGIN
    INSERT INTO admin(username,`password`) 
    VALUES('john1','0000'),('lily','0000'),('rose','0000'),('jack','0000'),('tom','0000');
END $


#  
CALL myp1()$

#2.   in         

#  1:              ,         

CREATE PROCEDURE myp2(IN beautyName VARCHAR(20))
BEGIN
    SELECT bo.*
    FROM boys bo
    RIGHT JOIN beauty b ON bo.id = b.boyfriend_id
    WHERE b.name=beautyName;


END $

#  
CALL myp2('  ')$
SHOW VARIABLES LIKE '%char%';

#  2 :        ,        

CREATE PROCEDURE myp4(IN username VARCHAR(20),IN PASSWORD VARCHAR(20))
BEGIN
    DECLARE result INT DEFAULT 0;#      

    SELECT COUNT(*) INTO result#  
    FROM admin
    WHERE admin.username = username
    AND admin.password = PASSWORD;

    SELECT IF(result>0,'  ','  ');#  
END $

#  
CALL myp3('  ','8888')$


#3.  out          
#  1:        ,        

CREATE PROCEDURE myp6(IN beautyName VARCHAR(20),OUT boyName VARCHAR(20))
BEGIN
    SELECT bo.boyname INTO boyname
    FROM boys bo
    RIGHT JOIN
    beauty b ON b.boyfriend_id = bo.id
    WHERE b.name=beautyName ;

END $

CALL myp5('  ',@bName)$
SELECT @bName$


#  2:        ,            

CREATE PROCEDURE myp7(IN beautyName VARCHAR(20),OUT boyName VARCHAR(20),OUT usercp INT) 
BEGIN
    SELECT boys.boyname ,boys.usercp INTO boyname,usercp
    FROM boys 
    RIGHT JOIN
    beauty b ON b.boyfriend_id = boys.id
    WHERE b.name=beautyName ;

END $


#  
CALL myp7('  ',@name,@cp)$
SELECT @name,@cp$



#4.   inout         
#  1:  a b   ,  a b      

CREATE PROCEDURE myp8(INOUT a INT ,INOUT b INT)
BEGIN
    SET a=a*2;
    SET b=b*2;
END $

#  
SET @m=10$
SET @n=20$
CALL myp8(@m,@n)$
SELECT @m,@n$

#############################  ##########################
SELECT * FROM admin;
DELIMITER $;
CREATE PROCEDURE te (IN username VARCHAR(20),IN PASSWORD VARCHAR(20))
BEGIN
    INSERT INTO admin(admin.username,admin.password)
    VALUES (usrname,PASSWORD);
END $

# 、      
#  :drop procedure      
DROP PROCEDURE p1;
DROP PROCEDURE p2,p3;#×

# 、         
DESC myp2;×
SHOW CREATE PROCEDURE  myp2;

#################연습문제 설명
# 、                ,   admin  

CREATE PROCEDURE test_pro1(IN username VARCHAR(20),IN loginPwd VARCHAR(20))
BEGIN
    INSERT INTO admin(admin.username,PASSWORD)
    VALUES(username,loginpwd);
END $

# 、              ,           

CREATE PROCEDURE test_pro2(IN id INT,OUT NAME VARCHAR(20),OUT phone VARCHAR(20))

BEGIN
    SELECT b.name ,b.phone INTO NAME,phone
    FROM beauty b
    WHERE b.id = id;

END $
# 、                     ,    

CREATE PROCEDURE test_pro3(IN birth1 DATETIME,IN birth2 DATETIME,OUT result INT)
BEGIN
    SELECT DATEDIFF(birth1,birth2) INTO result;
END $
# 、                 ,    xx xx xx    
CREATE PROCEDURE test_pro4(IN mydate DATETIME,OUT strDate VARCHAR(50))
BEGIN
    SELECT DATE_FORMAT(mydate,'%y %m %d ') INTO strDate;
END $

CALL test_pro4(NOW(),@str)$
SELECT @str $

# 、                 ,  :   and           
     :  
  :    AND    
DROP PROCEDURE test_pro5 $
CREATE PROCEDURE test_pro5(IN beautyName VARCHAR(20),OUT str VARCHAR(50))
BEGIN
    SELECT CONCAT(beautyName,' and ',IFNULL(boyName,'null')) INTO str
    FROM boys bo
    RIGHT JOIN beauty b ON b.boyfriend_id = bo.id
    WHERE b.name=beautyName;


    SET str=
END $

CALL test_pro5('  ',@str)$
SELECT @str $



# 、         ,             ,  beauty    
DROP PROCEDURE test_pro6$
CREATE PROCEDURE test_pro6(IN startIndex INT,IN size INT)
BEGIN
    SELECT * FROM beauty LIMIT startIndex,size;
END $

CALL test_pro6(3,5)$

좋은 웹페이지 즐겨찾기