search
HomeDatabaseMysql TutorialMySQL学习笔记3索引、存储过程_MySQL

bitsCN.com

MySQL索引 索引分类: 索引两种存储类型:B型树(BTREE)索引和哈希(HASH)索引,其中B型树为系统默认索引方法。MySQL的索引包括普通索引、唯一索引、全文索引、单列索引、多列索引和空间索引。

注意:只有MyISAM类型的数据表支持FULLTEXT全文索引,其他类型的数据表不支持全文索引。当用户在建立全文索引的时候,返回“ERROR 1283 (HY000): Column 'number' cannot be part of FULLTEXT index”的错误,则说明用户操作的当前数据表不支持全文索引,即不为MyISAM类型的数据表。

只有MyISAM类型表支持空间索引。而且,索引字段必须有非空约束。创建索引 在建立数据表时创建索引

语法结构如下:

create table table_name(

属性名 数据类型 [约束条件],

属性名 数据类型 [约束条件],

^……

属性名 数据类型

[UNIQUE | FULLTEXT | SPATIAL ] INDEX }KEY

[别名](属性名1 [(长度)] [ASC | DESC]) 创建多列索引

触发多列索引的条件是用户必须使用索引的第一字段,如果没有用到第一字段,则索引不起任何作用,用户想要优化查询速度,可以应用该类索引形式。在已建立的数据表中创建索引 基本命令结构如下:
CREATE [UNIQUE | FULLTEXT |SPATIAL ] INDEX index_name

ON table_name(属性 [(length)] [ ASC | DESC]); 修改数据表结构添加索引 基本结构如下:

ALTER TABLE table_name ADD [ UNIQUE | FULLTEXT | SPATIAL ] INDEX index_name(属性名[(length)] [ASC | DESC]);注意:

从功能上看,修改数据表结构添加索引与在已存在数据表中建立索引所实现功能大体相同,二者均是在已经建立的数据表中添加或创建新的索引。 删除索引 基本命令如下:

DROP INDEX index_name ON table_name; MySQL存储过程 创建存储过程和存储函数 创建存储过程

基本形式:CREATE PROCEDUER sp_name ([proc_parameter [,...]]) [characteristic ...] routine_body

存储过程调用方法:call 存储过程名;注意:MySQL中默认的语句结束符为分号; 存储过程中的SQL语句需要分号来结束,为了避免冲突,首先,用“DELIMITER // ”将MySQL的结束符设置为//。最后用“DELIMITER;”来将结束符恢复成分号。这与触发器的创建是一样的。创建存储函数

基本形式:CREATE FUNCTION sp_name ([func_parameter[,...]])

RETURNS types

[characteristic ...] routine_body变量的应用

MySQL存储过程中的参数主要有局部参数和会话参数两种,又可被称为局部变量和会话变量。局部变量只在定义该局部变量的begin……end范围内有效,会话变量在整个存储过程范围内均有效。

局部变量以关键字declare声明,后跟变量名和变量类型,例如 declare a int

也可以用关键字default为变量指定默认值,例如declare a int default 10MySQL中的会话变量不必声明即可使用,会话变量在整个过程中有效,会话变量名以字符“@”作为起始字符。为变量赋值 用DECLARE关键字定义变量:DECLARE var_name[,...] type [default value]使用SET关键字为变量赋值:SET var_name=expr[, var_name=expr]...SELETC col_name [ ,...] INTO var_name [, ...] FROM table_name where condition

示例:select tel into customer_tel from studentinfo where name='LeonSK';注意:上述赋值语句必须存在于创建的存储过程中,且需要将赋值语句放置在BEGIN……END之间。若脱离此范围,该变量将不能使用或被赋值。光标的运用

声明光标

光标必须声明在处理程序之前,且声明在变量和条件之后。

语法:DECLARE cursor_name CURSOR FOR select_statement

select 子句中不能包含INTO子句,并且光标只能在存储过程或存储函数中使用。打开光标

语法:OPEN info_of_student使用光标

使用FETCH...INTO语句来读取数据,语法如下:

FETCH cursor_name INTO var_name[, var_name]...关闭光标

语法:CLOSE curso_name

对于以关闭的光标,在其关闭之后则不能使用FETCH来使用光标,光标在使用完毕后一定要关闭。查看存储过程和函数 SHOW STATUS语句

SHOW {PROCEDUER | FUNCTION} STATUS[LIKE 'pattern' ]SHOW CREATE语句

SHOW CREATE { PROCEDUER | FUNCTION } sp_name;SHOW STATUS语句只能查看存储过程或函数所操作的数据库对象,如存储过程或函数的名称、类型、定义者、修改时间等信息,并不能查询存储过程或函数的具体定义。如需要查看详细定义,需要使用SHOW CREATE语句。修改存储过程和函数 语法如下:

ALTER {PROCEDUER | FUNCTION} sp_name [characteristic ...]

characteristic:

{ CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }

| SQL SECURITY { DEFINER | INVOKER }

| COMMENT 'string'

bitsCN.com
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL String Types: Storage, Performance, and Best PracticesMySQL String Types: Storage, Performance, and Best PracticesMay 10, 2025 am 12:02 AM

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

Understanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreUnderstanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreMay 10, 2025 am 12:02 AM

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

What are the String Data Types in MySQL?What are the String Data Types in MySQL?May 10, 2025 am 12:01 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

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.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!