Table of contents
2.3. Tables related to permissions
Delete user (use with caution):
1. Database storage engine
View storage engine commands: SHOW ENGINES
DEFAULT: is the default engine
YES: means it can be used
NO: Indicates that it cannot be used
① MEMORY
MEMORY
It is a storage engine in the MySQL database that stores table data in memory instead of on disk. Also called HEAP storage engine because it can be specified using statements when creating tables.ENGINE=MEMORY
The following are some storage engine features and limitations:
MEMORY
Features:
- Fast access: Data is stored in memory, so reading and writing are fast.
- Table locks: The storage engine uses table-level locks instead of row-level locks, which may affect concurrency performance.
MEMORY
- Easy to configure: No complex configuration is required and tables can be easily created in memory.
limit:
- Data loss: Because data is stored in memory, database restarts or server shutdowns can result in data loss.
- Table size limit: The storage engine has a limit on the size of a table because it is limited by the system's available memory and configuration parameters.
MEMORY
- Transactions are not supported: Transaction processing is not supported, which means that the or operation cannot be used.
COMMIT
ROLLBACK
Please note that although the storage engine is useful for some specific use cases, it is not suitable for all situations due to its limitations. When selecting a storage engine, you need to consider factors such as application requirements, data volume, and concurrency performance.
MEMORY
②MyISAM
MyISAM is a common storage engine in the MySQL database. It has been the default storage engine of MySQL for a long time, but starting from MySQL 5.5 version, InnoDB replaced MyISAM as the default storage engine. Still, MyISAM has its place in certain scenarios.
The following are some features and considerations of the MyISAM storage engine:
Features:
Table-level locks: MyISAM uses table-level locks, not row-level locks. This can lead to performance degradation under high concurrent writes.
Support for full-text indexing: The MyISAM storage engine supports full-text search, which makes it useful in certain search applications.
Transactions not supported: MyISAM does not support transactions, which means it cannot be used or operated.
COMMIT
ROLLBACK
Table compression: MyISAM supports table-level compression, which can reduce storage space.
Caching: MyISAM uses the system file cache to cache indexes and data.
Ease of backup: MyISAM is relatively easy to handle when backing up due to the use of table-level locks.
Precautions:
Does not support transactions: If your application requires transaction support, you should consider using a storage engine such as InnoDB that supports transactions.
Not suitable for high concurrent writes: Due to the use of table-level locks, MyISAM may not perform as well as InnoDB under high concurrent writes.
Note that while MyISAM has its uses, the InnoDB storage engine is more common in modern applications, especially those that require transaction support or high concurrency performance. The choice of storage engine should be made based on the specific needs of the application.
③InnoDB
InnoDB is a commonly used storage engine in MySQL database. It is widely used in modern MySQL versions and is the default storage engine (starting with MySQL 5.5 version). InnoDB provides many advantages and is particularly suitable for applications that require transaction support and high concurrency performance.
The following are the key features and benefits of the InnoDB storage engine:
Transaction support: InnoDB is a storage engine that supports transaction processing. It adheres to the ACID (Atomicity, Consistency, Isolation, Durability) properties, allowing the use of and to manage transactions.
COMMIT
ROLLBACK
Row-level locking: InnoDB supports row-level locking, not table-level locking. This means that it can perform concurrent operations between different rows in the same table, improving performance in multi-user environments.
Foreign key constraints: InnoDB supports foreign keys, which can maintain data integrity and realize associations between tables.
Crash recovery: InnoDB provides the ability to recover from crashes. It has a log file (redo log) to record data modification operations, so that the database can be recovered under abnormal circumstances.
Concurrency control: InnoDB uses MVCC (Multiple Version Concurrency Control) to handle concurrent access, allowing read operations not to be blocked by write operations.
Auto-increasing size: InnoDB table sizes can be automatically increased as needed without manual setting or space allocation.
Performance optimization: InnoDB is optimized for high concurrent loads and is suitable for applications that need to handle a large number of concurrent read and write requests.
Although InnoDB provides superior performance and functionality in many aspects, other storage engines may be chosen in certain scenarios, such as for read-only or rarely updated data, or where full-text index support is required. The choice of storage engine should be determined based on the application's needs and performance characteristics.
④ARCHIVE
ARCHIVE
It is a special storage engine in the MySQL database. Different from InnoDB or MyISAM, the storage engine is mainly designed to achieve high compression rate and fast insertion, and is suitable for data archiving and storage scenarios.ARCHIVE
The following are some characteristics and applicable scenarios of storage engines:
ARCHIVE
High compression rate: The storage engine uses the zlib compression algorithm, so it is very efficient for storing large amounts of similar data. This allows it to significantly reduce storage space when archiving large amounts of historical data.
ARCHIVE
Only supports INSERT and SELECT operations: The storage engine is mainly used to insert and retrieve large amounts of data. It does not support UPDATE, DELETE or other complex operations, so it is suitable for scenarios where a large amount of data is inserted at once and only queried later.
ARCHIVE
No index support: The storage engine does not support ordinary B-tree indexes, which means queries are slower. It is mainly suitable for archiving scenarios that require large amounts of historical data rather than frequent retrieval.
ARCHIVE
Suitable for log and archive data: Due to its high compression rate and read-only characteristics, the storage engine is suitable for storing log data or archive historical data.
ARCHIVE
It should be noted that due to the characteristics of the storage engine, it is not suitable for all types of applications. When selecting a storage engine, factors such as the application's read and write mode, data volume, and query requirements should be considered. For data tables that are frequently read and updated, other storage engines such as InnoDB are often chosen.
ARCHIVE
2. Database management
Metadatabase usually refers to a database in a database that stores information about other databases in the database and the relationships between them. This type of database is typically used to manage and store metadata about other database objects (such as tables, columns, indexes, etc.). The existence of the metabase enables users and applications to query and understand the data structures and relationships stored in the database, thereby managing and operating the database more effectively. A database that records MySQL's own data
2.1. Three major databases
2.1.1.information_schema
- Purpose: It is a database that contains metadata information. It provides information about the database structure and objects, allowing users to query and understand metadata information such as the structure, tables, columns, indexes, etc. of the database.
information_schema
- Content: Contains a series of tables, such as (database), (table), (column), (statistics information), (key column usage), etc. These tables store metadata information about the database and its objects.
SCHEMATA
TABLES
COLUMNS
STATISTICS
KEY_COLUMN_USAGE
- Usage examples: Users can query to obtain tables, columns, indexes, constraints and other information in the database for database management, query optimization and design.
information_schema
2.1.2.mysql
- Purpose: It is a database that stores MySQL server configuration and authorization information, including user accounts, permissions, global variables and other MySQL server configuration and security-related information.
mysql
- Content: Contains tables such as users, passwords, authorization information, global variables, such as , , , etc., used to store access rights and server configuration.
user
db
tables_priv
columns_priv
- Usage examples: Administrators can manage user permissions, modify global variable settings, and control the security and behavior of the database by operating on the database.
mysql
2.1.3.performance_schema
- Purpose: It is a database used for performance monitoring and diagnosis. It provides detailed information about the performance and activities of the database server, allowing users to query and analyze the performance characteristics of the database.
performance_schema
- Content: Contains a series of tables used to monitor database performance, such as , , etc. These tables provide performance-related information about statement execution, locks, file I/O, etc.
events_statements_summary_by_digest
mutex_instances
file_summary_by_event_name
- Usage examples: Database administrators and developers can query tables in to understand the performance of the database server, identify potential performance issues, and perform performance tuning and optimization.
performance_schema
Used to access the metadata information of the database, used to manage the configuration and security of the MySQL server, and used to monitor and diagnose the performance of the database server. These three databases provide important information and control of the MySQL database system at different levels.
information_schema mysql
performance_schema
2.2.Related operations
Use database:
use database name
Create database:
create data base if not exists database name default charset utf8 collate utf8_general_ci; (complete)
Query the database:
show databases;
Delete database:
drop database database name;
2.3. Tables related to permissions
mysql.user
Table: (User level permissions)
- Stores basic information of database users, including username, host, password, etc.
- Used to manage user authentication information.
mysql.db
Table: (Database layer permissions)
- Record database-level permission information, including which user has permission to access which database.
mysql.tables_priv
Table: (surface permissions)
- Store table-level permission information, including which user has permission to operate on which table (such as SELECT, INSERT, UPDATE, DELETE).
mysql.columns_priv
Table: (field level permissions)
- Record column-level permission information, including which users have permission to access which columns of the table.
3. Database table management
3.1. Three major paradigms
The three major paradigms (Normal Forms) in database design are a set of specifications used to ensure that the structure of database tables has optimal organization and correlation to reduce data redundancy and improve data consistency.
First paradigm: columns cannot be subdivided (atomicity). For example: Detailed address: Building 27, Qingshui, Longqinwan, Yuelu District, Changsha City, Hunan Province
- Each column in a data table contains atomic values and cannot be broken down into smaller units of data.
- Make sure each cell contains only one value, not a list or collection of multiple values.
Second normal form: primary key constraints, for example: order item table design: id, unit price quantity, subtotal
- The data table must conform to the first normal form, and the non-primary key columns in the table completely depend on the entire primary key, rather than just a part of the primary key.
- Mainly solve the problem of partial dependence.
Third normal form: foreign key constraints. For example: patient ID and patient name both exist in a related table.
- The data table must conform to the second normal form, and there cannot be transitive dependencies between non-primary key columns in the table.
- Mainly solves the problem of transitive dependencies.
3.2.Basic data types
Basic data types in databases usually include numeric, character, date/time, etc. Different database systems may support slightly different
integer | tinyint8 bits (-128~127) |
smallint16 bits (-32768~32767) | |
mediumint24-bit (-8388608~8388607) | |
int32 bit is approximately positive 2.1 billion | |
bigint64 bit |
Real numbers (with decimal point) | float4 bytes |
double8 bytes | |
decimal allows up to 65 numbers |
string | Char | Fixed length, MySQL allocates enough space at one time according to the length of the defined string |
Applicable scenarios: shorter strings, and all values are close to the same length | ||
varchar | Saves space compared to fixed-length types | |
Applicable scenarios: The longest length of the string is much larger than the evaluation length, and the columns are updated less frequently | ||
Disadvantages: When modifications occur frequently and the length of the string changes greatly, page splits may occur. |
text&blob | text stores character data | tinytext |
smalltext | ||
mediumtext | ||
text | ||
longtext | ||
blob stores binary data | tinyblob | |
smallblob | ||
mediumblob | ||
blob | ||
lungblob |
date time | datatime | Accuracy: seconds |
Time zone independent, 8 bytes of storage space | ||
Range: 1001 to 9999 | ||
timestamp | Saves the number of seconds since midnight on January 1, 1970 | |
Occupies 4 bytes of storage space | ||
Range: 1970 to 2038 | ||
related to time zone | ||
Default is NOTNULL | ||
Usually try to use timestamp | ||
Accuracy: seconds | ||
data | yyyy:MM:dd | |
time | HH:mm:ss |
Check identifier | Used to perform association operations |
as foreign key in other table | |
-Integers are usually the best choice for identity columns | |
- Use the same data type in related tables | |
-Try to avoid strings as identification columns, especially randomly generated strings (such as: UUID), which will cause insert and select to be very slow-----------The inserted value is randomly written to different positions in the index , insert is slow and can easily lead to page splits and random disk reading. Logically adjacent rows are distributed in different places on the disk and memory, and the select is slow. Is the MySQL query cache invalid? If UUID needs to be stored, the "-" should be removed |
4. Account management
Query users:
SELECT*from user
View common user information:
select host,user from user,
Delete user (use with caution):
#Command: dropuser username;
#drop user Is;
Permission operation
Setting permissions:
- Role assignment: Typically, permissions are managed through roles. First, define different roles and assign corresponding permissions to these roles.
- Assign permissions directly: In some cases, you may need to grant permissions directly to specific users, without going through roles. Such direct assignments may be temporary or required by special circumstances.
#语法: grantprvilegesondatabasename.tablenametousername@'host;
#Give the zs user the query permission for the table t_p1_user in the database db_xiaoli
grant SELECT on db_xiaoli.t_p1_user to zs@%;
#Give the zs user permission to modify the table t_p1_user in the database db_xiaoli
grant UPDATE on db_xiaoli.t_p1_user to zs@'%'
#Give the zs user query permissions on all tables in the database db_xiaoli
grant SELECT on db_xiaoli.*to zs@'%';
#Give the zs user all permissions on all tables in the database db_xiaoli
grant ALL on db_xiaoli.*to zs@%';
Revoke permission:
- Remove from role: Administrators can remove certain permissions from a user's role if they no longer need them.
- Directly revoke permissions: For directly assigned permissions, administrators can directly revoke specific permissions granted to users.
View user permissions:
- Role View: Administrators can often view the roles assigned to a user to understand the permissions the user has.
- View the permission list: Administrators can view the list of permissions granted directly to the user to understand their access rights in more detail.
- Audit log: The system may record the user's permission usage. Administrators can use the audit log to check the user's activities and permission change history.
#Command:showgrantsforusername
show grantssfor zs'@%
5. Mind map
Okay, that’s it for today! ! ! Hope this helps! ! !