MySQL Learning notes

Source: Internet
Author: User
Tags define session define local

1. Unique SQL commands

1) Modify field name: ALTER TABLE tableName change oldname newName type;

   Note: The type can be different from the original field type.

  2) Modify table name: ALTER TABLE tableName Rename [to] newtablename;

3) Paging query: select * from tableName limit startLine,amount;

Note: startLine-the startline+1 row in the database as the first data of the query result, startLine starting from 0;

Amount--Number of bars displayed

4) Query the database encoding format: Show create databases databaseName;

2. Stored Procedures

1) stored procedures with no parameters  

--fame TerminatorDELIMITER $--create a stored procedureCREATE PROCEDUREpro_test ()BEGIN    --multiple SQL statements/logic can be added    INSERT  intoEmpVALUES(1,'Huwei'); INSERT  intoEmpVALUES(2,'Xingkong');END $--call a stored procedureCall pro_test ();--To Delete a stored procedure--Note: finally No ()DROP PROCEDUREPro_test;

2) stored procedure with input parameters

--fame TerminatorDELIMITER $--Creating a stored procedure (in representing input parameters)CREATE PROCEDUREPro_test (inchEidINT)BEGIN    SELECT *  fromEmpWHEREId=Eid;END $--call a stored procedureCall Pro_test (1);--To Delete a stored procedure--Note: finally No ()DROP PROCEDUREPro_test;

3) stored procedure with output parameters

--fame TerminatorDELIMITER $--Create a stored procedure (out represents an output parameter)CREATE PROCEDUREPro_out (outStr VARCHAR( -))BEGIN    SET Str='Hello Procedure';END $--call a stored procedureCall Pro_out (@str);--View ResultsSELECT @str;

--Delete stored procedures
--Note: finally No ()
DROP PROCEDURE pro_out;

4) Stored procedures that use query results as OUTPUT parameters

DELIMITER $CREATE PROCEDUREPro_into (out enameVARCHAR( -))BEGIN    --into: Assign name to ename    SELECTNAME intoEname fromEmpWHEREId= 1;END$CALL Pro_into (@ename);SELECT @ename;

5) stored procedure with input and output parameters

--Declaration name TerminatorDELIMITER $--Create a stored procedure (out represents an output parameter)CREATE PROCEDUREPro_inout (InOut numINT)BEGIN    SETNum=Num+ 1;END $--set session variable num=10SET @num = Ten;--call a stored procedureCall Pro_inout (@num);--View Output ResultsSELECT @num;

--Delete stored procedures
--Note: finally No ()
DROP PROCEDURE pro_inout;

6) storage structure with If-else logic

DELIMITER $CREATE PROCEDUREPro_if (inchSorceINT, Out LevVARCHAR( -))BEGIN    IFSorce<=  -  Then        SETLev= 'fail'; ELSEIF Sorce<=  the  Then        SETLev= 'Pass'; ELSEIF Sorce<=  -  Then        SETLev= 'Good'; --else and no then.    ELSE        SETLev= 'Great'; --Note: Do not forget to end the IF and semicolon    END IF;END$CALL pro_if ( -,@lev);SELECT @lev;

7) stored procedure with while logic

DELIMITER $CREATE PROCEDUREPro_while (out resultINT)BEGIN    --Defining local Variables    DECLAREIINT DEFAULT 1; DECLARESummaryINT DEFAULT 0;  whileI<= Ten DoSETSummary=Summary+i; SETI=I+ 1; END  while; SETResult=Summary;END$CALL Pro_while (@result);SELECT @result;

Three variants of 3.MySQL:

1) Global variables: MySQL built-in variables

Query all global variables: show variables;

Querying a global variable:select @@ varname;

Modify the value of the global variable:set varName = value;

2) Session variables: Variables that are valid only in this connection (connection interruption, variable destruction)

Define session Variables: set @varName = value;

Value of the query session variable: SELECT @varName;

3) Local variables: variables defined in the stored procedure

Define local variables: declare varName type [default value];

For more information, please visit: http://www.cnblogs.com/BlueStarWei/

MySQL Learning notes

Related Article

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.