bitsCN.com DECLARE语句允许我们创建变量。它将出现在代码块中任何游标和处理及任何过程语句声明之前,DECLARE语句的语法如下:
常用的MySQL数据类型
数据类型解释相应值的示例INT,INTEGER32位整数。取值范围为-21亿到+21亿,如果是非符号数值可以达到42亿,但这样做就不能包扩负数123,345,-2,000,000,000BIGINT64位整数。取值范围为-9万亿到+9万亿或者非负的0到18万亿9,000,000,000,000,000,000FLOAT32位浮点数。取值范围为-1.7e38to1.7e38或者非负的0到3.4e380.000000000000002,17897.890790,-345.8908770,1.7e21DOUBLE64位浮点数。取值范围接近无限(1.7e308)1.765e23,-1.765e100
DECIMAL(precision,scale)
NUMERIC(precision, scale)
定点数。存储情况取决于precision,能保存可能出现的数字范围。NUMERIC通常用来保存重要的十进制数,例如现金78979.00,-87.50,9.95DATE日期类型,没有详述时间'1999-12-31'DATETIME日期和时间,时间精确到秒'1999-12-31 23:59:59'CHAR(length)定长字符串。值会被空白填充至指定长度,最大长度为255字节'hello world'VARCHAR(length)最大长度为64K的可变字符串'hello world'BLOB,TEXT最大64K长度,BLOB用来保存2进制数据,TEXT用来保存文本数据任何想象的内容LONGBLOB,LONGTEXTBLOB和TEXT的加长版本,存储能力达4GB任何想象的内容,但比BLOB和TEXT能存放更大的长度<pre class="brush:sql;">DECLARE l_int1 int default -2000000DECLARE l_int2 INT unsigned 4000000DECLARE l_bigint1 BIGINT DEFAULT 4000000000000000 DECLARE l_float FLOAT DEFAULT 1.8e8DECLARE l_double DOUBLE DEFAULT 2e45DECLARE l_numeric NUMERIC(8,2) DEFAULT 9.95;DECLARE l_date DATE DEFAULT '1999-12-31'DECLARE l_datetime DATETIME DEFAULT '1999-12-31 23:59:59'DECLARE l_char CHAR(255) DEFAULT 'This will be padded to 255 chars';DECLARE l_varchar VARCHAR(255) DEFAULT 'This will not be padded'DECLARE l_text TEXT DEFAULT 'This is a really long string. In sored programs we can use text colums fairly freely, but in tables there are some limiations regarding indexing and use in various expressions.'
字面常量
下面是三大基本字面量类型
数字字面常量,十六进制的传统表示方法,在它前面加上'0x'。
日期字面变量
字符字面变量
变量命名规则
参数
IN除非被具体定义,否则参数都假定IN属性。这意味着他们的值必须被主叫程序所指定,并且任何在存储程序内部对该参数的修改都不能在主叫程序中起作用
OUT 当存储程序开始时,任何OUT变量的值都被赋值为NULL,不管这个值在主叫程序中是否被赋予其他值。
CREATE PROCEDURE sp_demo_in_parameter(IN p_in INT)BEGIN /*We can see the value of the IN parameter*/ SELECT p_in; /*We can modify it */ SET p_in = 2; /* show that the modification took effect */ select p_in;END;/*This output shows that the changes made within the stored program cannot be accessed from the calling program(in this case, the mysql client):*/set @p_in = 1call sp_demo_in_parameter(@p_in)select @p_in, 'We can''t see the changed value from the calling program'
OUT参数的例子
CREATE PROCEDURE sp_demo_out_parameter(OUT p_out INT)BEGIN /*We can't see the value of the OUT parameter*/ SELECT p_out, 'We can''t see the value of the OUT parameter'; /*We can modify it */ SET p_out = 2; SELECT p_out, 'OUT parameter value has been changed';END;SET @p_out = 1CALL sp_demo_out_parameter(@p_out)SELECT @p_out, "Calling program can see the value of the changed OUT parameter"
INOUT参数的例子
CREATE PROCEDURE sp_demo_inout_parameter(INOUT p_inout INT)BEGIN SELECT p_inout, 'We can see the value of the INOUT parameter in the stored program'; SET p_inout = 2; SELECT p_inout, 'INOUT parameter value has been changed';END;set @p_inout = 1call sp_demo_inout_parameter(@p_inout)select @p_inout, "Calling program can see the value of the changed INOUT parameter"
用户变量
用于变量是在MySQL中被定义并且可以在存储程序中或存储程序之外被操作的变量。有两种方法是用用户变量:
1、因为用户变量具有独立于存储程序个体的作用域,可以用来描述能够被任何存储程序所读写的会话。
2、用户变量可以给方法传递参数以第二种选择,存储程序对用户变量具有读写权限,这样可以避免使用参数传值造成的不方便。
用户变量可以被MySQL命令行客户端从任何程序创建并操纵。来确保MySQL语句使用SET语句。
SELECT 'Hello World' into @x;SET @y = 'Goodbye Cruel World';SELECT @y;SET @z = 1+2+3;SELECT @z;
使用用户变量在主叫程序和被叫程序之间传递信息
CREATE PROCEDURE GreetWorld()SELECT CONCAT(@greeting, ' World');SET @greeting = 'Hello';CALL GreetWorld);
把用户变量当成全局变量交叉使用
CREATE PROCEDURE p1()SET @last_procedure = 'p1';CREATE PROCEDURE p2()SELECT CONCAT('Last procedure was ', @last_procedure);CALL p1();CALL p2();
注释
MySQL存储过程支持两种不同风格的注释:
1、两个连字符跟上一个空格,创建了一个到当前行末的注释
2、C语言风格的注释,用/*开始,以*/结束。我们称它为多行注释
操作符
MySQL包括大家在大多数语言中早已熟识的操作符,但是C风格的操作符(++, += )并不支持。
CREATE PROCEDUREoperators()BEGIN DECLARE a int default 2; DECLARE b int default 3; DECLARE c FLOAT; SET c = a + b; SELECT 'a+b=', c; SET c = a/b; SELECT 'a/b=', c; SET c = a*b; SELECT 'a*b=', c; IF (a < b) THEN SELECT 'a is less than b'; END IF; IF NOT (a=b) THEN SELECT 'a is not equal to b'; END IF;END;
bitsCN.com

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

MySQL is not a programming language, but its query language SQL has the characteristics of a programming language: 1. SQL supports conditional judgment, loops and variable operations; 2. Through stored procedures, triggers and functions, users can perform complex logical operations in the database.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools