Database study notes (five, basic query)

Add, delete, modify and check tables:
Create, Retrieve, Update, Delete
1.Create

 INSERT [INTO] table_name 
 [(column [, column] ...)] 
 VALUES (value_list) [, (value_list)] ... 
 value_list: value, [, value] ...

When the insert fails because the value corresponding to the primary key or the unique key already exists, you can selectively perform a synchronous update operation.

INSERT ... ON DUPLICATE KEY UPDATE 
column = value [, column = value] ...

2.Retrieve

SELECT
          [DISTINCT] {* | {column [, column] ...} 
          [FROM table_name] 
          [WHERE ...] 
          [ORDER BY column [ASC | DESC], ...] 
LIMIT ...

Make an alias for the query structure:

SELECT column [AS] alias_name [...] FROM table_name;

Where condition
Database study notes (five, basic query)
Database study notes (five, basic query)
Note: Alias ​​cannot be used in where condition.
The result sorting is
ASC in ascending order (from small to large), DESC is in descending order (from large to small), and the default is ASC without setting.

SELECT ... FROM table_name [WHERE ...] 
         ORDER BY column [ASC|DESC], [...];

Note: For queries without an ORDER BY clause, the return order is undefined. Never rely on this order.
Note: You can use column aliases in the ORDER BY statement.
The
start index of the results of the filtering paging is 0
- starting from 0, filtering n results

SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n; 

- Starting from s, filter n results

SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n; 

- Starting from s, filter n results, which is more clear than the second usage, it is recommended to use

SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET n;

Note: When querying an unknown table, it is best to add a LIMIT 1 to avoid the database getting stuck due to too large data in the table and querying the full table data.
3.Update

UPDATE table_name SET column = expr [, column = expr ...] 
       [WHERE ...] [ORDER BY ...] [LIMIT ...]

4.Delete
1) Delete data

DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]

2) Truncate the table

TRUNCATE [TABLE] table_name

Note: Use this operation with caution

  1. It can only operate on the entire table, and cannot operate on part of the data like DELETE;
  2. In fact, MySQL does not operate on data, so it is faster than DELETE
  3. The AUTOINCREMENT item will be reset
    5. Insert query result
    INSERT INTO table_name [(column [, column ...])] SELECT ...

    6. Aggregate function
    Database study notes (five, basic query)
    7. The
    use of group by clause Use the group by clause in select to group and query the specified column

    select column1, column2, .. from table group by column;

    Interview questions:

    The order of execution of each keyword in SQL query?
    Answer: from> on> join> where> group by> with> having >select> distinct> order by> limit

Guess you like

Origin blog.51cto.com/14289397/2545109