当前位置:网站首页>Some concepts often asked in database interview

Some concepts often asked in database interview

2022-07-06 11:45:00 Ride the wind to break the bug

Super key 、 Candidate key 、 Primary key 、 Foreign keys

  • Super key : The set of attributes that uniquely identify tuples in a relationship is called the superkey of the relationship pattern , An attribute can be used as a super key , Multiple attributes can also be combined as a superkey , Superkeys contain candidate keys and primary keys
  • Candidate key : It's the smallest superbond , Hyperkeys without redundant elements
  • Primary key : A combination of data columns or attributes in a database table that uniquely and completely identifies the stored data object , A data column can only have one primary key , And the value of primary key cannot be missing , Cannot be null (null)
  • Foreign keys : The primary key of another table in one table is called the foreign key of this table

What is business ? What is a lock ?

  • Business : Is bound together as a logical unit of work SQL Statement grouping , If any statement operation fails, the whole operation will fail , The previous operation will be rolled back to the pre operation state , Or there is a node on . In order to make sure that or carry out , Or not , You can use transactions . Consider having groups of statements as transactions , It needs to pass ACID test , Atomicity , Uniformity , Isolation and persistence
  • lock : Of all the DBMS in , Lock is the key to transaction , Locks guarantee the integrity and concurrency of transactions , Just like the lock in real life , It can make the owner of some data , Certain data or data structures cannot be used for a certain period of time , Of course, the lock is also graded .

Four characteristics and meanings of database transaction

  • Atomicity : All operations in the whole transaction , Or it's all done , Or not at all , It's impossible to stop in the middle , An error occurred during the execution of the transaction , Will be rolled back (Rollback) Status to the beginning of the transaction , It's like this transaction has never been executed .
  • Uniformity : Before and after transaction start , Data integrity constraints are not broken
  • Isolation, : Isolated state execution transactions , Make them seem like the only operation that the system performs at a given time , If there are two things , Run at the same time , Perform the same function , The isolation of transactions ensures that each transaction in the system believes that only this transaction is using the system , This property is sometimes serialized , To prevent confusion between transaction operations , You must serialize the call or request , Make only one request for the same data at the same time
  • persistence : After the transaction is completed , Changes made by the firm to the database are persisted in the database , Will not be rolled back

What is a view ?

A view is a virtual table , It has the same function as the physical table , You can add, delete, modify and query the view , Views usually have a table or a subset of rows or columns of multiple tables , Changes to the view do not affect the base table , It makes it easier for us to get data , Compared to multi table query
The following two scenarios generally use views :

  1. You don't want visitors to get information about the entire table , Only some fields and visitors are exposed , So build a virtual table , It's the view
  2. The query data comes from different tables , And searchers want to query in a unified way , This can also create a view , Combine multiple table query results , The searcher just needs to get the data directly from the view , It's not necessary to consider the differences caused by different tables

Be careful : This view is created in the database , Not created in code

Function of trigger ?

Trigger is a special stored procedure , It is mainly triggered by events and executed , It can strengthen constraints , To maintain data integrity and consistency , You can track operations within the database so that unauthorized updates and changes are not allowed , Can be concatenated , Such as , Triggers on one table contain data operations on another , This operation will cause the trigger of the table to be triggered

Maintain the integrity and consistency of the database , Do you prefer to use triggers or write your own business logic ? Why? ?

Use constraints as much as possible , Such as check, Primary key , Foreign keys , Non empty fields, etc , It's the most efficient , Is the most convenient , The second is to use triggers , This method can guarantee , No matter what business system accesses the database, it can ensure the integrity and consistency of data , Finally, I will consider writing business logic , But it's more troublesome , Programming complexity , inefficiency .

The function of index ? And its advantages and disadvantages ?

Database index , It is a sort data structure in database management system , To assist in quick query , Update data in database tables , The implementation of index usually uses B Trees and their varieties B+ Trees
Out of data , The database system also maintains a data structure that satisfies a specific search algorithm , These data structures are referenced in some way ( Point to ) data , In this way, advanced search algorithms can be implemented on these data structures , This data structure , It's index.

Indexing tables comes at a price : One is to increase the storage space of the database , Second, it takes more time to insert and modify data ( Because the index has to change with it )
advantage : Creating indexes can greatly improve system performance ( advantage )

  • First of all , By creating a unique index , Ensure the uniqueness of each row of data in the database table
  • second , Can greatly speed up the retrieval of data , This is also the main reason for creating an index
  • Third , It can speed up the connection between the meter and the meter , In particular, it is of great significance to realize the referential integrity of data
  • Fourth , When using grouping and sorting clauses for data retrieval , It can also significantly reduce the time of grouping and sorting in queries
  • The fifth , By using index , During the process of query , Using the optimize hide tool , Improve system performance

shortcoming

  • First of all : Creating and maintaining indexes takes time , This time will increase as the amount of data increases
  • second : Indexes need to occupy physical space , In addition to the fact that data tables occupy data space , Each index takes up a certain amount of physical space , If you want to build a clustered index , So much more space is needed
  • Third : When adding data in the table , When deleting and modifying , Index also needs dynamic maintenance , This reduces the speed of data maintenance

An index is based on some columns in a database table , When creating an index , You should consider which columns you can create indexes on , On which columns indexes cannot be created
(1) On columns that often need to be searched , Can speed up the search
(2) On the index as the primary key , Enforce the uniqueness of this column and the table column structure of the data in the organization table
(3) On columns that are often used in joins , These columns are mainly some foreign keys , Can speed up the connection
(4) Create indexes on columns that often need to be searched by range , Because the index is sorted , Its specified range is continuous
(5) Create indexes on columns that often need to be sorted , Because the index is sorted , In this way, the query can take advantage of index sorting , Speed up sorting query time
(6) It is often used in where Create an index on the column in clause , Speed up the judgment of conditions

Again , Some columns should not be indexed :
First of all , Indexes should not be created for columns that are rarely used or referenced in queries . This is because , Since these columns rarely use , So there's an index or no index , It doesn't improve the query speed . contrary , With the addition of index , On the contrary, it reduces the maintenance speed of the system and increases the space demand .

second , The index should not be increased for columns with few data values . This is because , Because these columns have very few values , For example, the gender column in the personnel table , In the results of the query , The data rows in the result set account for a large proportion of the data rows in the table , That is, a large proportion of data rows need to be searched in the table . Add index , It can not speed up the retrieval speed obviously .

Third , For those defined as text, image and bit Columns of data type should not be indexed . This is because , The amount of data in these columns is either quite large , Or the value is very small .

Fourth , When modification performance is much greater than retrieval performance , Index should not be created . This is because , Modification performance and retrieval performance are contradictory . When increasing the index , Will improve retrieval performance , But it will slow down the modification performance . When reducing the index , Will improve modification performance , Reduce retrieval performance . therefore , When modification performance is much greater than retrieval performance , Index should not be created .

drop,delete And truncate The difference between

  • drop Delete the table directly
  • truncate Delete data in table , Automatically increase when inserting again id Again from 1 Start
  • delete Delete data in table , You can add where Clause
  1. delete Statement Deletes one row at a time from the table , At the same time, the operation of this line is recorded in the log as a transaction for rollback .truncate table It is to delete all data from the table at one time, and do not record the individual deletion operations in the log , Deleted rows cannot be restored , And the delete trigger related to the table will not be activated during the deletion process , Fast execution
  2. Table and index space , When the watch is truncate after , The space occupied by this table and index will return to its original state , and delete The operation does not reduce the space occupied by the table or index ,drop Statement to free up all the space occupied by the table .
  3. generally speaking ,drop>truncate>delete
  4. Range of application ,truncate Only right table;delete It can be table and view
  5. truncate and delete Delete data only , and drop Delete the entire table ( Structure and data )
  6. truncate With or without where Of delete: Delete data only , Without deleting the structure of the table ( Definition )drop Statement will delete the constraint that the structure of the table is dependent on (constrain), trigger (tigger) Indexes (index); Stored procedures that depend on the table / The function will be reserved , But its state will change to :invalid
  7. delete Statement for DML(data maintain Language) This operation will be put into rollback segment in , The transaction will not take effect until it is committed , If there is a corresponding tigger, Execution will be triggered
  8. truncate,drop yes DLL(data define language), The operation takes effect immediately , Metadata is not put in rollback segment in , Cannot be rolled back
  9. Without backup , Use caution drop And truncate, To delete some rows of data, use delete And pay attention to the combination of where To limit the scope of influence , The rollback segment should be large enough , To delete a table, use the drop, If you want to keep the table and delete the data in the table , If it has nothing to do with the business , use truncate That is to say , If it's about business , Or want to trigger tigger, Or use it delete
  10. truncate table Table name , Fast , And it's efficient , because :truncate table In function and without where Clause delete Same statement , Both delete all rows in the table , but truncate Than delete fast , It also uses less system and transaction log resources ,delete Delete one line at a time , And record one entry for each Deleted Row in the transaction ,truncate table Delete by releasing the data pages used to store table data , And only record the release of the page in the transaction log
  11. truncate table Delete all rows in the table , But the structure of the table and its columns , constraint , Index and so on remain unchanged , The count value used for the new row ID is reset to the zongzi of the column , If you want to keep the ID count , Please switch to delete, If you want to delete the table definition and its data , Please use drop table sentence
  12. For the FOREIGN KEY Constraint referenced table , Out of commission truncate table, It should be used without where Clause delete sentence , because truncate table Not in the Journal , So he can't activate the trigger
原网站

版权声明
本文为[Ride the wind to break the bug]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060913128038.html