个人觉得就是一个cursor,就是一个标识,用来标识数据取到什么地方了。你也可以把它理解成数组中的下标。。
使用游标(cursor)
1.声明游标
DECLARE cursor_name CURSOR FOR select_statement
这个语句声明一个游标。也可以在子程序中定义多个游标,但是一个块中的每一个游标必须有唯一的名字。声明游标后也是单条操作的,但是不能用SELECT语句不能有INTO子句。
2. 游标OPEN语句
OPEN cursor_name
这个语句打开先前声明的游标。
3. 游标FETCH语句
FETCH cursor_name INTO var_name [, var_name] ...
这个语句用指定的打开游标读取下一行(如果有下一行的话),并且前进游标指针。
4. 游标CLOSE语句
CLOSE cursor_name
这个语句关闭先前打开的游标。
例1
代码如下 | 复制代码 |
//建立表结构 insert into person values(¹a¹); //初始化 //建立 //定义变量 declare mycursor CURSOR FOR select name from person; declare CONTINUE HANDLER FOR SQLSTATE ¹02000¹ SET tmpname = null; //打开游标 //遍历游标 //把游标查询出的 name 都加起并用 ; 号隔开 select mycursor; //调用游标 |
运行结果:
mysql> call mycursor()
+--------------------------------------+
| name2 |
+--------------------------------------+
| a;b;c;d;e; |
+--------------------------------------+
1 row in set (0.01 sec)
例2
一个完整的例子:
代码如下 | 复制代码 |
-- 定义本地变量 DECLARE o varchar(128);
DECLARE ordernumbers CURSOR FOR SELECT callee_name FROM account_tbl where acct_timeduration=10800; DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_departments=1; SET no_more_departments=0; OPEN ordernumbers;
REPEAT -- Get order number FETCH ordernumbers INTO o; update account set allMoney=allMoney+72,lastMonthConsume=lastMonthConsume-72 where NumTg=@o; UNTIL no_more_departments -- 关闭游标 CLOSE ordernumbers; |
游标(cursor)的特性
1,只读的,不能更新的。
2,不滚动的
3,不敏感的,不敏感意为服务器可以活不可以复制它的结果表
游标(cursor)必须在声明处理程序之前被声明,并且变量和条件必须在声明游标或处理程序之前被声明

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
