Mysql important basic concepts

Mysql storage engine

Insert picture description here
The storage engine is the storage method of the tables at the bottom of the database. All storage engines implement the required functions according to specific needs (such as our car engines, which are fuel-intensive, fast, and powerful, etc.). All of the choices lead to the effect of driving)

The presentation layer faces the user, while the logic layer faces the machine.

Insert picture description here

The bottom layer of the database relies on the file system for storage, and all database records are stored in files

System view the engine that comes with mysql

Insert picture description here

  • InnoDB is the default engine of mysql
  • Transactions: Whether to support transactions
  • XA: Whether to support distributed transactions
  • Savepoints: Whether to support the rollback point of the transaction

3 common engines

MyISAM engine
-does not support transactions

MyISAM uses 3 files to organize a table

  • *.frm table definition, a file that describes the structure of the table
  • *.MYD saves the data record data information file of the table, which is the data file of the table
  • *.MYI index information file is the data tree of any index in the table data file
  • *.opt records database options, database character set settings

Advantages: support compression, save space, can be converted into read-only tables, improve retrieval efficiency
Disadvantages: do not support transactions

InnoDB engine
-advantages: support transactions, row-level locks, foreign keys, etc. InnoDB engine has the best data security

Table structure is stored in *.frm file.
Data and index are stored in tablespace such as tablespace (logical concept, in fact, it is still stored in file), which cannot be compressed and cannot be converted to read-only table

  • Shared table space: All table data of a certain database, index files are all placed in one file, the default file name is: ibdata1

  • Independent table space: Each table will be generated and stored as an independent file. Each table has a .frm table description file and an .ibd file.
    Among them, this file includes the data content and index content of a single table. By default, its storage location is also in the location of the table.

    show variables like "innodb_file_per_table";
    ON代表独立表空间管理,OFF代表共享表空间管理
    

InnoDB engine provides automatic recovery mechanism after database crash.
InnoDB engine supports cascade delete and cascade update (deleting one table and cascading will delete another table)

MEMORY engine

  • Advantages: the fastest query speed, because the data is stored in memory
  • Disadvantages: does not support transactions, power failure will cause data loss

Database table structure

Table composition

  • Column: field
  • Row: record/data

The structure refers to the field, and the record is generated according to the type defined by the field

MySQL data type

The data type is used to specify the rules for the data contained in a specific field. It determines the way the data is stored in the field, including the width assigned to the field, and whether the value can be letters, numbers, date and time, etc.

  • String type (CHAR (0-255 fixed length), VARCHAR (0-255 variable length)
  • Numerical type (INT (integer type), FLOAT (floating point))
  • Date and time type (DATE (year, month, day), TIME (hour, minute, second))

MySQL constraint types

Constraint is a kind of restriction, which ensures the integrity and uniqueness of the data in the table by restricting the data in the rows or columns of the table

  • Primary key constraint (Primary key): a combination of unique constraint + non-empty constraint, multiple field combinations can be set (not recommended), a table can only have one primary key
    • Role: to determine the unique identifier of the record
  • Foreign key constraint (foreign key): A foreign key is a primary key or a unique reference to another table, that is, the foreign key value must be the value in the referenced field. The table set as the foreign key is called the child table, and the referenced table is called The parent table, there is a relationship between subordinate and subordinate
    • Role: to ensure the consistency and legality of data and to achieve some cascading operations
  • Unique constraint (unique): The record inserted under the field cannot be repeated, but null can be repeated, and multiple field combinations can be set
  • Not null constraint (not null): the record under the field cannot be empty
  • Default constraint (default): insert the default value of the record (when the value is not specified)

Foreign key constraint

Primary key constraint

1. Add a primary key constraint

id int primary key;
#列级字段会面加上primary key

primary key(字段)
#表级主键方式

2. Primary key classification

Divided according to the number of fields of the primary key:

  • Single primary key: single field (recommended)
  • Composite primary key: combination of multiple fields (not recommended, contrary to the three paradigm)

Divided according to the nature of the primary key:

  • Natural primary key: The primary key value is preferably a natural number that has no relationship with the business (recommended)
  • Business primary key: The primary key value is linked to the business system (identity number, card number...), it is not recommended, because when the primary key field business changes, the entire table will change, and the change may cause the primary key value to be the same

3. Auto-increment of the primary key

The natural primary key is a natural number, then self-increment can be achieved without manual maintenance

id int primary key auto_increment;
#字段会面加auto_increment

Foreign key constraint

The value of the field set as a foreign key constraint cannot be written casually, it can only be the field value of another table that is referenced

1. The foreign key value can be null
2. The foreign key field refers to other table fields, and the referenced field can be a primary key constraint or a unique constraint (usually a primary key constraint)
3. A foreign key constraint sql

foreign key(字段) references 表(字段)
#中文标注的都是可以改变的

Mysql transaction

  • A transaction is a complete business logic unit and cannot be divided. That is, the execution of multiple sql statements either succeeds or fails, and one succeeds and one fails is not allowed.
  • Only DML statements are related to transactions and support (insert delete update)
  • Role: to ensure data integrity and security
  • There is no need for transaction mechanism for a DML statement

Transaction principle

1. Start the transaction
2. The executed statement is first stored in the recording operation (cache), without changing the hard disk file data
3. You can commit the transaction or roll back the transaction (commit and rollback will end the transaction, clear the record operation), submit After a one-time execution of the sql statement to change the data on the real hard disk

Submit statement: commit (sql default semicolon end is a commit)
rollback statement: rollback

The characteristics of the transaction

  • Atomicity: the smallest unit of work, cannot be subdivided
  • Consistency: The execution of multiple DML statements either succeeds or fails
  • Isolation: there is isolation between transaction A and transaction B
  • Persistence: the data is persisted to the hard disk before the end of the transaction

The isolation level of the transaction (theoretically divided into 4 levels)

Level 1: Read uncommitted, I can read if the other party's transaction is not committed

  • There are problems: dirty data, data records are unstable because they are not submitted

Level 2: Read has been submitted, I can read after the other party's transaction is submitted

  • Resolved dirty data
  • There is a problem: it cannot be read repeatedly, because I read different data every time the other party submits it

The third level: repeatable read, mysql default level

  • Solved non-repeatable read
  • There is a problem: the data read may not be true

The fourth level: transaction queuing

  • Solved all problems
  • There are problems: low efficiency, transactions need to be queued

index

The index is similar to the catalog of a book, and the corresponding resources can be quickly found through the catalog

1. When to add an index

Indexes have maintenance costs. After data changes, the indexes need to be reordered for maintenance

  1. Huge amount of data
  2. Less DML operations in this field
  3. This field often appears in the where clause

Primary key and unique constraints automatically add indexes

2. Create and delete indexes

create index 索引名 on 表名(字段名)
drop index 索引名 on 表名
#通过expain sql语句查看执行计划的type和rous变化

Three paradigms of database design

Tables designed according to the three paradigms will not have data redundancy

The first paradigm: any table must have a primary key, and the fields cannot be divided. The
second paradigm: based on the first paradigm, all non-primary key fields are completely dependent on the primary key, and cannot be partially dependent (the field combination cannot be set)
. Zhang table, relational table with 2 foreign keys
Third paradigm: Based on the second paradigm, all non-primary key fields directly depend on the primary key, and cannot be transitively dependent. One
-to-many, two tables, multiple tables plus foreign keys

In practice, redundancy can be exchanged for execution speed. More table links will result in slower execution

Guess you like

Origin blog.csdn.net/yangshihuz/article/details/108826929