Databases 25 min read

Comprehensive Guide to MySQL Storage Engines, Data Types, Indexes, Views, Stored Procedures, and Triggers

This article provides an in‑depth overview of MySQL, covering storage engine concepts, selecting appropriate data types, index design and usage, view creation, stored procedure syntax, variable handling, and trigger implementation, with practical examples and SQL code snippets to illustrate each topic.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Comprehensive Guide to MySQL Storage Engines, Data Types, Indexes, Views, Stored Procedures, and Triggers

MySQL storage engines act as the data‑storage engine that writes data to disk; the architecture can be understood as a three‑layer model.

Supported engines include MyISAM, InnoDB, BDB, MEMORY, MERGE, EXAMPLE, NDB Cluster, ARCHIVE, CSV, BLACKHOLE, and FEDERATED. The current engine can be inspected with show variables like 'table_type'; or show engines; .

The default‑engine parameter default-table-type was removed after MySQL 5.5.3. An engine can be specified when creating a table, for example create table cxuan002(id int(10),name varchar(20)) engine = MyISAM; . Since MySQL 5.1 the default engine is InnoDB, which can be verified with show create table … .

Choosing a storage engine: use MyISAM for read‑heavy workloads with few writes, InnoDB for transactional and high‑concurrency workloads, MEMORY for small tables that require fast access, and MERGE to combine multiple MyISAM tables.

Data‑type selection: CHAR stores fixed‑length strings (padded with spaces) while VARCHAR stores variable‑length strings with a length byte; strict mode affects overflow handling. TEXT and BLOB families differ in that BLOB stores binary data. Floating‑point types ( FLOAT , DOUBLE ) may lose precision compared to fixed‑point DECIMAL . Date/time types include DATE , TIME , DATETIME , TIMESTAMP , and YEAR , each suited to different use‑cases.

MySQL character sets: common sets are ASCII, ISO‑8859‑1, GBK, UTF‑8, UTF‑16, UTF‑32. List them with show character set; or query information_schema.character_sets for details.

Index design: index types include FULLTEXT, HASH, BTREE, and RTREE. Use unique indexes for columns with unique values, prefix indexes for long columns, and composite (multi‑column) indexes to exploit left‑most prefix rules. Create and analyze indexes with explain . Example: create a prefix index on a table and later drop it.

Views: a view is a virtual table. Create with create view v1 as select * from product; , describe with describe v1; , update through the view, and drop with drop view v1; .

Stored procedures: change the statement delimiter, e.g. delimiter $$ , then create a procedure: create procedure sp_product() begin select * from product; end $$ . Call it with call sp_product(); . Procedures can accept parameters and be dropped with drop procedure sp_product; .

Variables: MySQL has system, user, session, and global variables. Set a user variable with set @myId = "cxuan"; . List session variables with show session variables; . Set global variables with set global sql_warnings=ON; or set @@global.sql_warnings=OFF; .

Control‑flow statements in stored programs include IF, CASE, LOOP, LEAVE, ITERATE, REPEAT, and WHILE, each with its own syntax for conditional and iterative logic.

Triggers: create with syntax such as create trigger tg_pinfo before insert on product for each row begin insert into product_info values(new.name); end; . Triggers can enforce data integrity, maintain audit logs, or automate related table updates. View existing triggers with show triggers; or query information_schema.triggers . Note that triggers cannot invoke stored programs that return data to the client or start/commit transactions.

Overall, the article equips readers with practical knowledge to design efficient MySQL schemas, choose appropriate storage engines and data types, and leverage indexes, views, stored procedures, variables, and triggers for robust database development.

DatabaseStorage EngineMySQLIndexesdata typesViewsTriggersstored procedures
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.