Chapter 1: Understanding MySQL
- Three-tier architecture:
- Presentation layer (
UI): the interface that interacts directly with users. - Business logic layer (
BLL): processes business logic and user requests. - Data access layer (
DAL): database operations such as create, read, update, and delete.
- Presentation layer (
SQL(Structured Query Language) is declarative: it tells the database what to query, not how. The database uses algorithms and statistics to calculate the best access path, a task performed by theoptimizer.- MySQL permissions:
- Grant query, insert, update, and delete permissions:
grant select,insert,update,delete on db_name.* to user_name@ '10.%' identified by 'pwd' - Revoke permissions:
revoke select,insert,update,delete on db_name.* from user_name@ '10.%'
- Grant query, insert, update, and delete permissions:
- Storage engines: ARCHIVE is designed for large volumes of enterprise transaction data. It uses lossless zlib compression to store data in compressed archive tables, compressing it on insertion. It supports only
insertandselect, an auto-increment key and its index, but no other indexes. It suits log and user behavior analysis whereu、dand indexes are unnecessary. MySQLmeans theMYSQLserver;mysqlmeans the client.- Clear entered text with
'\c. If the preceding statement uses a single quote, use a single quote; if it uses a double quote, use a double quote. - Main
MySQLparameter settings:innodb_buffer_pool_size: to improve write performance, if the data and access patterns are unknown, set it to 70%-80% of physical memory.innodb_log_file_size: size of each file in the log group; 256mb or more is recommended.innodb_flush_log_at_trx_commit: 2 is recommended; the default is 1. At 2, committing a transaction writes the log buffer to a file without flushing the log file to disk.sync_binlog: 0 is recommended, with no disk synchronization. Inautocommitmode each statement writes to the binary log; otherwise each transaction writes once.
Chapter 3
- Development basics:
- Data model: the definition and format of data, or how data is composed. A relational model represents entities and their relationships in two-dimensional tables, each also called a relation.
- DLL →
Data Definition language: defines data structures and database objects, usually for creating databases and tables (create、alter、drop). - DML →
Data Manipulation Language: queries and modifies data, includingselect、insert、update、delete, the four operations calledCRUD(create、read、upadte、delete). XML(eXtensible Markup Language)is an extensible markup language, initially designed to exchange data on theInternet.xmltransports and stores data; its tags describe content. HTML displays data; its tags format it.
- Data types: all of the following are signed.
tinyint: 1 byte, range-128~~127.smallint: 2 bytes,-32768 ~~ 32767.mediumint: 3 bytes,-8388608~~8388607.int: 4 bytes,-2147483648~~2147483647.bigint: 8 bytes,-9223372036954775808~~9223372036954775807.float: values are rounded.- Dates and times:
- Without the
NO_ZERO_DATESSQLmode, MySQL by default only permits dates with zero months and days indateordatetimecolumns. 70~99represents1970-1999.00~69represents2000-2069.- For both date and time use
datetime, 8 bytes. - For dates only use
date, 3 bytes. timestampis stored inutc: it converts from the current time zone on storage and back on retrieval. The current time zone defaults to thetime_zonesystem variable. It occupies four bytes.time: three bytes.year: for displaying only the year, 1 byte.
- Without the
- Introduction to indexes:
- Index categories:
- B-tree (
B-tree), hash (hash), spatial (R-tree), and full-text (full-text) indexes. - Logical categories: single-column, composite (multiple columns), unique (
Unique), and non-unique (Non Unique) indexes.
- B-tree (
- Clustered indexes: if the logical order of index keys matches the physical order of the corresponding rows in the table, it is a clustered index. Data and the index (
B+tree) are together, with records stored in its leaves. A clustered index is also called an index-organized table; otherwise the index is non-clustered.InnoDBuses clustered indexes. - Index queries:
- Filter using the leftmost-prefix principle.
- MySQL optimizer:
- Meanings of
extrainexplainoutput:select tables optimized away:MIN、MAXoperations can be answered directly from index information without retrieving physical records.Using index: a covering index, where all data comes from the index without reading physical records.
- Meanings of