【Interview】Database common interview questions

The role of triggers

Solution: Trigger is a special stored procedure, which is mainly executed through practice triggering. It can strengthen constraints to complete data integrity and consistency, and can track operations in the database, so that unauthorized access is not allowed. updates and changes. Can be cascaded. For example, a trigger on one table contains a data operation on another table, which in turn causes the trigger on that table to be fired.

What is a stored procedure and what is used to call it?

A stored procedure is a precompiled SQL statement, which has the advantage of allowing modular design, that is, it only needs to be created once and can be called multiple times in the program later. If an operation requires multiple SQL executions, using a stored procedure is faster than a simple SQL statement. **call** 1) A stored procedure can be called with a command object. 2} Can be called in external programs (such as java programs)

Advantages and disadvantages of stored procedures

Advantages:
1) The stored procedure is precompiled and has high execution efficiency.
2) The code of the stored procedure is directly stored in the database, and is called directly through the name of the stored procedure to reduce network communication.
3) For security, a user with certain permissions is required to execute the stored procedure.
4) Stored procedures can be reused, reducing the workload of database developers. Disadvantage: poor portability

difference between stored procedure and function

stored procedure function
Used to complete specific operations or tasks in the database (such as insert, delete, etc.) for specific data (such as selection)
procedure header declaration with procedure Program header declaration with function
The program header declaration does not need to describe the return type The return type should be described in the program header declaration, and at least one valid return statement should be enclosed in the PL/SQL block.
Parameters in three modes of in/out/in out can be used Parameters in three modes of in/out/in out can be used
Can be executed as a standalone PL/SQL statement cannot be executed independently, must be called as part of an expression
Can return zero or more values ​​via out/in out Return a value through the return statement, and the changed value must be consistent with the declaration part, or it can be a variable brought out by a parameter of type out
Stored procedures are not callable in SQL statements (DML or SELECT) Functions can be called in SQL statements (DML or SELECT)

What does the index do? And what are its pros and cons?

Index is a special query table, which can be used by database search to speed up data retrieval. It is very similar to the catalogue of books in real life. You can find the desired data without querying the entire book content. Indexes can be unique, and creating an index allows specifying a single column or multiple columns. The downside is that it slows down data entry and also increases the size of the database.
What kind of fields are suitable for indexing: unique, non-null, frequently queried fields

What are the index types

logically
Single column single row index
Concatenated multi-row index
Unique unique index
NonUnique non-unique index
Function-based function index
Domain domain index
Physically:
Partitioned partitioned index
NonPartitionefe nonpartitioned index
B-tree :
Normal Normal B-tree
Rever Key Reversed B-tree
Bitmap Bitmap index

What is a transaction? What is a lock?

A transaction is a grouping of SQL statements that are bound together as a logical unit of work. If any statement operation fails, the entire operation fails, and the operation will be rolled back to the state before the operation, or the previous node. To ensure that it either executes or it doesn't, transactions can be used. To consider a SQL statement as a transaction, it needs to pass the ACID test, that is to say, the transaction (atomicity, consistency, isolation, and durability) is
locked in all DBMSs. Locks are the key to realizing transactions, and locks can guarantee Transaction integrity and concurrency, like locks in real life, can prevent the owner of certain data from using certain data or data structures for a certain period of time.

What is a view? What is a cursor?

View: is a virtual table with the same function as a physical table. Views can be added, deleted, modified and searched. A view is usually a subset of the rows and columns of a table or multiple tables. Compared to multi-table queries, it makes getting data easier.

Cursor:
The result of the query is effectively processed as a result set. 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. The current row of the result set can be modified. (It is generally not applicable to cursors, but when dealing with data one by one, cursors are very important)

Advantages of views:

  • 1) Access to the database, because the view can selectively select a part of the database.
  • 2) Users can obtain results from complex queries through simple queries.
  • 3) To maintain the independence of data, views can retrieve data from multiple tables.
  • 4) The same data can be presented through different views.

Disadvantages of views:
When querying a view, the view query must be converted into a query on the basic table. If the view is defined by a complex multi-table query, it will be more troublesome to change the data, or the view cannot be modified at all. data.

Several table connection methods, what is the difference?

Inner join, self join, outer join, cross join

Inner join: Only two element tables that match can be displayed in the result set.

Outer join:
( Left outer join : The data table on the left is the main table, all the data in the main table is displayed, the data table on the right is the matching table, and the data in the matching table can only be displayed if the data matches)
( Right outer join : The data table on the right is displayed) It is the main table, similar to the left outer join)
( full outer join : only display the data in both the main table and the matching table)
( Cartesian effect : the displayed result is the product of the number of linked tables)

What is the difference between primary key and foreign key?

The primary key is unique in this table and cannot be null, and the foreign key can be duplicated and null. A foreign key is associated with a primary key in another table, and a foreign key that does not exist in the corresponding table cannot be created.

How to optimize the query statement?

1. Create an appropriate index.
2. Reduce the association between tables.
3. Try to avoid full table query, useless fields, and control the returned results.
4. Try to use PreparedStatement query instead of Statement.

What are the three paradigms of database?
First Normal Form: Columns are not subdivided.
Second Normal Form: Rows can be uniquely distinguished, primary key constraints.
The third normal form: the non-primary attributes of a table cannot depend on the non-primary attributes of other tables, foreign key constraints, and the three normal forms are level-by-level dependent, the second normal form is based on the first normal form, and the third normal form is based on One or two paradigms.

What is the difference between union and union all?

Union and union all both combine two result sets into one.
1. Union will filter out duplicate records after table linking, but union all will not.
2. Union will sort according to the order of the fields; union all will not, just simply combine the two results and return them.

Note: In terms of efficiency, union all does not deduplicate data, so it is more efficient than union.
Difference between varchar2 and varchar?
The length of char is fixed, and the length of varchar2 can be changed. For example, storing the string "abc" for char(20) means that the character you store will occupy 20 bytes, including 17 spaces, and the same varchar2(20) only takes up 3 bytes, 20 is only the maximum value, when the characters you store are less than 20, they are stored according to the actual length. char is more efficient than varchar2. Currently varchar is a synonym for varchar2, the industry standard varchar type can store empty strings

The role of sequence?

Oracle uses sequences to generate unique numbers for processing auto-incrementing fields in a table. oracle sequences are atomic objects and are consistent. That is, once a sequence number is accessed, oracle will automatically increment the next number before processing the next request, ensuring that there are no duplicate values.

The relationship between tables and views?

A view is actually a query SQL statement used to display related data in one or more tables or other views. Tables are what actually store data in relational databases.

oracle basic data types?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325798188&siteId=291194637