Database ---- recruit school often test the contents of written examination and interview summary

1. What is a stored procedure? What are the advantages and disadvantages?

Stored procedures are prebuilt SQL statements.
I understood more straightforward: the stored procedure can be a set of records, it is a code block by a number of T-SQL statements, such as T-SQL statement codes as a method to realize some functions (by single table or multiple tables deletion check), then give this block a name, call him on the line when use this function.

  • A stored procedure is a precompiled block of code, a relatively high efficiency
  • Alternatively a large number of stored procedure T_SQL statements can reduce network traffic, the communication rate increase
  • Ensure data security can to a certain extent

2. What indexes are? What is the role and the advantages and disadvantages?

The index is the structure of the database table values of one or more columns to sort, to help acquire MySQL efficient data structure of data
you can interpret it this way: the index is to speed up data retrieval method in the table. Index database is similar to the index of the book. In the book, the index allows the user does not have to read a complete book to find the information they need quickly. In the database, the index database program also allows to quickly find the data in the table, without having to scan the entire database.
MySQL database several basic types of indexes :
the general index, the only index, primary key indexes, full-text indexing
indexes role :

  • Index quicker retrieval database
  • Index slows down the insert, delete, modify, and other maintenance tasks
  • The only index to ensure the uniqueness of each row of data
  • By using an index can be used to hide in the process of optimizing query, improve system performance

Features index
(1) index, once established, Oracle management system will be maintained automatically, and decide when to use an index by Oracle management system
(2) user does not specify which index to use in the query language
in the definition of primary key (3) after the restraint system or unique index is automatically created in the appropriate column
(4) the user can according to their needs, to specify a single field or multiple fields, add an index
it should be noted that: Oracle is automatically help us manage the index, and If we specify a primary key or unique constraints on the system will automatically create a corresponding column in the index ...
when [to] create an index
(1) regular table SELECT operation
(2) the table is large (super multi recording), recorded content distribution of a wide range of
(3) column names often appear in the WHERE clause or join condition
when [Do not] create an index
(1) table regular INSERT / UPDATE / DELETE operations
(2) the table is small (less record over)
( 3) column name does not often appear as the join condition in the WHERE clause or
indexed advantages and disadvantages:

  • Index quicker retrieval database
  • Index reducing insert, delete, modify the speed and other maintenance tasks (although the index can improve query speed, but they also cause performance database system updates the data decreases, because most of the data update need to update the index)
  • The only index to ensure the uniqueness of each row of data, by using an index can be used to hide in the process of optimizing query, improve system performance
  • Index needs to account for the physical and data space

Index Category:

  • The only index : unique index does not allow two rows have the same index value
  • Primary key indexes : a primary key will automatically create a primary key indexes defined for a table, the primary key index is a special type of unique index. Primary key index requires each primary key values are unique, and can not be null
  • Aggregation index (Clustered): the physical order of the key values in each row of the logic table (index) in the same order, each table can have only one
  • Non-clustered index (Non-clustered): non-clustered index specified logical order table. Data is stored in a location index is stored in another location, the index contains pointers to data storage locations. There may be multiple, less than 249

3. What is a transaction?

Transaction (Transaction) is the basic unit of concurrency control. The so-called transaction, it is a sequence of operations, these operations are either executed or not executed, it is an indivisible unit of work. A transaction is a database maintaining data consistency of the unit, at the end of each transaction, can maintain data consistency.

Transaction characteristics:

  • Atomicity (Atomicity): All operations in one transaction (Transaction) is either completed or not completed all, it does not end in the middle of a link. Transaction error occurs during execution, it will be rolled back (Rollback) to the state before the start of the transaction, as the transaction never performed the same.
  • Consistency (Consistency): and after the end of the transaction before the transaction began, the integrity of the database is not corrupted. This means that data written must be fully compliant with all of the default rules, which include precision data, and the subsequent series of new database can complete the scheduled work spontaneously. Then A + B = 100, the enforcement branch:: A transfer to B, before performing the transaction A + B = 100
  • Isolation (Isolation): exhibited mutual relationship when two or more concurrent transactions access (access here refers to query and modify operations) when the same data in the database. Transaction isolation divided into different levels, including a read not to submit (the Read
    Uncommitted), Read Committed (Read committed), repeatable read (Repeatable
    Read) and serialized (Serializable). Results concurrent access to multiple transactions do not affect each other, A does not overwrite the result of the transaction the transaction B
  • Persistent (Durability): After the completion of the transaction, the transaction changes made to the database will be permanently stored in the database, and is complete. Once the transaction is committed, changes to the database permanent data is
    a result of an operation usually contain many sub-operations, and these sub-operations may be problematic due to hardware damage or other factors, to correctly implement ACID is not easy. ACID recommended that the database will need to update and modify all the data once the operation is completed, but in fact is not feasible.

There are two main ways to achieve ACID: The first is Write ahead logging, journaling is the way. The second is the Shadow paging.

Write ahead logging (write-ahead log) :

  1. All the changes caused by the firm to be recorded in the log, before the completion of the transaction commits, all of these records must be written to disk;
  2. A buffer page can modify the database until after the occurrence be logged. Until the corresponding log buffer page is written to disk before the buffer page in the hard disk;
  3. When the buffer page has been modified and the modified log is being updated, the mutex must be added to ensure that the changes are recorded in the order of its occurrence log is consistent.
    The results of the above rule:
    If a log is not stored in the hard disk, it can be ignored, because the changes contained in the log must belong to the uncommitted transactions. Furthermore, such does not reflect the persistent log in the database changes;
    logging changes the order of the recording system. Locking protocol (latch protocol) to ensure that if there are two log records for the same page changes, the order reflects a change in the order of two records on the page.

What optimistic and pessimistic locking 4. Database is?

Concurrency Control task database management system (DBMS) is isolated and ensuring uniformity and unity without destroying the transaction database when multiple transactions simultaneously access the same data in the database.
Optimistic concurrency control (optimistic locking) and pessimistic concurrency control (pessimistic locking) concurrency control techniques are mainly used.

  • Pessimistic locking: assume concurrency violation occurs, the shield may violate data integrity of all operations
  • Optimistic locking: Suppose concurrency conflicts will not occur, if only to check data integrity violation when a commit operation.

5. Use the index query will be able to improve the performance of queries it? why

Typically, the index query data faster than a full table scan. But we also must be noted that price.
Index requires space to store, but also require regular maintenance, whenever a record is modified or changes in the index column in the table, the index itself can be modified. this means that each record INSERT, DELETE, UPDATE will pay more than 4,5 times the disk I / O for this purpose because the index requires additional storage space and processing, but those unnecessary indexes query response time will slow down the query may not be able to use the index to improve query performance, range queries index (iNDEX rANGE SCAN) in two situations:

  • Based on a retrieval range, the query results typically set less than 30% of the number recorded in the table
  • Based retrieval of non-unique index

6. talk about the difference between simple drop, delete and truncate the

SQL in the drop, delete, truncate have expressed deleted, but there are some differences three

  • delete and delete only truncate table does not delete the data structure of the table
  • Speed, in general: drop> truncate> delete
  • delete statement is dml, this action will put rollback segement, the only take effect after the transaction commits;
    if there is a corresponding trigger, when the execution will be triggered truncate, drop is ddl, operating with immediate effect, the original data is not put rollback segment. can not be rolled back. operation does not trigger the trigger.

7.drop, delete and truncate, respectively, under what scenario to use?

  • When a table is no longer needed, with drop
  • I want to delete some data lines when using delete, and bring the where clause
  • Reserved tables to delete all data when using truncate
  • Super key, what the candidate keys, primary keys, foreign keys are?
  • Super Key: relational attribute set that uniquely identifies the tuple mode called super key relationships. A property may be used as a super key, the plurality of attributes can be grouped together as a super key. Super key contains the primary key and a candidate key.
  • Candidate key: the key is the smallest super, super key that is not redundant elements.
  • Primary Key: database table columns or attributes of data to be a combination of unique and complete identification of the stored data objects. A data column can have only one primary key and the primary key value can not be deleted, i.e., not a null value (Null).
  • Foreign key: in the presence of a primary key in another table called the foreign key of the table.

8. What is a view? And the use of the scene view of what?

A view is a virtual table, has the same functionality as a physical table. Can be increased to view, change, operation, there is usually attempted to a table or a subset of the plurality of rows or columns of the table. Changes to the view do not affect the basic table. It allows us to obtain data more easily, compared to multi-table queries.

  • Only the exposed portions of the field to the visitor, so we built a virtual table, it is the view.
  • Query data from different tables, queries and wishes in a uniform manner queries, so you can create a view, the query results together multiple tables, the query only needs to get data directly from the view, regardless of the data source differences in table brought

9. The talk about three paradigms

  • The first paradigm (1NF): database table fields are a single property, can not be divided. This property consists of a single basic types, including integer, real number, character, logic type, date type.
  • The second paradigm (2NF): database table no non-key field dependent on any one part of the function key field candidates (partial functional dependency means that there is some combination of keywords in the field of non-key field decision ), i.e. all non-key fields are totally dependent on any set of candidate keys.
  • The third paradigm (3NF): on the basis of the second paradigm, if there is no data in the table the non-dependent key field transfer function according to any one candidate keyword section is in line with the third paradigm. The so-called dependent transfer function, such as referring to
    the presence of "A → B → C" determines the relationship fruit, depend on the transfer function C A. Key fields →: Thus, the third normal dependency database tables should not exist as
    a non-key field non-keyword segment x → y

10.SQL constraints are there?

  • NOT NULL: field for controlling the content must not be empty (NULL).
  • UNIQUE: Control contents of the field can not be repeated, allowing a plurality of table Unique constraints.
  • PRIMARY KEY: is used to control field contents can not be repeated, but it appears in a table allows only a.
  • FOREIGN KEY: an operation for preventing damage between the connection table, can prevent illegal data into the foreign key column, because it must be one of the values ​​that it points in the table.
  • CHECK: range of values ​​for the control field.

11. The commonly used storage engine:

  • Innodb engine, Innodb database engine provides support for ACID transactions. And also provides row-level locks constraints and foreign keys. Its design goal is to handle large data capacity database system.
  • MyIASM engine (originally Mysql default engine), does not provide support for transactions, row-level locking and does not support foreign keys.
  • MEMORY engine: all data in memory, fast data processing speed, but security is not high.

Table 12. mysql related rights which has several?

MySQL server to control user access authority table by the database, permissions table stored in the mysql database by mysql_install_db script to initialize. These rights are tables user, db, table_priv, columns_priv and host. The following are brief structure and content of these tables:

  • user permissions table: recording allows the user to connect to the server account information, which is a global authority level.
  • db permissions table: operating authority records on each individual account database.
  • table_priv permissions table: data recording operation authority table level.
  • columns_priv permissions table: recording operating authority level data column.
  • host permission table: db with permission table on the database level permissions to a given host for finer control. The permissions table is not affected by GRANT and REVOKE statements.

13. The data table damage repair options?

Use myisamchk to repair, concrete steps are:
1) prior to repair mysql service will stop.
2) Open the command line, and then into the / bin directory of mysql.
3) execution path myisamchk -recover database is located /*.MYI

OPTIMIZE table or using the repair table command to repair, REPAIR TABLE table_name repair table optimization OPTIMIZE TABLE table_name table REPAIR TABLE table for repairing damaged.
After OPTIMIZE TABLE to reclaim unused database space, when the data row on the table is deleted, the disk space occupied and not immediately recovered, use the OPTIMIZE TABLE command these spaces will be recycled, and rows of data on the disk to rearrangement (Note: It is on the disk, rather than the database)

InnoDB row lock 14.MySQL the engine is completed by adding in what

InnoDB is based on the index to complete the line lock
Example: SELECT * from tab_with_index WHERE id =. 1 for Update;
for Update can be done row locks according to the conditions, and id is the column index key,
if id is not the index key that InnoDB completion table lock, concurrent will be impossible

15. The database optimization ideas

When we write SQL statements, in fact, the order of writing, the policy will affect SQL performance, although the realization of the function is the same, but their performance will be a little different.
Therefore, the following will explain at the time of writing SQL, how to write better.
① Select the most efficient sequence table name
resolver database processed in the order from right to left of the table name in the FROM clause, FROM clause written in the final table will be processed first
in the FROM clause contains multiple in the case of tables:

  • If three tables are completely unrelated, it will record and minimal table column names, written in the final, that is then followed by analogy: Choose the fewest number of records on the final table
  • If there are more than 3 tables join query: If three tables are related, it will most cited table, placed at the end, and so on. In other words: referenced by other tables table on the last
    example: query employee number, name, salary, salary grade, department name
  • most cited emp table, also the largest number of records, and therefore on the rearmost form words

②WHERE clause in the connection order
database using from right to left in order to resolve the WHERE clause, according to this principle, the connection between tables must be written in other WHERE condition Zhizuo, which can filter out the maximum number of conditions must be recorded in writing the right hand of the WHERE clause.
The rightmost emp.sal can filter multiple records, written in the WHERE clause

In ③SELECT clause to avoid the use of an asterisk
when we were learning, " " numbers can get all the fields in the data table.
 it through querying the data dictionary, which means that the more time-consuming
 Use
numbers to write SQL statements are also not intuitive.

④ with TRUNCATE DELETE alternative
here is only: delete all records of the table, in addition to the table structure to do so.
DELETE to delete a record, and Truncate is to delete the entire table, reserved table structure, so faster than DELETE

⑤ more use of internal SQL function to improve efficiency
such as using the mysql concat () function will be faster than using splicing ||, because concat () function has been optimized mysql before.

⑥ Use a table or column alias
name if table or column is too long, use some short aliases can slightly increase the number of SQL performance. After all, the character length to be scanned becomes less. . .

⑦ more use of the commit
comiit release roll back point ...

⑧ make good use of the index
Index is to improve our query data, when the amount of table records is very large, we can use the index.

⑨SQL capital write
us at the time of writing SQL, the official recommendation is to use the capital to write the keyword, because the Oracle server always turn first lowercase letters to uppercase only after execution

⑩ avoid the use of the index columns NOT
because Oracle Server after encountering NOT, he will stop current work and instead perform a full table scan
①① avoid the use of columns in the index calculation
WHERE clause, if the index column is part of the function , will not use the index to optimize the use of full table scan, which will become slower
with> = alternate>

Database structure optimization
1) Paradigm optimization: such as eliminating redundancy (space-saving ..)
2) trans Paradigm optimization: such as adding an appropriate redundancy (decrease the Join)
. 3) split table: Split vertical and horizontal resolution
server hardware optimization
What this slightly more money!

16.sql order of execution of sentence

Keywords used in the query contains six main, and their order was
select-from-where-group by -having-order by

17、union和union all

  1. UNION and UNION ALL keywords are the result of combining the two into one, but both the use and efficiency from it are different.
  2. Handling of repeatable results: UNION making table after links will filter out duplicate records, Union All does not remove duplicate records.
  3. Processing of the sort: Union will be sorted in order of the fields; UNION ALL simply merge the two results after return.
  4. From the efficiency, UNION ALL UNION much faster than, so, if you can confirm merger of two result sets do not contain duplicate data and does not require the sort of case, then use UNION ALL

18.mysql difference in the hash index and the index btree

  1. If the query is equivalent, then the hash index obviously has an absolute advantage, because only after one algorithm to find the appropriate key; of course, this premise is that the key is unique. If the key is not unique, it is necessary to find the location of the key is present, then the next scan according to the list until it finds the appropriate data;
  2. If the range is from a query, this time hash index on entirely useless, because originally ordered key, after hashing algorithm may become discontinuous, and no way to re-use indexing is complete range query;
  3. Similarly, there is no way hash indexes use the index to complete the order, and like 'xxx%' this part of the fuzzy queries (queries that partially obscured, in fact, the essence is the range query);
  4. Hash indexes do not support the left-most column joint multi-matching rule index;
  5. B + tree index keyword retrieval efficiency is relatively average, unlike the B-tree as large fluctuations, in the case of a large number of duplicate keys, hash index of efficiency is very low, because there are so-called hash collision.

19. Foreign keys

FOREIGN KEY constraints for preventing destruction of the connection between the operation table.
FOREIGN KEY constraints can prevent illegal data into the foreign key column, because it must be one of the values that it points in the table.

20. The transaction isolation level

Uncommitted Read (Read uncommitted): changes not committed allow reading the data. It may result in dirty, magic, non-repeatable read.
Read Committed (Read committed): After reading allowed concurrent transaction has been submitted. It prevents dirty reads, but phantom reads and non-repeatable reads still occur.
Repeatable Read (Repeatable read): multiple reads of the same field is the same, unless the data transaction itself is changed. Prevents dirty, non-repeatable read, phantom read but may still occur.
Serialize (Serializable): full compliance database four levels of isolation, dirty ensure that does not occur, magic, non-repeatable read. Slowest, it is completely locked by the data table involved in the transaction to complete the transaction performed in serial order.
Does not consider isolation, it will lead to problems:
dirty read : a transaction reads another transaction rewrite data but not committed, if the data is rolled back, the read data is invalid. Read invalid data, leading to inconsistent results before and after the query.
Non-repeatable read : In the same transaction, read another transaction has been submitted updated data. Read the updated data, resulting in the same data repeatedly read the result returned is different.
Phantom read : a transaction reads a few rows back, another transaction inserts some records, phantom read occurs. In subsequent queries, the first transaction would not have found that some of the original recording. Reading to a new record
MySQL's default transaction isolation level is Repeatable read

21.sql injection, how to avoid

sql injection : user-entered data, undetected, become part of sql statement, resulting in unexpected output.
Avoid sql injection : avoid data into the code is executed, to be pre-compiled sql statement query parameters and bound with a placeholder in the SQL statement, then with placeholders compiled SQL statement passed to the database '?' , performed only when the user input data as a parameter execution to the user. This operation not only makes the SQL statement is no longer required at the time of writing stitching, looks more directly, and the data entered by the user is also no chance of being sent to the SQL interpreter database is compiled executed, it will not become unauthorized code.

Example b: the user login name input screen: admin 'or 1 = 1'
On the next injection principle and achieve the login function module injected sql

22. Database Connectivity

Access the database via JDBC which includes the following steps?

  1. Load JDBC driver
  2. Provide URL JDBC connections
  3. Create a database connection
  4. Creating a Statement
  5. Execute SQL statements
  6. process result
  7. Close JDBC objects

23.inner join,left join,right join的区别

Table A TableB
aid adate bid bdate
1 a1 1 b1
2 a2 2 b2
3 a3 4 b4

from A * SELECT Inner the Join B = ON a.aid b.bid
extract only data matching results:
. 1 A1 B1
2 A2 B2

* from a SELECT left the Join B = ON a.aid b.bid
first remove all the data in a table, and then combined with a, b of the matching data, the result ::
. 1 A1 B1
2 A2 B2
. 3 A3 null character

from A * SELECT right the Join b = ON a.aid b.bid
refers b is first removed all the data in the table, and then combined with a, b data matching results:
. 1 A1 B1
2 A2 B2
. 4 null character b4

24.MongoDB and MySQL difference

MongoDB: log
storage: + virtual memory persistence.
Query: Query is a unique way of Mongodb.
Suitable scene: record the event, or blog content management platforms and so on.
Architectural features: high availability can be achieved by the replica set, and fragmentation.
Data processing: data is data stored on the hard disk, but require frequent read is loaded into memory, the physical memory to achieve high-speed memory read and write data.
It does not support transactions

25, the index underlying database implementation

  • 1. Database underlying implementation:
    the underlying structure is a B + tree, all the nodes are stored in the leaf node points to the next pointer, only the leaf nodes store data, InnoDB recommendations for most of the primary key table using the default increment as the primary index.

  • 2. Why is B + tree is not red-black tree or B-tree binary tree
    B-tree that is binary search tree:
    all non-leaf node has at most two sons, all nodes in a storage key, non-leaf node pointer to the left is smaller than the keyword the right pointer points to larger keyword
    B- tree i.e. a B-tree , do not read as B- tree, which is the search tree multiple
    keywords distributed throughout the tree, the search to find the binary equivalent performance, it is possible to search for a non- end leaf nodes of
    the B + tree is a variant B- tree , also multipath search tree
    all keywords in the leaf nodes, adding a chain pointer to all leaf nodes, increased B + tree leaf node list pointer is based on the B- tree All keywords appear in a leaf node, the non-leaf nodes as leaf nodes of the index, more suitable for document indexing system. With respect to the B-tree, the B + tree high utilization of space, since the internal node except the B + tree as an index, rather than the B- tree requires that each node pointer storage drives

26. The lock of classification

By size division : row-level cable, table-level locks, page-level search
by grade division of locks : shared locks, exclusive lock, update lock
according to use : optimistic locking, pessimistic locking
row-level cable : be added to the line of the current operation lock, reduce conflicts of database operations, spending large
page-level search : quick table-level locking speed, but more conflict, less row-level conflict, but slower. Therefore, to obtain a compromise of page-level page-level

These elements are reviewing when looking for internships in January and February knowledge, some sort of their own, some taken from the web page, if infringement, please inform! If wrong, please correct me! !

Guess you like

Origin blog.csdn.net/u013075024/article/details/93008609