The most complete knowledge of MySQL database + interview questions finishing

Databases, especially relational databases, are the most commonly used by programmers. For a good engineer, only CRUD is not enough. You also need to understand its principles. Understanding its principles can enable programmers to write more reliable and efficient code and improve their ability to solve practical problems. This article will use MySQL as For example, learn database principles with everyone.

Database basics

1. Why use a database

Data is stored in memory

Advantages: fast access speed
Disadvantages: data cannot be stored permanently

Data is saved in file

Advantages: data is stored permanently.
Disadvantages: 1) The speed is slower than memory operations, frequent IO operations 2) Inconvenient data query

Data is saved in the database

1) Data is stored permanently 2) SQL statement is used for convenient and efficient query 3) Data management is convenient

2. What is SQL?

Structured Query Language (Structured Query Language) referred to as SQL, is a database query language. Role: used to access data, query, update and manage relational database systems.

3. What is MySQL?

MySQL is a relational database management system developed by the Swedish company MySQL AB and is a product of Oracle. MySQL is one of the most popular relational database management systems. In terms of web applications, MySQL is one of the best RDBMS (Relational Database Management System) application software. It is very commonly used in Java enterprise-level development, because MySQL is open source and free, and it is easy to expand.

  1. What are the three database paradigms
  2. What are the mysql tables about permissions
  3. How many input formats does MySQL binlog have? What are the differences?

Due to limited space, a part is omitted here. Friends who need the full version can click the link below

Link: 1103806531 Password: CSDN

type of data

1. Integer type

Including TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT, which respectively represent 1 byte, 2 bytes, 3 bytes, 4 bytes, and 8 bytes integers. Any integer type can be added with the UNSIGNED attribute, indicating that the data is unsigned, that is, a non-negative integer.
Length: Integer type can be specified length, for example: INT(11) means INT type with length 11. Length is meaningless in most scenarios. It does not limit the legal range of values, but only affects the number of displayed characters, and it is meaningful only when it is used in conjunction with the UNSIGNED ZEROFILL attribute.
For example, suppose the type is set to INT(5) and the attribute is UNSIGNED ZEROFILL. If the data inserted by the user is 12, the actual data stored in the database is 00012.

2. Real number type

Including FLOAT, DOUBLE, DECIMAL.
DECIMAL can be used to store integers larger than BIGINT and can store precise decimals.
FLOAT and DOUBLE have a range of values, and support the use of standard floating point for approximate calculations.
FLOAT and DOUBLE are more efficient than DECIMAL when calculating. DECIMAL can be understood as a string for processing.

3. String type

Including VARCHAR, CHAR, TEXT, BLOB
VARCHAR is used to store variable-length character strings, it saves more space than fixed-length types.
VARCHAR uses an extra 1 or 2 bytes to store the length of the string. When the column length is less than 255 bytes, use 1 byte for representation, otherwise use 2 bytes for representation.
When the content stored in VARCHAR exceeds the set length, the content will be truncated.
CHAR is fixed-length, and sufficient space is allocated according to the defined string length.
CHAR will be filled with spaces as needed to facilitate comparison.
CHAR is suitable for storing very short strings, or all values ​​are close to the same length.
When the content stored in CHAR exceeds the set length, the content will also be truncated.

Usage strategy:
CHAR is better than VARCHAR for frequently changing data, because CHAR is not prone to fragmentation.
For very short columns, CHAR is more efficient in storage space than VARCHAR.
Pay attention to allocate only the space needed when using it. Longer columns will consume more memory when sorting.
Try to avoid using TEXT/BLOB types. Temporary tables will be used when querying, resulting in serious performance overhead.

4. Enumeration type (ENUM)

Store unique data as a predefined collection.
Sometimes you can use ENUM instead of commonly used string types.
ENUM storage is very compact and will compress the list value to one or two bytes.
When ENUM is stored internally, it actually stores an integer.
Try to avoid using numbers as constants in ENUM enumerations, because it is easy to get confused.
Sorting is according to the internally stored integer

5. Date and time type

Try to use timestamp. The space efficiency is higher than datetime. It
is usually inconvenient to store timestamps in integers.
If you need to store subtle, you can use bigint storage.
Seeing this, this real question will be easier to answer.

engine

  1. The difference between MySQL storage engine MyISAM and InnoDB
  2. The difference between MyISAM and InnoDB
  3. What is the difference between MyISAM index and InnoDB index?
  4. 4 major features of InnoDB engine
  5. Storage engine selection

index

  1. What is an index?
  2. What are the advantages and disadvantages of indexes?
  3. Index usage scenarios (emphasis)
  4. What are the types of indexes?
  5. Index data structure (b tree, hash)
  6. Basic principles of indexing

At last

Hope this article is helpful to everyone!

Due to limited space, transactions, locks, views, stored procedures and functions, triggers, commonly used SQL statements, SQL optimization, database optimization, etc. are not shown in this article, but I have compiled them into documents, including architects. A full set of video tutorials and systematic materials about java, friends in need can click the link below to receive

Link: 1103806531 Password: CSDN

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_48655626/article/details/108711452