Database common interview questions

1. What is the database three-paradigm?

      First Normal Form: Each field in the table cannot be further divided.

      Second Normal Form: The first normal form is satisfied and the non-primary key fields in the table all depend on the primary key fields.

      Third Normal Form: The second normal form is satisfied and non-primary key fields in the table must not be transitively dependent on the primary key fields.

2. What is a database transaction?

     Transactions have four characteristics: consistency, atomicity, isolation, and durability.

     A database transaction refers to: several SQL statements, either all of them are executed successfully, or all of them fail to be executed. For example, bank transfer is a typical scenario of a transaction.

     Three common commands for database transactions: Begin Transaction, Commit Transaction, and RollBack Transaction.

3. What is a view?

     A view is actually a virtual table composed of multiple table fields extracted from multiple tables through the Select query statement in the database.

  The view does not occupy physical space, so the records queried through the view are not stored in the view, but in the original table.

  The corresponding table fields can be hidden from the specified users through the view to protect the data.

   When certain conditions are met, the records in the original table can be added, deleted or modified through the view.

   When creating a view, only a single select query statement can be used.

4. What is an index?

   An index is a structure that sorts the values ​​of one or more columns in a database table, and uses an index to quickly access specific information in a database table.

  Index is divided into: clustered index, non-clustered index, unique index, etc.

   A table can have multiple unique indexes and non-clustered indexes, but only one clustered index at most.

   The index can contain multiple columns.

   Reasonable creation of indexes can improve the execution efficiency of query statements, but it reduces the speed of adding and deleting operations, and also consumes a certain amount of database physical space.

5. What is a stored procedure?

     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.

6. What is a trigger?

       Trigger is a special stored procedure, which is mainly triggered by events and executed. It enforces constraints to maintain data integrity and consistency, and tracks operations within the database so that unauthorized updates and changes are not allowed. 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.

7. Write a Sql statement: take out the 31st to 40th records in table A (MS-SQLServer)

    解1:select top 10 * from A where id not in (select top 30 id from A) 

        2:select top 10 * from A where id > (select max(id) from (select top 30 id from A )as A) 

     解3:select * from (select *, Row_Number() OVER (ORDER BY id asc) rowid FROM A) as A where rowid between 31 and 40

8. Write a Sql statement: take out the 31st to 40th records in table A (Mysql)

     select * from A limit 30, 10

9. Write a Sql statement: take out the 31st to 40th records in table A (Oracle)

     select * 

      from (select A.*,

     row_number() over (order by id asc) rank 

    FROM A)   

    where rank >=31 AND rank<=40;

10. How to describe a many-to-many relationship in a relational database?

    To describe a many-to-many relationship in a relational database, a third data table needs to be established. For example, when a student chooses a course, a course selection information table needs to be established on the basis of the student information table and the course information table, and the student ID and course ID are stored in the table.

11. What are database constraints, and what are the common constraints?

      Database constraints are used to ensure the integrity (correctness and consistency) of database table data. Data integrity can be guaranteed by defining constraints\index\triggers.

In general, constraints can be divided into:

Primary key constraint: primary key;

Foreign key constraint: foreign key;

unique constraint: unique;

check constraint: check;

Null constraint: not null;

Default value constraint: default;

12. List several commonly used aggregation functions?

Sum: Sum\Avg: Average\Max: Maximum value\Min: Minimum value\Count: Number of records

13. What are inner joins, left outer joins, and right outer joins?

Inner Join: Matches related records in two tables.

Left Outer Join: In addition to matching the related records in the two tables, it also matches the remaining records in the left table, and the unmatched fields in the right table are represented by NULL.

Right Outer Join: In addition to matching the related records in the two tables, it also matches the remaining records in the right table, and the unmatched fields in the left table are represented by NULL.

When judging the left table and the right table, the left and right positions of the Outer Join should appear according to the table name.

14. How to delete the records associated with the slave table when deleting the records of the master table?

If there is a primary-foreign key relationship between the two tables, when deleting a record in the primary key table, if there is an associated record in the slave table, the deletion will fail.

When defining a foreign key constraint, you can specify three deletion strategies at the same time: one is to delete the records from the table together (cascading delete); the second is to set the foreign key field from the table to NULL; the third is to delete the records from the table The key field is set to the default value.

Cascading delete example:

 

       alter table from table name

       add constraint foreign key name

      foreign key (field name) references main table name (field name)

      on delete cascade

15. What is a cursor?

A cursor is actually a mechanism for fetching one record at a time for processing from a result set that contains multiple data records.

Steps to use the cursor:

1. Define the cursor: declare cursor cursor name for select query statement [for {readonly|update}]

2. Open the cursor: open cursor

3. Operate data from the cursor: fetch... ... current of cursor

4. Close the cursor: close cursor

 

 

 

 

 

 

 

 

 

 

 

Guess you like

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