Database mysql of java interview collection

1. Inspection point

1. Connect

1.1 The concept of connection:

  Simply put, a join is a mechanism used to associate tables in a SELECT statement, so it is called a join.

1.2 Classification of connections

Note: The connection does not mean that only the use of the join keyword is a connection, and where is also an implementation of the connection.

1.2.1 Internal joins

  Inner joins are also called equivalence joins, which are based on equality tests between two tables, and are basically implemented in the following two formats:

  1.SELECT name,price FROM table 1 INNER JOIN table 2 ON table 1.id = table 2.id

  2.SELECT name,price FROM table1, table2 WHERE table1.id = table2.id

1.2.2 Self-Association

  The use case for self-joins is to query within the same table, and self-joins are often used as external statements to replace subqueries used to retrieve data from the same table. While the end result is the same, sometimes processing joins is much faster than processing subqueries.

  1.select id,name from table 1 where id = (select id from products where id = 'DTNTR') (sub query)

  2.select p1.id,p1.name from products as p1, products as p2 where p1.id = p2.id and p2.id = 'DTNTR'(self-connection)

1.2.3 Natural connection

  When joining tables, it is possible for a column to appear in more than one table, and a standard join returns all data, even the same column appears multiple times. Natural joins exclude multiple occurrences so that each column is returned only once.

  Implementation method: Generally, it is done by using wildcards for tables and using explicit subsets for all other table columns (personal understanding is to use full-qualification to indicate).

1.2.4 External joins

  Many joins associate rows in one table with rows in another table, but sometimes it is necessary to include rows that do not have associated rows, and include rows that have no associated rows in the related table. Such joins are called outer coupling

  Types of outer joins: There are two basic types of outer joins: left outer join and right outer join, the only difference between them is the order of the associated tables

  Implementation of external join: use OUTER JOIN to achieve, subdivided into two formats: 1.

    Right outer join: A right outer join B implementation effect: select all rows in the right table

    Left outer join: A left outer join B implementation effect: select all rows in the left table

2. View

  Concept: A view is a virtual table. My personal understanding is that it is not the entire table that has the highest access frequency, but the result set filtered out by certain conditions in the table. After these data are created using a view, the access rate is first improved. Speed, because it is no longer necessary to query the whole table, and only need to operate on the result set of the view. On the other hand, it greatly simplifies the complexity of SQL writing and avoids redundant query time brought by implementation methods such as sub-queries. .

  Basic operations of views:

    Create: CREAT VIEW AS +SELECT statement => The result set queried by the SELECT statement is stored in the view

    Use: access using the view name, the same as the use of the table

    Delete: DROP VIEW viewname

  Note: Can the data in the view be updated? The answer depends on the situation. In fact, adding or deleting rows to the view is actually adding or deleting rows to the base table. The following operations are defined in the view and cannot be updated, such as joins, subqueries, and so on.

3. Stored procedure

  Concept: In short, a collection of one or more MySQL statements that are saved for later use.

  Advantages: Simple, safe, high performance. Simple: It is no longer necessary to repeatedly establish a series of processing steps, which simplifies complex operations, similar to encapsulation, but the writing of stored procedures is much more complicated than basic SQL. Security: Reduces erroneous modification of data for other reasons. High performance: Find speed blocks.

  建立:CREATE PROCEDURE productpricing(OUT A1,IN A2)

     BEGIN

      SQL CODE

      INTO

     END

  Call: call productpricing(@parameter 1 (output variable name), 2000 (incoming data))

  delete: drop name

4. Cursor

  Concept: When we need to move forward or backward one line in the retrieved line, we need to use the cursor. A cursor is a database query stored on the MySQL server. It is not a SELECT statement, but a result set retrieved by the statement.

  Note: Cursors can only be used with stored procedures and functions

  Steps for usage:

    1. Declare the cursor DECLARE cursor name CURSOR

    2. Open the bid OPEN

    3. Use loops or judgment statements combined with FETCH to operate on the retrieved result set, which can be stored in different variables and returned according to requirements

    4. Close the cursor CLOSE

5. Triggers

  Concept: A trigger is a MySQL statement (or a group of statements between BEGIN and END) that is automatically executed when MySQL corresponds to DELETE, INSERT, and UPDATE.

  Creating a trigger requires four pieces of information: a unique trigger name, the table associated with the trigger, the activity that should be responded to (delete/insert/update), and when the trigger will execute (before or after processing)

  eg: CREATE TRIGGER trigger name AFTER INSERT ON table name

    FOR EACH ROW trigger action

6. Transactions

  A few terms about transactions:

    Transaction: refers to a group of SQL statements

    Rollback: The process of undoing the specified SQL statement

    Commit: Write unstored SQL statement results to database tables

    Retention point: refers to a temporary placeholder set in a transaction, which can be released or rolled back

7. Paradigm

The first three paradigms are the design specifications of relational databases, in order to maintain data integrity while minimizing data redundancy. Various paradigms present sub-standards (that is, the first paradigm is the basis of the second paradigm, and the second paradigm is the basis of the third paradigm). Wait to decide.
1) In the first normal form (1NF),
each attribute (each column value) is atomic and cannot be subdivided. That is, each row in the table only contains the information of one entity, and each column of each row can only store one attribute of the entity.
  Example: Student ID, Name, Gender, Class   201202031078
  Liang Zixuan, Male, Class 12, Class 2. The   above violates
  1NF . On the basis of , the attributes of the entity completely depend on the primary key (primary key), that is, there cannot be a part of the attribute that only depends on the primary key.   Example: Table A: Primary key attribute   Employee number, position → (decision) → name, age, education, basic salary   The above violates 2NF, and the relationship can be split into two tables:   Table B: employee number → (decision) → name, age, Education   Table C: Position → (Decision) → Base Salary 3) Third Normal Form (3NF) On the basis of 2NF, any non-primary attribute does not depend on other non-primary attributes (elimination of transfer function dependencies). The so-called transfer function dependency means that the key field A determines the non-key field B, the non-key field B determines the non-key field C, and the non-key field C transfer function depends on the key field A.   Example: employee number → (decision) → name, age, department number, department manager












  The above satisfies 2NF, but violates 3NF, because there are the following transfer function dependencies,
  employee number → (decision) → department number → (decision) → department manager can be
  split into the following two relationships (tables):
  A: employee number → ( decision) → name, age, department number
  B: department number → (decision) → department manager

8. Engine

9. Partition

Guess you like

Origin blog.csdn.net/weixin_43934939/article/details/114540083