03_Basic operation of MySQL database

1. Create a database

After MySQL is installed, you first need to create a database, which is a prerequisite for using MySQL's various functions. This chapter will introduce the basic operations of the database in detail. The main contents include: creating a database, deleting a database, different types of database storage engines and the selection of storage engines.
After MySQL is installed, several necessary databases will be automatically created in its data directory. You can use the'show databases;' statement to view all existing databases as follows:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| mytest             |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

As you can see, the database list contains 4 databases, mysql is required, it describes user access permissions, users often use the mytest database to do test work, other databases will be introduced in the following chapters.
Creating a database is to divide an area on the system disk for database storage and management. If the administrator creates a database for the user when setting permissions, it can be used directly, otherwise, you need to create the database yourself.
//The basic sql statement format for creating a database in MySQL is:

CREATE DATABASE [IF NOT EXISTS] <数据库名> [[DEFAULT] CHARACTER SET <
字符集名>] [[DEFAULT] COLLATE <校对规则名>];

Explanation:

  • <database name>: The name of the database to be created. The MySQL database storage will represent the MySQL database in a target manner, so the database name must conform to the file name rules of the operating system. Note that case insensitive in MySQL.
  • IF NOT EXISTS: Make a judgment before creating a database, and perform operations only when the database does not currently exist. This option can be used to avoid errors caused by repeated creation of the database that already exists.
  • [DEFAULT] CHARACTER SET: Specify the database default character set.
mysql> create database db_test1 character set gb2312;  //字符集为gb2312
Query OK, 1 row affected (0.01 sec)

mysql> show create database db_test1;          //查看数据库字符集
+----------+---------------------------------------------------------------------+
| Database | Create Database                                                     |
+----------+---------------------------------------------------------------------+
| db_test1 | CREATE DATABASE `db_test1` /*!40100 DEFAULT CHARACTER SET gb2312 */ |
+----------+---------------------------------------------------------------------+
1 row in set (0.00 sec)

2. Delete the database

Deleting a database is to clear the existing database from the disk space. After clearing, all the data in the database will also be deleted. The delete database statement is similar to the command to create a database. The basic syntax format for deleting a database in MySQL is:

DROP DATABASE database_name;

Explanation: "database_name" is the name of the database to be deleted. If the specified database does not exist, an
error will be deleted .

mysql> drop database db_test1;
Query OK, 0 rows affected (0.00 sec)

3.1 Database storage engine

The database storage engine is the underlying software component of the database. The database management system (DBMS) uses the data engine to create, query, update, and delete data. Different storage engines provide different storage mechanisms, indexing techniques, locking levels and other functions. Using different storage engines to provide specific functions can also be obtained. Many different database management systems now support a variety of different data engines. The core of MySQL is the storage engine.

3.2 Introduction to MySQL Storage Engine

MySQL provides a number of different storage engines, including an engine that handles transaction-safe tables and an engine that handles non-transaction-safe tables. In MySQL, there is no need to use one engine in the entire server, and a different storage engine can be used for each table for specific requirements. The storage engines supported by MySQL5.5 are: InnoDB, MyISAM Memory, etc.
//View the command of the engine:

mysql> show engines\g
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

//View the default storage engine

mysql> show variables like 'default_storage_engine';
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| default_storage_engine | InnoDB |
+------------------------+--------+
1 row in set (0.01 sec)


1. Introduction to storage engine

  1. The storage engine in white is the format of data storage. Different storage engines have different functions, occupy different amounts of space, and have different read performances.
  2. The database storage engine is the underlying software component of the database. Different storage engines provide different storage mechanisms.
  3. In MySQL, there is no need to use the same storage engine in the entire server, you can use a different storage engine for each table
  4. MySQL supports multiple storage engines, such as InnoDB, MyISAM, Memory, Merge, Archive, CSV, Federated, etc.

Features of MyISAM storage engine:
5. MyISAM engine is used before MySQL 5.5, and InnoDB engine is used after MySQL 5.5
. 6. MyISAM engine reads faster, occupies relatively less resources, does not support transactions, and does not support foreign key constraints , But supports full-text index
7. Reading and writing block each other, which means you cannot write data when reading data
, and you cannot read data when writing data. 8. MyISAM engine can only cache indexes, not data

MyISAM applicable scenarios:

  1. Services that do not require transaction support, such as money transfers
  2. It is suitable for the business with more data reading, not suitable for the business with frequent reading and writing
  3. Businesses with relatively low concurrency and relatively few data modifications
  4. Machines with poor hardware resources can consider using the MyISAM engine

InnoDB storage engine features:

  1. The engine of choice for transactional databases, supports transaction-safe tables, supports locks and foreign keys,
    after MySQL 5.5.5, InnoDB is the default storage engine
  2. A transaction-safe storage engine with commit, rollback, and crash recovery capabilities, capable of handling huge amounts of data, with
    high performance and efficiency, and fully supporting foreign key integrity constraints
  3. It has very efficient caching features, which can cache indexes and data, which has higher requirements on hardware
  4. When using InnoDB, a 10MB auto-expanded data file named ibdata1, and two 5MB log files named ib_logfile0 and ib_logfile1 will be created in the MySQL data directory

InnoDB application scenarios:

  1. Services that require transaction support, and highly concurrent services
  2. Scenes where data is updated frequently, such as BBS, SNS, Weibo, etc.
  3. Services that require high data consistency, such as recharge transfers, bank card transfers

Memory storage engine features:

1. The Memory storage engine stores the data in the table in the memory to provide quick access for querying and referencing other table data
. 2. The Memory storage engine executes HASH and BTREE indexes, does not support BLOB and TEXT columns, but supports AUTO_INCREMENT columns And for indexes that can contain NULL value columns
3. When the contents of the Memory table are no longer needed, to release the memory used by the Memory table, you should execute DELETE FROM or TRUNCATE TABLE, or delete the entire table

Second, the choice of storage engine

  1. If you want to provide transaction security capabilities such as commit, rollback, and crash recovery capabilities, and require concurrency control
    , InnoDB is a good choice
  2. If the data table is mainly used to insert and query records, the MyISAM engine can provide higher processing efficiency
  3. If you only store data temporarily, the amount of data is not large, and you do not need high security, you can choose
    the Memory engine that saves the data in memory, and MySQL uses this engine as a temporary table to store
    the intermediate results of the query
  4. If there are only INSERT and SELECT operations, you can select Archive engine to support highly concurrent insert
    operations, such as logging log information, you can use Archive engine

Expansion:
In mysql, each database can create up to 2 billion tables, one table allows to define 1024 columns, and the maximum size of each row is 8092 bytes (not including the size of text and image types). When a varchar, nvarchar, or varbinary type column is defined in the table, if the data inserted into the table exceeds 8092 bytes, the statement will fail and an error message will be generated. SQL Server does not directly limit the number of rows in each table, but it is limited by the storage space of the database. The maximum space of each database is 1048516TB, so the maximum space available for a table is 1048516TB minus the space occupied by database system tables and other database objects. Theoretically, the size is not limited, depending on whether your hard drive is large enough. In most cases, your hard drive is not enough.

Guess you like

Origin blog.csdn.net/weixin_45310323/article/details/109982697