Comprehensive summary of SQL statements in MySql database

SQL language is divided into four categories: data query language DQL, data manipulation language DML, data definition language DDL, data control language DCL. The following is a small summary of these four languages ​​and transaction processing languages

1. Data Definition Language-DDL

  • Operations on the structure of the database or database components (DATA DEFINITION LANGUAGE):
  • Features: Does not allow transaction rollback.

1.1 CREATE-table creation

Syntax
CREATE TABLE table name (
field name data type constraint,
field name data type constraint
)

1.2 DROP-delete the table

DROP TABLE table name

1.3 ALTER-change table

1.4 TRUNCATE-truncated table

  • Truncation of the table, clear all records in the table.

  • Syntax structure:
    TRUNCATE TABLE table name

  • The difference between TRUNCATE and DELETE

  1. TRUNCATE is DDL, you can only delete all records in the table, release storage space, and ROLLBACK cannot roll back.
  2. DELETE is DML, you can delete the specified record without releasing storage space, and you can roll back using ROLLBACK.

2. Data manipulation language-DML

  • Data Manipulation Language (DML) is mainly used to implement operations on data in database tables-addition, deletion, and modification
  • Data manipulation languages ​​mainly include the following:
    • Add row data: use INSERT statement to
    modify row data: use UPDATE statement to achieve
    • delete row data: use DELETE statement to achieve
  • Features: can perform transaction rollback

2.1 INSERT to insert records into the table

  • Syntax:
    INSERT INTO table name (field list) VALUES (value list)
  • Among them:
    1. Field list and value list are separated by commas.
    2. Values ​​in the value list, string and date constants, need to be enclosed in single quotes.
    3. The type of value must match the data type of the field.
    4. INTO can be omitted.
    5. If the field list is a field inserted into the entire table, it can be omitted, but the number and order of the value list should be in accordance with the order of the defined table fields same.
    6. The field can also write a few of them, which corresponds to the number of values ​​that follow.
    7. VALUES keyword can also use VALUE, but generally use values

Or see the picture below:
Insert picture description here
For example :
Insert picture description here
Insert picture description here

Insert multiple records in batch

  • Use the insert statement to batch insert multiple records into the table at one time. The syntax format is as follows.

INSERT INTO table name (field name) VALUES (value list 1), (value list 2),..., (value list n);

  • E.g:
    Insert picture description here

2.2 UPDATE clause to modify the data in the database

  • Modifying data is mainly used to modify the column data of certain rows in the table according to specified conditions.

  • Modify the data using the UPDATE clause, the syntax structure is as follows:
    UPDATE table name SET field name 1=value 1, field name 2=value 2 WHERE clause

  • Note :

    1. The where clause is used to limit the modified records. If no where clause is added, all records in the table are modified by default.
    2. The SET clause is used to limit which columns to modify.
    3. Multiple records can be modified at onceInsert picture description here
    4. Pay attention to integrity constraint errors when making modifications

2.3 The DELETE statement deletes rows that meet the conditions in the table

  • Delete data is mainly used to delete certain rows from the table according to specified conditions.
  • Syntax
    DELETE FROM table WHERE clause
  • E.g:

a. Delete the selected record

Insert picture description here

b. Delete all records

Insert picture description here

When deleting records, also pay attention to integrity constraint errors


3. Data query language-DQL (DQL DATA QUERY LANGUAGE)

For detailed DQL instructions, please see this article: MySQL Statement-Data Query Language DQL Detailed Explanation

  • Basic SELECT statement syntax:
    SELECT [DISTINCT]{*|column|expression [alias],…} FROM table where clause;
  • note:
    1. The SELECT clause indicates the data column to be retrieved.
    2. The FROM clause indicates which table the retrieved data comes from.
  • For example:
    Insert picture description here

4. Data control language-DCL (DATA CONTROL LANGUAGE)

  • GRANT
  • REVOKE

5. Transaction processing language-TPL (TRANSACTION PROCESS LANGUAGE)

For a detailed explanation of TPL, please see this article: MySql Office TPL Summary

  • Transaction Process Language: Transaction Process Language, TPL for short, is mainly used to confirm or cancel the operation results of DML statements that constitute a transaction. Confirmation means to make the DML operation take effect, which is realized by using the COMMIT command; Cancellation means to make the DML operation invalid, which is realized by using the ROLLBACK command.

  • Automatic transaction commit mode

    1. Transactions in mysql are automatically committed, that is, COMMIT statements are added by default after each sql statement
    2. View the automatic commit mode of mysql transaction: SHOW VARIABLES LIKE'autocommit'
    3. Modify the automatic commit mode of mysql
      SET AUTOCOMMIT=1 means open
      SET AUTOCOMMIT=0 means close
  • There are two main methods for MySQL transaction processing

    1. Use begin, rollback, commit to achieve:
      begin to start a transaction
      rollback transaction rollback
      commit transaction commit

    2. Use set directly to change the automatic submission mode of
      MySQL MySQL defaults to automatic submission, that is, if you submit a sql, it will be executed directly! You can use set autocommit = 0 to prohibit automatic submission set autocommit = 1 to enable automatic submission to achieve transaction processing.

    But note that when set autocommit = 0 is used, all subsequent sql will be processed as a transaction until the commit is confirmed or rollback is ended. Note that when this transaction is ended, a new transaction is also opened! According to the first method, only the current one is a transaction!

Look at the official, if you think this article is not bad, click a thumbs up and pay attention and then go ^-^
MySql series of articles:

  1. Basic operation of MySql database (1)
  2. MySql database management operation basic statement summary
  3. Summary of constraints when building tables in MySql
  4. Summary of data types in MySql
  5. A small summary of mySql transaction processing TPL
  6. MySQL statement-data query language DQL detailed
  7. Comprehensive summary of SQL statements in MySql database
  8. Summary of the most basic and most commonly used functions in MySql

Guess you like

Origin blog.csdn.net/qq_45768060/article/details/108586052