30 super classic MySQL database interview questions, are you sure not to come and have a look?

Databases are divided into relational databases and non-relational databases , which are what we often call SQL and NoSQL. The representative products of the databases in these two directions are MySQL and Redis. This time we mainly use the form of interview questions and answers to learn about the relationship Basic knowledge of MySQL database.

The interview begins, prepare to accept the interviewer's soul torture!

 

Relational Database

What is a relational database?

A relational database refers to a database that uses a relational model to organize data. It stores data in the form of rows and columns for easy understanding by users. The series of rows and columns in a relational database are called tables, which consist of a group of tables. The database. The user retrieves the data in the database by query, and the query is an execution code used to limit certain areas in the database.

Simply put, the relational model is a two-dimensional table model.

What are the advantages of relational databases?

Advantages of relational databases:

  • Easy to understand

    The structure of the relational two-dimensional table is very close to the real world, and the two-dimensional table is easy to understand.

  • Support for complex queries. SQL statements can be used to easily perform very complex data queries between a table and multiple tables.

  • Support the reliable transaction processing and maintain the integrity of the transaction, so that the data access requirements with high security performance can be realized.

MySQL database

What is SQL

Structured Query Language (Structured Query Language), abbreviated as SQL, is a special purpose programming language, a database query and programming language programming language, used to access data and query, update and manage relational database systems.

What is MySQL?

MySQL is a relational database management system. MySQL is one of the most popular relational database management systems. Common relational databases include Oracle, SQL Server, Access and so on.

MySQL has become the most popular open source database in the past due to its high performance, low cost, and good reliability, and it has been widely used in small and medium-sized websites on the Internet .

 

MySQL and MariaDB are stupidly unclear?

MySQL was originally developed by the Swedish company MySQL AB. The founder of MySQL is Ulf Mikhail Videnius, commonly nicknamed Monty.

After being acquired by Oracle, it is now a product of Oracle. Oracle substantially increased the price of MySQL commercial edition, which caused the free software community to worry about whether Oracle will continue to support MySQL community edition.

The founder of MySQL is the former boss named Monty who established a branch plan MariaDB based on MySQL.

MariaDB intends to maintain a high degree of compatibility with MySQL, ensuring that it has the direct replacement function of the library binary parity, and the exact matching of the MySQL API (application program interface) and commands. The original open source software that used MySQL gradually turned to MariaDB or other databases.

So if you see that your company uses MariaDB, don't doubt it. In fact, it is still MySQL. If you learn MySQL, you will get MariaDB.

An easter egg

MariaDB is named after Monty's youngest daughter Maria, just like MySQL is named after his other daughter My, two famous databases are named after two daughters, your uncle is still your uncle, the old man is awesome! 

How to check the current version number of MySQL?

Under the system command line:mysql -V

Connect to the MySQL command line and enter:

> status;

Server:   MySQL
Server version:  5.5.45
Protocol version: 10

or select version();

+------------------------+
| version()              |
+------------------------+
| 5.5.45-xxxxx |
+------------------------+

Basic data type

What data types does MySQL have?

MySQL data types are very rich. Common types are briefly introduced as follows:

Integer type:BIT、BOOL、TINY INT、SMALL INT、MEDIUM INT、 INT、 BIG INT

Floating point type:FLOAT、DOUBLE、DECIMAL

String type:CHAR、VARCHAR、TINY TEXT、TEXT、MEDIUM TEXT、LONGTEXT、TINY BLOB、BLOB、MEDIUM BLOB、LONG BLOB

Date type:Date、DateTime、TimeStamp、Time、Year

Other data types: BINARY、VARBINARY、ENUM、SET...

What is the difference between CHAR and VARCHAR?

CHAR is a fixed-length character type, and VARCHAR is a variable-length character type . The following discussion is based on MySQL 5.0 and above.

Common ground

Both CHAR(M) and VARCHAR(M) indicate that the column can store M characters , note that it is not a byte! !

CHAR type characteristics

  • CHAR can store up to 255 characters (note that it is not a byte) , and characters have different encoding sets, such as UTF8 encoding (3 bytes), GBK encoding (2 bytes), etc.

  • For  CHAR(M) if the length of the actual data stored is less than M, then MySQL will automatically make up a space character to its right, but those out of the fill space character is removed in the retrieval operation.

VARCHAR type characteristics

  • The maximum length of VARCHAR is 65535 bytes .

  • VARCHAR stores the actual string plus 1 or 2 bytes to record the actual length of the string. If the string length is less than 255 bytes, 1 byte is used to record, and if it exceeds 255, 2 bytes are required to record. [^12]

How many Chinese characters can be stored in VARCHAR(50)?

The number of Chinese characters stored is related to the version.

For versions below mysql 4.0, varchar(50) refers to 50  bytes . When storing Chinese characters encoded in UTF8 format (3 bytes per Chinese character), only 16 characters can be stored.

In mysql 5.0 and above, varchar(50) refers to 50  characters , no matter whether it is stored in numbers, letters or UTF8 encoded Chinese characters, 50 characters can be stored.

Can int(10) and bigint(10) have the same data size?

The specific reasons are as follows:

  • int can store four-byte signed integers.

  • bigint can store eight-byte signed integers.

Therefore, the size of the data that can be stored is different, and the numbers in it  10  represent only the display width of the data. [^13]

  • The display width indicates the maximum possible number of numbers displayed by Mysql. When the number of digits is less than the specified width, the left side of the number will be filled with spaces , which is not easy to see.

  • If a value larger than the display width is inserted, as long as the value does not exceed the value range of the type, the value can still be inserted and displayed.

  • When you specify the zerofill option when building the table  , the part that is less than the display width will be  0 filled. If it is 1, it will be displayed  0000000001.

  • If the display width is not specified, the default width of bigint is 20 and the default width of int is 11.

Storage engine related

What are the MySQL storage engine types?

Commonly used storage engines include InnoDB storage engine and MyISAM storage engine. InnoDB is MySQL's default transaction engine.

To view the currently supported engines of the database table, you can use the following query statement to view:

# 查询结果表中的 Engine 字段指示存储引擎类型。
show table status from 'your_db_name' where name='your_table_name'; 

What is the application scenario of InnoDB storage engine?

InnoDB is MySQL's default "transaction engine" and is set to handle a large number of short-lived transactions. Most of the short-lived transactions are committed normally and rarely rolled back.

What are the characteristics of the InnoDB storage engine?

MultiVersion Concurrency Control (MVCC) is used to support high concurrency. It also implements four standard isolation levels, and next-key lockingprevents the appearance of phantom reading through the gap lock strategy.

The engine table is built based on a clustered index, which has high performance for primary key queries. However, its secondary index secondary indexnon-primary key index must contain the primary key column, so if the primary key column is large, all other indexes will be large. Therefore, if there are more indexes on the table, the primary key should be as small as possible. In addition, the storage format of InnoDB is platform independent.

InnoDB has made a lot of optimizations, such as: predictable read-ahead for disk read data, automatic creation of hash indexes in memory to speed up read operations, and adaptive hash indexes that can speed up insert operations. Insert buffer (insert buffer) etc.

InnoDB supports true hot backup through some mechanisms and tools. Other MySQL storage engines do not support hot backup. To obtain a consistent view, you need to stop writing to all tables. In a mixed read-write scenario, stopping writing may also mean Stop reading.

What are the four major features of the InnoDB engine?

Insert buffer

Insert Buffer is used for insert and update operations of non-clustered index. First determine whether the inserted non-clustered index is in the buffer pool, if it is, insert it directly, otherwise insert it into the Insert Buffer object. Then perform the merge operation of Insert Buffer and auxiliary index leaf nodes at a certain frequency to merge multiple inserts into one operation to improve the insertion performance of the non-clustered index.

Double write

Double Write consists of two parts, one is the double write buffer in the memory, the size is 2MB, and the other is the continuous 128 pages of the shared table space on the physical disk, and the size is also 2MB. When refreshing the dirty pages of the buffer pool, the dirty pages are not directly written to the disk, but the dirty pages are first copied to the area in the memory through the memcpy function, and then divided into two times through the doublewrite buffer, each time 1MB is written sequentially On the physical disk of the shared table space, then immediately call the fsync function to synchronize the disk to avoid problems caused by the operating system buffer write.

Adaptive Hash Index (Adaptive Hash Index)

InnoDB will build a hash index for hot pages according to the frequency and mode of access to improve query efficiency. The index is constructed by the B+ tree page of the buffer pool, so the establishment speed is very fast. The InnoDB storage engine will monitor the query of each index page on the table. If it is observed that the establishment of a hash index can bring a speed improvement, then the establishment of Ha Greek index, so it is called adaptive hash index.

Buffer pool

In order to improve the performance of the database, the concept of the buffer pool is introduced. The parameter innodb_buffer_pool_size can set the size of the buffer pool, and the parameter innodb_buffer_pool_instances can set the number of instances of the buffer pool. The buffer pool is mainly used to store the following:

The types of data pages cached in the buffer pool are: index page, data page, undo page, insert buffer, adaptive hash index, lock info stored in InnoDB, and data dictionary information (data dictionary).

What are the application scenarios of MyISAM storage engine?

MyISAM is the default storage engine for MySQL 5.1 and earlier versions. MyISAM provides a large number of features, including full-text indexing, compression, spatial functions (GIS), etc., but MyISAM does not "support transaction and row-level locks". For read-only data, or tables that are small and can tolerate repair operations, they can still be used it.

What are the features of MyISAM storage engine?

MyISAM "does not support row-level locks but locks the entire table." When reading, a shared lock is added to all tables that need to be read, and when writing, an exclusive lock is added to the table. But while the table has read operations, you can also insert new records into the table, which is called concurrent insert.

MyISAM tables can be checked and repaired manually or automatically. But unlike transaction recovery and crash recovery, it may cause some "data loss", and the repair operation is very slow.

For MyISAM tables, even fields BLOBwith TEXTequal lengths can be indexed based on the first 500 characters. MyISAM also supports "full-text indexing", which is an index created based on word segmentation that can support complex queries.

If the DELAY_KEY_WRITEoption is specified , every time the modification is executed, the modified index data will not be written to the disk immediately, but will be written to the key buffer in the memory, only when the key buffer is cleared or the table is closed Write the corresponding index block to disk. This method can greatly improve the write performance, but when the database or the host crashes, it will cause "index damage" and repair operations need to be performed.

5 major differences between MyISAM and InnoDB storage engines

  • InnoDB supports things, but MyISAM does not support things

  • InnoDB supports row-level locks, while MyISAM supports table-level locks

  • InnoDB supports MVCC, but MyISAM does not

  • InnoDB supports foreign keys, but MyISAM does not

  • InnoDB does not support full-text indexing, while MyISAM supports

A table simply lists the main differences between the two engines, as shown below:

Which engine does SELECT COUNT(*) perform faster?

SELECT COUNT(*)   It is often used to count the total number of rows in a table. It is faster in the MyISAM storage engine, provided that no WHERE conditions can be added .

This is because MyISAM optimizes the number of rows in the table. It uses a variable to store the number of rows in the table. If the query condition does not have a WHERE condition, it means how many pieces of data are in the query table. MyISAM can quickly return the result. If WHERE is added The conditions will not work.

InnoDB tables also have a variable that stores the number of table rows, but this value is an estimated value, so it does not have much practical meaning.

MySQL basics

What are the three paradigms of database design?

1 Paradigm: 1NF is the atomic constraint on attributes, requiring attributes to be atomic and not decomposable; (as long as it is a relational database, 1NF is satisfied)

2 Paradigm: 2NF is a unique constraint on records, requiring records to be uniquely identified, that is, the uniqueness of entities;

3 Paradigm: 3NF is a constraint on the redundancy of fields, that is, any field cannot be derived from other fields, and it requires no redundancy in the field. No redundant database design can be done

However, a database without redundancy may not be the best database. Sometimes in order to improve operational efficiency, it is necessary to lower the paradigm standard and appropriately retain redundant data. The specific method is: to comply with the third paradigm and lower the paradigm standard when designing the conceptual data model The work is considered when designing the physical data model, and reducing the paradigm is to increase the fields and allow redundancy.

What are the classifications of SQL statements?

  1. DDL: Data Definition Language (create alter drop)

  2. DML: Data manipulation statement (insert update delete)

  3. DTL: data transaction statement (commit collback savapoint)

  4. DCL: data control statement (grant revoke)

What is the difference between delete, drop and truncate in database delete operation?

  • When the table is no longer needed, you can use drop to delete the table;

  • When you still want to keep the table, but want to delete all records, use truncate to delete the records in the table.

  • When you want to delete some records (generally there is a WHERE clause constraint) use delete to delete some records in the table.

What is a MySql view?

The view is a virtual table, and does not store data, only the dynamic data of the statement at the time of definition.

Create view syntax:

CREATE
    [OR REPLACE]
    [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
    [DEFINER = user]
    [SQL SECURITY { DEFINER | INVOKER }]
    VIEW view_name [(column_list)]
    AS select_statement
    [WITH [CASCADED | LOCAL] CHECK OPTION]

Parameter Description:

  • OR REPLACE: If the view exists, replace the existing view.

  • ALGORITHM: View selection algorithm, the default algorithm is UNDEFINED (undefined) and MySQL automatically selects the algorithm to be used.

  • DEFINER: Specify the view creator or definer. If this option is not specified, the user who created the view is the definer.

  • SQL SECURITY: SQL security, default is DEFINER.

  • select_statement: SELECT statement to create a view, you can select data from a base table or other views.

  • WITH CHECK OPTION: Indicates that the view is guaranteed to be constrained when it is updated. The default is CASCADED.

What are the advantages of using MySQL views?

  1. The operation is simple and convenient. View users do not need to care about the structure, association conditions and filtering conditions of the table corresponding to the view at all. For users, it is already a result set of filtered compound conditions.

  2. Data is more secure. View users can only access the result set in the view, and the view can restrict access to the table to certain rows and columns.

  3. Data isolation. The impact of changes in the source table structure on users is shielded, and the view structure remains unchanged when the source table structure changes. ^1

What is the default port number of the MySql service?

The default port is 3306

View port commands:> show variables like 'port';

How to filter multiple columns with DISTINCT?

DISTINCT is used to de-duplicate the selected data, and the single column usage is easy to understand. For example, the following data table  tamb:

   name        number
   Tencent      1
   Alibaba      2
   Bytedance    3
   Meituan      3

Query statement: The SELECT DISTINCT name FROM table tamb results are as follows:

   name       
   Tencent   
   Alibaba    
   Bytedance 
   Meituan  

If you want number to display by  column deduplication at the same time  name , you may write a query statement:

SELECT DISTINCT number, name FROM table tamb

The multi-parameter DISTINCT de-duplication rule is: treat all the parameters after DISTINCT as a filter condition, which means that the (number, name)overall de- duplication will be processed, and the duplicate will be  de-duplicated only when the combination is different . The results are as follows:

       number   name
         1  Tencent
         2  Alibaba
         3  Bytedance
         3  Meituan

From the results, it seems that the effect of de-duplication has not been achieved, so how can we achieve " number de- duplication in  columns and simultaneous display  name"? You can use the  Group By statement:

SELECT number, name FROM table tamb GROUP BY number The output is as follows, which is exactly what we want:

       number   name
         1  Tencent
         2  Alibaba
         3  Bytedance

What is a stored procedure?

A collection of one or more SQL statements has the following characteristics:

  • The stored procedure can achieve faster execution speed.

  • Stored procedures can be written with flow control statements, which have strong flexibility and can complete complex judgments and more complex calculations.

  • Stored procedures can be fully utilized as a security mechanism.

  • Stored procedures can reduce network traffic

delimiter 分隔符
create procedure|proc proc_name()
begin
    sql语句
end 分隔符
delimiter ;    --还原分隔符,为了不影响后面的语句的使用
--默认的分隔符是;但是为了能在整个存储过程中重用,因此一般需要自定义分隔符(除\外)

show procedure status like ""; --查询存储过程,可以不适用like进行过滤
drop procedure if exists;--删除存储过程

Stored procedures and functions seem to be similar, what is the difference between them?

Stored procedures and functions are a collection of SQL statements that have been compiled and stored in the database in advance. Calling stored procedures and functions can simplify a lot of work for application developers, reduce the transmission of data between the database and the application server, and improve data processing. The efficiency is good.

Same point

  • Stored procedures and functions are a collection of SQL statements for the repeatable execution of the database.

  • Stored procedures and functions are cached after being compiled once, and the compiled sql statement will be directly hit next time it is used, reducing network interaction and improving efficiency.

difference

  • The identifier is different, the identifier of the function is function, and the stored procedure is procedure.

  • The function returns a single value or table object, while the stored procedure does not return a value, but it can return multiple values ​​through OUT parameters.

  • There are many restrictions on functions. For example, temporary tables cannot be used, only table variables can be used, some functions are not available, etc., while the restrictions on stored procedures are relatively small.

  • Generally speaking, the function realized by the stored procedure is a little more complicated, and the function realization of the function is more targeted

  • The parameters of the function can only be of type IN, and the parameters of the stored procedure can be of IN OUT INOUTthree types.

  • Stored functions are called using select, and stored procedures need to be called using call.

in conclusion

This article summarizes a series of common basic knowledge points in interviews in the form of interview questions and answers. They are all very basic content, but the more basic, the more important they are. They can be used as knowledge points notes, often used to review the past and learn the new.

 

Guess you like

Origin blog.csdn.net/bjmsb/article/details/108503294