2020 MySQL database interview questions (50 questions with answers and mind map summary)

foreword

The knowledge points about MySQL summarizes a mind map to share with you, I hope it will be helpful to you!

1. What kinds of locks are there in MySQL?

(1) Table-level locks: small overhead, fast locking; no deadlocks; large locking granularity, the highest probability of lock conflicts, and the lowest concurrency.

(2) Row-level locks: high overhead and slow locking; deadlocks may occur; the locking granularity is the smallest, the probability of lock conflicts is the lowest, and the concurrency is the highest.

(3) Page lock: The overhead and locking time are between table locks and row locks; deadlocks will occur; the locking granularity is between table locks and row locks, and the concurrency is average.

2. What are the different tables in MySQL?

There are 5 types of tables:

1)MyISAM

(2)Heap

(3)Merge

(4)INNODB

(5)ISAM

3. Briefly describe the difference between MyISAM and InnoDB in the MySQL database

MyISAM:

(1) Transactions are not supported, but each query is atomic;

(2) Support table-level locks, that is, each operation locks the entire table;

(3) The total number of rows in the storage table;

(4) A MYISAM table has three files: index file, table structure file, and data file;

(5) Using the Philippine clustered index, the data field of the index file stores the pointer to the data file. The auxiliary index is basically the same as the main index, but the auxiliary index does not need to guarantee uniqueness.

InnoDb:

(1) Support ACID transactions, support four isolation levels of transactions;

(2) Support row-level locks and foreign key constraints: so it can support write concurrency;

(3) Do not store the total number of rows:

(4) An InnoDb engine is stored in a file space (shared table space, the table size is not controlled by the operating system, and a table may be distributed in multiple files), or it may be multiple (set as an independent table empty, the table size is affected by the The file size limit of the operating system, generally 2G), is limited by the file size of the operating system;

(5) The primary key index adopts a clustered index (the data field of the index stores the data file itself), and the data field of the secondary index stores the value of the primary key; therefore, to find data from the secondary index, you need to find the primary key value through the secondary index first, and then access the secondary index ; It is better to use the auto-increment primary key to prevent the large adjustment of the file in order to maintain the B+ tree structure when inserting data.

4. What are the names of the four transaction isolation levels supported by InnoDB in MySQL, and the differences between them? The four isolation levels defined by the SQL standard are:

(1) read uncommited: read uncommitted data

(2) read committed: dirty read, non-repeatable read

(3) repeatable read: rereadable

(4) serializable: serial things

5. What is the difference between CHAR and VARCHAR?

(1) CHAR and VARCHAR types are different in storage and retrieval

(2) The CHAR column length is fixed to the length declared when creating the table, and the length value ranges from 1 to 255. When CHAR values ​​are stored, they are filled with spaces to a specific length, and trailing spaces need to be removed when retrieving CHAR values.

6. What is the difference between a primary key and a candidate key?

Each row of the table is uniquely identified by a primary key, and a table has only one primary key.

A primary key is also a candidate key. By convention, candidate keys can be designated as primary keys, and can be used for any foreign key references.

7. What is myisamchk used for?

It is used to compress MyISAM tables, which reduces disk or memory usage.

What is the difference between MyISAM Static and MyISAM Dynamic?

All fields on MyISAM Static have a fixed width. Dynamic MyISAM tables will have fields like TEXT, BLOB, etc. to accommodate data types of different lengths.

MyISAM Static is easier to restore in case of damage.

8. What happens if a table has a column defined as TIMESTAMP?

The timestamp field will get the current timestamp whenever the row is changed.

What happens if the maximum value is reached in the table when the column is set to AUTO INCREMENT?

It stops incrementing and any further inserts will generate an error because the key is already in use.

How can I find out which autoincrement was assigned on the last insert?

LAST_INSERT_ID will return the last value assigned by Auto_increment and does not require specifying a table name.

 

9. How do you see all the indexes defined for the table?

Indexes are defined for tables by:

SHOW INDEX FROM <tablename>;

10. What do the % and _ in the LIKE statement mean?

% corresponds to 0 or more characters, _ is just one character in the LIKE statement.

How to convert between Unix and MySQL timestamps?

UNIX_TIMESTAMP is the command to convert from MySQL timestamp to Unix timestamp

FROM_UNIXTIME is the command to convert from Unix timestamp to MySQL timestamp

11. What is the column comparison operator?

Use the =, <>, <=, <, >=, >, <<, >>, <=>, AND, OR, or LIKE operators in column comparisons in a SELECT statement.

12. What is the difference between BLOB and TEXT?

A BLOB is a binary object that can hold variable amounts of data. TEXT is a case-insensitive BLOB.

The only difference between the BLOB and TEXT types is that sorting and comparisons are case sensitive for BLOB values ​​and not case sensitive for TEXT values.

 

13. What is the difference between MySQL_fetch_array and MySQL_fetch_object?

The following is the difference between MySQL_fetch_array and MySQL_fetch_object:

MySQL_fetch_array() – returns result rows as an associative array or a regular array from the database.

MySQL_fetch_object – Returns result rows from the database as objects.

14. Where will MyISAM tables be stored and also provide their storage format?

Each MyISAM table is stored on disk in three formats:

(1) ".frm" file storage table definition

(2) Data files have the extension ".MYD" (MYData)

(3) Index files have ".MYI" (MYIndex) extension

15. How does MySQL optimize DISTINCT?

DISTINCT converts to GROUP BY on all columns and is used in conjunction with the ORDER BY clause.

SELECT DISTINCT t1.a FROM t1,t2 where t1.a=t2.a;

 

16. How to display the first 50 rows?

In MySQL, use the following code query to display the first 50 rows:

SELECT*FROM

LIMIT 0,50;

 

17. How many columns can be used to create an index?

A maximum of 16 indexed columns can be created on any standard table.

 

18. What is the difference between NOW() and CURRENT_DATE()?

The NOW() command is used to display the current year, month, date, hour, minute and second.

CURRENT_DATE() only displays the current year, month and day.

 

19. What is a non-standard string type?

(1)TINYTEXT

(2)TEXT

(3)MEDIUMTEXT

(4)LONGTEXT

20. What is a general SQL function?

(1) CONCAT(A, B) – Concatenates two string values ​​to create a single string output. Typically used to combine two or more fields into one.

(2) FORMAT(X, D)- format the number X to D valid digits.

(3) CURRDATE(), CURRTIME()- Return the current date or time.

(4) NOW() – returns the current date and time as a value.

(5) MONTH(), DAY(), YEAR(), WEEK(), WEEKDAY() – Extracts the given data from a date value.

(6) HOUR(), MINUTE(), SECOND() – Extract given data from time value.

(7) DATEDIFF(A, B) – Determines the difference between two dates, usually used to calculate age

(8) SUBTIMES(A,B) - Determines the difference between two times.

(9) FROMDAYS(INT) – Converts an integer number of days to a date value.

21. Does MySQL support transactions?

In the default mode, MySQL is in autocommit mode, and all database update operations will be committed immediately, so by default, MySQL does not support transactions.

But if your MySQL table type uses InnoDB Tables or BDB tables, your MySQL can use transaction processing, use SETAUTOCOMMIT=0 to make MySQL allow non-autocommit mode, in non-autocommit mode, you must use COMMIT to Commit your changes, or use ROLLBACK to roll back your changes.

22. What field type is good for recording currency in MySQL

The NUMERIC and DECIMAL types are implemented by MySQL as the same type, which is allowed in the SQL92 standard. They are used to hold values ​​where the exact precision is extremely important, such as money-related data. When declaring a class to be one of these types, precision and scale can be (and usually are) specified.

For example:

salary DECIMAL(9,2)

In this example, 9 (precision) represents the total number of decimal places that will be used to store the value, and 2 (scale) represents the number of digits that will be used to store the value after the decimal point.

So, in this case, the range of values ​​that can be stored in the salary column is from -9999999.99 to 9999999.99.

 

23. How many tables are related to permissions in MySQL?

The MySQL server controls the user's access to the database through the permission table, which is stored in the MySQL database and initialized by the MySQL_install_db script. These permission tables are user, db, table_priv, columns_priv and host.

 

24. What can be the string type of the column?

String types are:

(1)SET2

(2)BLOB

(3)ENUM

(4)CHAR

(5)TEXT

25. The MySQL database is used as the storage of the publishing system, and the increment of more than 50,000 records per day is expected to be operated and maintained for three years. How to optimize it?

(1) Design a good database structure, allow some data redundancy, try to avoid join queries, and improve efficiency.

(2) Select the appropriate table field data type and storage engine, and add indexes appropriately.

(3) MySQL library master-slave read and write separation.

(4) Find rules and divide tables to reduce the amount of data in a single table and improve query speed.

(5) Add caching mechanism, such as memcached, apc, etc.

(6) For pages that do not change frequently, generate static pages.

(7) Write efficient SQL. For example, SELECT * FROM TABEL is changed to SELECT field_1, field_2, field_3 FROM TABLE.

26. Lock optimization strategy

(1) Read-write separation

(2) Sectional locking

(3) Reduce the lock holding time

(4) Multiple threads try to acquire resources in the same order

The granularity of locks cannot be overly refined, otherwise threads may be locked and released too many times, but the efficiency is not as good as adding a large lock at a time.

27. The underlying implementation principle and optimization of the index

B+ tree, optimized B+ tree

The main reason is that pointers to the next leaf node are added in all leaf nodes, so InnoDB recommends using the default self-incrementing primary key as the primary index for most tables.

28. Under what circumstances is the index set but cannot be used

(1) LIKE statement starting with "%", fuzzy matching

(2) Indexes are not used at the same time before and after the OR statement

(3) There is an implicit conversion of the data type (for example, if varchar does not add single quotes, it may be automatically converted to int type)

 

29. How to optimize MySQL in practice

It is best to optimize in the following order:

(1) Optimization of SQL statements and indexes

(2) Optimization of database table structure

(3) Optimization of system configuration

(4) Hardware optimization

 

30. How to optimize the database

(1) Select the most applicable field attribute, reduce the width of the defined field as much as possible, and set the field to NOTNULL as much as possible, such as 'province' and 'gender', it is best to apply ENUM

(2) Use join (JOIN) instead of subquery

(3) Apply union (UNION) instead of manually created temporary table

(4) Transaction processing

(5) Lock tables and optimize transaction processing

(6) Apply foreign keys and optimize locking tables

(7) Build an index

(8) Optimize the query statement

31. Briefly describe the difference between index, primary key, unique index, and joint index in MySQL, and what impact it has on the performance of the database (from both read and write aspects)

Indexes are special files (indexes on InnoDB data tables are part of the table space) that contain reference pointers to all records in the data table.

The sole task of ordinary indexes (those defined by the keywords KEY or INDEX ) is to speed up access to data.

Ordinary indexes allow the indexed data columns to contain duplicate values. If you can determine that a data column will only contain values ​​that are different from each other, you should use the keyword UNIQUE to define it as a unique index when creating an index for this data column. In other words, a unique index can guarantee the uniqueness of data records.

A primary key is a special unique index. Only one primary key index can be defined in a table. The primary key is used to uniquely identify a record and is created using the keyword PRIMARY KEY.

An index can cover multiple data columns, such as an index like INDEX (columnA, columnB), which is a joint index.

Indexing can greatly improve the query speed of data, but it will reduce the speed of inserting, deleting, and updating tables, because when performing these write operations, the index file must also be operated.

 

32. What is a transaction in a database?

A transaction is an ordered set of database operations as a unit. A transaction is considered successful if all operations in the group succeed, and is not successful even if only one operation fails. If all operations are complete, the transaction is committed and its modifications will be applied to all other database processes. If an operation fails, the transaction is rolled back and the effects of all operations on that transaction are cancelled.

Transaction characteristics:

(1) Atomicity: that is, indivisibility, all transactions are either executed or not executed at all.

(2) Consistency or stringability. The execution of a transaction causes the database to transition from one correct state to another.

(3) Isolation. Any changes to data made by a transaction are not allowed to be made available to any other transaction until the transaction is properly committed.

(4) Persistence. After the transaction is submitted correctly, its result will be permanently saved in the database, even if there are other failures after the transaction is submitted, the processing result of the transaction will also be preserved.

Or understand it this way:

A transaction is a group of SQL statements that are bound together as a logical unit of work. If any statement operation fails, the entire operation will fail, and subsequent operations will roll back to the pre-operation state, or there is a node on it. To ensure that something is either executed or not executed, transactions can be used. To consider a group statement as a transaction, it needs to pass the ACID tests, namely Atomicity, Consistency, Isolation and Durability.

33. What are the causes of SQL injection vulnerabilities? How to prevent it?

Reasons for SQL injection: In the process of program development, the standard writing of sql statements and filtering of special characters are not paid attention to, so that the client can submit some sql statements through global variables POST and GET for normal execution.

Ways to prevent SQL injection:

Turn on the magic_quotes_gpc and magic_quotes_runtime settings in the configuration file

Use addslashes to convert sql statements when executing sql statements

Try not to omit double quotes and single quotes when writing Sql statements.

Filter out some keywords in the sql statement: update, insert, delete, select, *.

Improve the naming skills of database tables and fields, and name some important fields according to the characteristics of the program, so that they are not easy to be guessed.

34. Select the appropriate data type for the fields in the table

Field type priority: integer>date,time>enum,char>varchar>blob,text

Give priority to numeric types, followed by date or binary types, and finally string types. For data types of the same level, data types that occupy less space should be preferred.

35. Storage period

Datatime:

Store period time in YYYY-MM-DD HH:MM:SS format, accurate to seconds, occupying 8 bytes of storage space, datatime type has nothing to do with time zone Timestamp: stored in timestamp format, occupying 4 bytes, small range From 1970-1-1 to 2038-1-19, the display depends on the specified time zone. By default, the value of the timestamp column can be automatically modified when the data in the first row is modified

Date:

(Birthday) takes up less bytes than using string .datatime.int to store, and using date only needs 3 bytes to store the date and month, and you can also use the date time function to calculate the date period

Time:

Store the time part of the data

Notice:

Do not use the string type to store date and time data (it usually takes up less storage space than a string, and you can use the date function when searching and filtering)

Using int to store date time is not as good as using timestamp type

36. For relational databases, indexing is a very important concept. Please answer a few questions about indexes:

(1) What is the purpose of the index?

Quickly access specific information in the data table to improve retrieval speed

Create a unique index to ensure the uniqueness of each row of data in the database table.

Accelerated tables and joins between tables

When using grouping and sorting clauses for data retrieval, you can significantly reduce the time for grouping and sorting in queries

(2) What are the negative impacts of indexes on database systems?

Negative impact:

It takes time to create and maintain indexes, and this time increases as the amount of data increases; indexes need to occupy physical space, not only tables need to occupy data space, each index also needs to occupy physical space; when adding or deleting tables , Change, and index must also be dynamically maintained, which reduces the speed of data maintenance.

(3) What are the principles for indexing data tables?

Build indexes on the most frequently used fields to narrow down queries.

Build indexes on frequently used fields that need to be sorted

(4) Under what circumstances should indexing not be established?

For columns rarely involved in queries or columns with many repeated values, it is not appropriate to build indexes.

For some special data types, it is not suitable to build indexes, such as text fields (text), etc.

37. Explain the difference between MySQL outer join, inner join and self join

Let me first talk about cross-join: Cross-join is also called Cartesian product. It refers to directly matching all records in one table with all records in another table without using any conditions.

inner join 

It is a cross-connect with only conditions. According to a certain condition, the records that meet the conditions are filtered out, and the records that do not meet the conditions will not appear in the result set, that is, the inner connection only connects the matching rows.

outer join 

The result set not only contains rows that meet the join conditions, but also includes all data rows in the left table, right table, or both tables. These three cases are called left outer join, right outer join, and full outer join. .

left outer join

Also known as left join, the left table is the main table, and all records in the left table will appear in the result set. For those records that do not match in the right table, they will still be displayed, and the corresponding field values ​​on the right will be filled with NULL. Right outer join, also known as right join, the right table is the main table, and all records in the right table will appear in the result set. Left joins and right joins are interchangeable, and MySQL does not currently support full outer joins.

38. Overview of transaction rollback mechanism in Myql

A transaction is a sequence of database operations defined by the user. These operations are either all done or not done at all. It is an inseparable unit of work. Transaction rollback refers to the undo of the transaction’s completed update operations on the database.

When you want to modify two different tables in the database at the same time, if they are not a transaction, when the first table is modified, there may be an exception during the modification process of the second table and it cannot be modified. At this time, only the second table It is still the state before it was modified, and the first table has been modified. And when you set them as a transaction, when the first table is modified, the second table is modified abnormally and cannot be modified, the first table and the second table will return to the unmodified state, This is called transaction rollback

39. What parts does the SQL language include? What are the action keywords for each section?

SQL language includes data definition (DDL), data manipulation (DML), data control (DCL) and data query (DQL) four parts.

Data definition:

Create Table,Alter Table,Drop Table, Craete/Drop Index 等

Data manipulation:

Select ,insert,update,delete,

Data Control:

grant,revoke

data query:

select

40. What are the integrity constraints?

Data integrity (Data Integrity) refers to the accuracy (Accuracy) and reliability (Reliability) of data.

Divided into the following four categories:

(1) Entity integrity:

Each row of the table is stipulated to be the only entity in the table.

(2) Domain integrity:

It means that the columns in the table must meet a specific data type constraint, and the constraint includes the value range, precision and other regulations.

(3) Referential integrity:

It means that the data of the primary key and foreign key of the two tables should be consistent, which ensures the consistency of data between tables and prevents data loss or meaningless data from spreading in the database.

(4) User-defined integrity:

Different relational database systems often require some special constraints according to their different application environments. User-defined integrity is a constraint for a specific relational database, which reflects the semantic requirements that a specific application must meet.

Constraints related to tables:

Including column constraints (NOT NULL (not empty constraints)) and table constraints (PRIMARY KEY, foreign key, check, UNIQUE).

41. What is a lock?

A database is a shared resource used by multiple users. When multiple users access data concurrently, multiple transactions will simultaneously access the same data in the database. If the concurrent operations are not controlled, incorrect data may be read and stored, destroying the consistency of the database.

Locking is a very important technology to achieve database concurrency control. Before a transaction operates on a data object, it first sends a request to the system to lock it. After locking, the transaction has certain control over the data object. Before the transaction releases the lock, other transactions cannot update the data object.

Basic lock types: locks include row-level locks and table-level locks

42. What is a view? What is a cursor?

A view is a virtual table that has the same functionality as a physical table. Views can be added, modified, checked, and manipulated. A view is usually a subset of rows or columns of one or more tables. Modifications to the view do not affect the underlying tables. It makes it easier for us to get data, compared to multi-table queries.

Cursor: It is to effectively process the query result set as a unit. The cursor can be positioned on a specific row in the cell to retrieve one or more rows from the current row in the result set. Can modify the current row of the result set. Cursors are generally not used, but they are very important when data needs to be processed one by one.

43. What is a stored procedure? What to call?

A stored procedure is a precompiled SQL statement. The advantage is that it allows a modular design, that is, it only needs to be created once, and it can be called multiple times in the program later. If an operation needs to execute multiple SQLs, using stored procedures is faster than pure SQL statements. Stored procedures can be called with a command object.

44. How to understand the three paradigms in a popular way?

First normal form: 1NF is an atomic constraint on attributes, which requires attributes to be atomic and cannot be decomposed;

Second normal form: 2NF is a unique constraint on records, requiring records to have a unique identifier, that is, the uniqueness of entities;

The third normal form: 3NF is a constraint on field redundancy, that is, any field cannot be derived from other fields, and it requires no redundancy in fields. .

Pros and cons of paradigmatic design:

Advantages: Data redundancy can be reduced as much as possible, making updates fast and small in size

Disadvantages: For queries, multiple tables are required to be associated, which reduces writing efficiency and increases reading efficiency, making index optimization more difficult

Denormalization:

Advantages: It can reduce the association of tables, and can perform better index optimization

Disadvantages: Data redundancy and data anomalies, data modification requires more cost

45. What is a basic table? What is a view?

The basic table is a table that exists independently. In SQL, a relationship corresponds to a table. Views are tables derived from one or several base tables. The view itself is not stored independently in the database, it is a virtual table

46. ​​Describe the advantages of views?

(1) Views can simplify user operations

(2) Views enable users to view the same data from multiple perspectives;

(3) The view provides a certain degree of logical independence for the database;

(4) Views can provide security protection for confidential data.

47. What does NULL mean?

NULL This value means UNKNOWN (unknown): it does not mean "" (empty string). Any comparison against the value NULL will produce a NULL value. You cannot compare any value to a NULL value and logically expect an answer.

Use IS NULL for NULL judgment

48. What is the difference between primary key, foreign key and index?

The difference between primary key, foreign key and index

definition:

Primary key - uniquely identifies a record, cannot have duplicates, and is not allowed to be empty

Foreign key - the foreign key of the table is the primary key of another table, the foreign key can have duplicates, and can be null

Index - the field has no duplicates, but can have a null value

effect:

Primary key - used to ensure data integrity

Foreign keys - used to establish relationships with other tables

Index - is to improve the speed of query sorting

Number:

Primary key - there can only be one primary key

Foreign Keys - A table can have multiple foreign keys

Indexes - A table can have multiple unique indexes

49. What can you use to ensure that a field in a form only accepts values ​​within a certain range?

Check limit, which is defined in the database table, is used to limit the value entered in this column.

Triggers can also be used to limit the values ​​that fields in a database table can accept, but this approach requires the trigger to be defined in the table, which may affect performance in some cases.

50. Tell me about the methods for optimizing SQL statements? (choose several)

(1) In the Where clause: the connection between where tables must be written before other Where conditions, and those conditions that can filter out the maximum number of records must be written at the end of the Where clause. HAVING is the last.

(2) Replace IN with EXISTS and NOT IN with NOT EXISTS.

(3) Avoid using calculations on index columns

(4) Avoid using IS NULL and IS NOT NULL on indexed columns

(5) To optimize the query, you should try to avoid full table scanning, and first consider building indexes on the columns involved in where and order by.

(6) Try to avoid judging the null value of the field in the where clause, otherwise the engine will give up using the index and perform a full table scan

(7) Try to avoid performing expression operations on fields in the where clause, which will cause the engine to give up using indexes and perform full table scans

at last

Follow the official account: Programmer Chasing the Wind, reply 003 to get the latest 2020 Java interview question manual (more than 200 pages of PDF documents).

 

Guess you like

Origin blog.csdn.net/Design407/article/details/108055113