[Database] Frequently Asked Questions

Q1: What are the common relational database management system products?

Oracle、Sql Server、mysql、Sybase

Q2: What parts does the SQL language include? What keywords are there in each section?

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

DDL: Create Table, Alter Table, Drop Table, Create / Drop Index create / delete - table / index
DML: Select, Insert, Update, Delete CRUD
DCL: grant, revoke authorization and revocation of authorization
DQL: select

Q3: What are the integrity constraints?

There are three main types of integrity constraints, namely entity integrity, referential integrity, and user-defined integrity.
The first two are database system requirements, and user-defined integrity exists from the user's perspective.
Entity integrity refers to (primary code), one or a group of attributes is the primary attribute of the basic relationship R, then the attribute cannot be vacant (can not be meaningless, do not know, do not exist)
Referential integrity refers to (outer code), If an attribute or attribute group F is the outer code of the basic relationship R, either it takes a null value or corresponds to the main code of the relationship S, which is equal to the
user-defined integrity of the main code value of an ancestor in S , It is a constraint condition for a specific relational database, which reflects the semantic requirements that the data involved in a specific application must meet.

Q4: What are the types of SQL constraints?

The entity integrity constraint is the primary key, which is used to control the field content is not repeated. Only one primary key is allowed to
refer to the integrity constraint in a table. It is the foreign key Foreign Key, which is used for reference connection between tables and tables. It is used for prevention. destruction of the connection between the operation table, it is possible to prevent illegal data into the foreign key column, since it must be a value of the reference point of the table
fields integrity constraints, refers to the table column must satisfy a specific data type constraints , Including value range, precision, etc.
User-defined integrity is the semantic requirement that data for a specific application must meet. There are mainly attribute constraint conditions
Not NULL, used to control the content of the
field is not unique, used to control the content of the field can not be repeated, there can be multiple
Check, used to control the value range of the field, check whether the column value meets a certain condition expression

Q5: What can be used to ensure that the fields in the table only accept a specific range of values

check can be used to limit the value entered in the column, within a certain range

Q6: What is a basic table? What is a view?

A basic table is a table that exists by itself, and a relationship corresponds to a basic table in SQL.
Views are tables derived from one or several basic tables. The view itself is not stored independently in the database, it is a virtual table. The basic operations of adding, deleting, and modifying the view can be performed, but modifying the view does not affect the basic table. Because the database only stores the definition of the view and does not store the data corresponding to the view, these data are still stored in the basic table of the exported view.

Q7: Advantages of view

Convenience: The view can simplify the user's operation
Angle: Let the user view the same data from multiple angles
Logical independence: The view provides a certain degree of logical independence to the reconstructed database
Security: The view can provide security protection for confidential data

Q8: Explain correlated sub-queries and uncorrelated sub-queries

In nested queries, if the query result of the subquery does not depend on the parent query, it becomes an unrelated subquery; if the query condition of the subquery depends on the parent query, it is called a correlated subquery

Q9: The difference between drop \ delete \ truncate

Delete content: delete and truncate can only delete the data in the table, delete deletes a row of data from the table each time, truncate can delete the data of the entire table, but they can not delete the structure of the table, drop is the table data and structure together delete.
After deletion, delete belongs to the data manipulation language DML. The delete operation will be saved as a transaction record in the log. Therefore, the delete operation can be rolled back and the delete operation can be undone. Truncate and drop are data definition languages ​​DDL. Once executed, they cannot be rolled back.
In execution speed, truncate and drop are faster.
If the operation is related to the transaction, you still need to use delete

Q10: Application scenarios of drop \ delete \ truncate

When you no longer need a table, use drop to delete the entire table. To delete
some data rows, you can use delete, and you can use the where clause to limit the deletion of the rows. You
need to keep the table, but when you want to clear the data, you can use truncate

Q11: Database index

The database index is similar to a book directory, which can speed up the database query speed.
An index is a data structure that can sort the values ​​of one or more columns in a database table to speed up data access. When searching for specific content, you do n’t need to scan the entire table to get the required data. You only need to find the correspondence according to the index. Address. Analogy is to find a certain knowledge point, you don't need to go through the entire book, you only need to find the corresponding content in the table of contents.

Q12: When not to create an index

The main use of indexes is to speed up queries, so a database that requires fewer queries can reduce the use of or not use an index.
For databases with a small amount of data and a small size, there may be only a few records. Scanning such a table itself does not take a long time. , There is no need to build an index. If you add an index, it will increase the content that the system needs to maintain and occupy unnecessary space
. Tables that often need to be modified, and also reduce the index as much as possible, just like the linked list and sequence table in the data structure. The index is to facilitate retrieval, but the operation of adding, deleting, and modifying is too frequent will also waste a lot of operations on modifying the index

Q13: Index optimization

The purpose of indexing is to speed up the search. The idea of ​​searching in the data structure is mainly optimized from two aspects. On the one hand is the data structure, choose the appropriate data structure, B tree or hash hash table; Appropriate search algorithms, sequential search, half search, and block search
In addition, computer problems can also be roughly divided into hardware and software. Software is actually the allocation and optimization of hardware resource scheduling.
On the hardware, the search is mainly the time and space consumption of CPU access. The indexed table is generally large, and it is impossible to store all of it in memory. That is, the CPU needs to access the disk when accessing a certain table, which generates disk I / O scheduling , CPU allocation and other issues, so the choice of data structure and algorithm should be as coordinated as possible with these problems, you can achieve optimization.

Q14: Advantages and disadvantages of indexes

Advantages:
By establishing an index, you can greatly improve the speed of obtaining the required information in the database, and at the same time improve the efficiency of the server to process related search requests.
In addition to speeding up the retrieval of its own data, it can also be used on foreign keys to speed up the table and The connection speed between tables.
Disadvantages: It
takes time to build and maintain the index table. In the case of increasing data, the time spent will also increase the
index. It takes a certain amount of physical space. If the operating system manages a disk, There needs to be something like an index table to manage each data on the disk, the table itself also takes up space

Q15: Difference between primary key, foreign key and index

Primary key: uniquely identifies a record, there can be no duplicates, and it is not allowed to be empty. It can guarantee the integrity of the data entity, and there can only be one primary key.
Foreign key: The foreign key of a table can be empty, or it can be used gradually to establish contact with other tables to ensure the referential integrity of the table. A table can have multiple foreign keys.
Index: The index has no duplicate values, but there can be a control that can be used to increase the speed of query sorting. A table can have multiple unique indexes

Q16: What is a transaction and its characteristics

Transaction is a series of inseparable sequence of database operations, and it is also the basic logical unit of database concurrency control.
The four basic elements of the transaction: ACID
atomicity (Atomicity): That is, the transaction is a period of operation, or all are executed, or all rolled back
consistency (Consistency): the execution of the transaction database from one state to another State, closely related to atomicity
Isolation: Before the transaction is committed, it is not allowed to provide any changes to the data of the transaction to other transactions
. Durability: After the transaction is submitted correctly, the results will be permanently stored in the database Even if there are other failures after the transaction is submitted, the transaction processing results will be saved. That is, once submitted, the transaction changes the data in the database permanently.

Q17: What is a cursor

SQL is set-oriented. A single SQL statement can generate or process multiple records. The main language of embedded SQL is record-oriented. A group of main variables can only store one record at a time. Therefore, the host variable cannot meet the requirements of the SQL statement output data, so the embedded SQL introduces a cursor to coordinate the processing of the two.
The cursor is a data buffer created by the system for the user to store the execution result of the SQL statement. Each cursor has a name. The user obtains the record through the cursor and assigns it to the main variable, and hands it to the main language for further processing.

Q18: What is a trigger

A trigger is a database object related to a table. It is triggered when a defined condition is met, and executes the set of statements defined in the trigger. Can assist the application on the database side to ensure the integrity of the database

Q19: What is a stored procedure? What to call

Stored procedures are similar to functions in other languages. It is a process written by procedural SQL statements. This process is stored in the database server after compilation and optimization, and it can be called only when it is used.
Use Call / Perform to activate stored procedure execution. In procedural SQL, the database server supports calling other stored procedures in the procedure body.

Format:
CALL / PERFORMANCE PROCEDURE procedure name

Q20: What are the advantages and disadvantages of stored procedures

Advantages: It can encapsulate a piece of operation code and save it in the database. It only needs to be called every time. It saves a lot of programming time than the realization of sentence by sentence, and the stored procedure is stored in the database after compilation Each call can be executed directly, which is more efficient.
Disadvantages: The stored procedure syntax of each database is almost different, the compatibility is very poor, and it cannot be universal. You need to put the business logic on the server side, such as the JAVA part.

Q21: Paradigm understanding

First Normal Form (1NF): All attributes in relational model R are indivisible basic data items, then R belongs to 1NF. The first paradigm is the minimum requirement for the relational model. A database model that does not meet the first paradigm cannot be called a relational database.
Second Normal Form (2NF): If the relationship mode R belongs to 1NF, and each non-primary attribute is completely dependent on the code of R, then R belongs to the 2NF
third normal form (3NF): There is no such code X, attribute group Y and non-primary attribute Z (Z is not included in Y), yes X-> Y (Y cannot determine X), Y-> Z is established, it is called R <U, F> Belongs to 3NF. (The third normal form eliminates transitive dependencies on the basis of the second normal form)
BCND, modified 3NF or extended 3NF: If X-> Y and Y does not belong to X, X must have a code, then it is called R <U , F> belongs to BCNF.
The fourth normal form (4NF): the relationship mode R <U, F> belongs to 1NF, if for each trivial multi-value of R depends on X-> Y (Y is not included in X), X has a code, it is called R <U , F> belongs to 4NF.
Multi-value dependency: Let R (U) be a relationship pattern on attribute set U, X, Y, Z are a subset of U, and Z = U-X-Y, multi-value dependency X in relationship pattern R (U) ->-> Y holds true if and only if for any relationship r of R (U), given a pair of (x, z) values, there is a set of Y values, which is determined only by the value of x and Independent of z value

Any binary relationship belongs to 3NF, BCNF, 4NF

Q22: What is a lock

Similar to the process scheduling management in the operating system, it is an important technology to achieve concurrency control. The database is a shared resource used by multiple users. When multiple users access data concurrently, multiple transactions will occur in the database to access the same data at the same time. Therefore, there must be a blockade mechanism as a control.
When the transaction operates on a certain data object, it sends a request to the system to let it lock. After locking, the transaction can operate on the data, and after the end, unlock and release resources for other transactions.

Q23: What are optimistic locking and pessimistic locking of the database

The task of concurrency control in the database management system DBMS is to ensure that when multiple transactions access the same data in the database at the same time, the isolation and unity of the transactions and the unity of the database are not destroyed.
Optimistic locking and pessimistic locking are the main technical methods used in concurrency control.
Optimistic lock, that is, optimistic concurrency control lock. Assuming that no concurrency conflicts will occur, only check for data integrity violations when submitting operations. (That is, allocate resources first, and check whether there is a problem after the operation is completed)
Pessimistic locks, that is, pessimistic concurrent control locks. Assuming that concurrency conflicts will occur, all operations that may violate data integrity are blocked.

Q24: What are the data table types

MySQL supports InnoDB, GEAP, BOB, ARCHIVE, CSV and other data table types

Q25: Optimized SQL statement

SQL statements are as simple as possible.
Do n’t have too many table joins
. Use fewer subqueries. Nested queries or view nesting are also as simple as possible.
Avoid scanning the entire table.
Use indexes reasonably. Where clause
like statements are also written as clearly as possible.% Although it can be omitted a lot, it also means that the burden of the query is increased
. The use of in is reduced. For example, between and exist,
select * from table is used. The
intermediate results can be temporarily stored in a temporary table, avoiding multiple scans of the main target, and reducing the execution of shared locks "" Blocking "
uses as small a field as possible, saving memory and reading consumption

Q26: The idea of ​​database optimization

Database structure optimization
Paradigm optimization, eliminating
the split of redundant (space saving) tables, so that the table structure is not too complicated
Index optimization
Algorithm data structure optimization
Physical optimization
SQL statement optimization: as above

Published 16 original articles · Likes0 · Visits 458

Guess you like

Origin blog.csdn.net/weixin_43951831/article/details/104842449