Basic SQL commands, important SQL commands, SQL constraints and execution order of SQL statements

learning target:

学习目标如下:

  • SQL statement execution order

Learning Content:

Basic SQL commands:`

  1. FROM
  2. ON
  3. JOIN
  4. WHERE
  5. GROUP BY
  6. AGG_FUNC
  7. WITH
  8. HAVING
  9. SELECT extracts data from the database
  10. UNION
  11. DISTINCT
  12. ORDER BY sorting
  13. LIMIT

Important sql commands:

1. SELECT - extract data from the database

2. UPDATE - update the data in the database

3. DELETE - delete data from the database

4. INSERT INTO - Insert new data into the database

5. CREATE DATABASE - create a new database

6. ALTER DATABASE - modify the database

7. CREATE TABLE - create a new table

8. ALTER TABLE - change (change) database table

9. DROP TABLE - delete a table

10. CREATE INDEX - create an index (search key)

11. DROP INDEX - delete the index

sql constraints:

1. NOT NULL - Indicates that a column cannot store NULL values.

2. UNIQUE - Guarantee that each row of a column must have a unique value.

3. The combination of PRIMARY KEY - NOT NULL and UNIQUE. Ensuring that a column (or a combination of two or more columns) is uniquely identified makes it easier and faster to find a specific record in a table.

4, FOREIGN KEY - to ensure that the data in one table matches the referential integrity of the values ​​in another table.

5, CHECK - to ensure that the values ​​in the column meet the specified conditions.

6. DEFAULT - Specifies the default value when no value is assigned to the column.

Summarize:

  • 1. SQL statement execution sequence: FROM, ON, JOIN, WHERE, GROUP BY, AGG_FUNC, WITH, HAVING, SELECT, UNION, DISTINCT, ORDER BY, LIMIT.

In the actual execution process, each step will generate a virtual table for the next step, and this virtual table will be used as the data of the next execution step.

1. FROM : Select the table followed by FROM to generate virtual table 1.

2. ON : ON is the connection condition of JOIN, and the rows that meet the connection condition will be recorded in virtual table 2.

3. JOIN : If LEFT JOIN is specified, the unmatched rows in the reserved table will be added to virtual table 2 as external rows, resulting in virtual table 3. If there are multiple JOIN links, steps 1~3 will be repeated until all tables are processed.

4. WHERE : Perform WHERE condition filtering on virtual table 3, and records that meet the conditions will be inserted into virtual table 4.

5. GROUP BY : According to the columns in the GROUP BY clause, the records in the virtual table 4 are grouped to generate virtual table 5.

6. AGG_FUNC : Commonly used Aggregate functions include the following types: (AVG: return the average value), (COUNT: return the number of rows), (FIRST: return the value of the first record), (LAST: return the value of the last record ), (MAX: returns the maximum value), (MIN: returns the minimum value), (SUM: returns the sum).

7. WITH applies the ROLLUP or CUBE option to virtual table 5 to generate virtual table 6.

8. HAVING : Perform HAVING filtering on the virtual table 6, and the qualified records will be inserted into the virtual table 7.

9. SELECT : SELECT is only executed at one step, selects the specified column, and inserts it into virtual table 8.

10. UNION : The two SELECT query statements connected by UNION will repeat steps 1~9 to generate two virtual tables 9, and UNION will merge these records into virtual table 10.

11. DISTINCT removes duplicate rows from virtual table 10 to generate virtual table 11. DISTINCT is used to delete duplicate rows and only keep unique ones.

12. ORDER BY: Sort the records in virtual table 11, virtual table 12.

13. LIMIT : Take out the records of the specified row and return the result set.

  • 2. Query data

(1) Basic query

SELECT * FROM <table name>;

(2) Projection query

SELECT column_name,column_name
FROM table_name;

(3) The query result returns a unique value

SELECT DISTINCT column_name,column_name
FROM table_name;

(4) Condition query

SELECT * FROM <table name> WHERE <conditional expression>;

(5) Sorting

SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;

(6) Paging query

SELECT column_name(s)
FROM table_name
LIMIT number OFFSET 起始值;

(7) Aggregation query

1. COUNT() counts the amount of data in a table

SELECT COUNT(*) FROM <table name> WHERE <conditional expression>;

2. SUM() calculates the total value of a column, which must be of numeric type

SELECT SUM(*) FROM <table name> WHERE <conditional expression>;

3. AVG() calculates the average value of a column, which must be of numeric type

SELECT AVG(*) FROM <table name> WHERE <conditional expression>;

4. MAX() calculates the maximum value of a column

SELECT MAX(*) FROM <table name> WHERE <conditional expression>;

5. MIN() calculates the minimum value of a column

SELECT MIN(*) FROM <table name> WHERE <conditional expression>;

6. The GROUP BY statement is used to combine aggregate functions to group the result set according to one or more columns

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

7. The HAVING clause allows us to filter each group of data after grouping.

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value;

(8) Multi-table query (Cartesian query)

SELECT * FROM <表1> <表2>;

(9) Connection query

SELECT … FROM tableA JOIN tableB ON tableA.column1 = tableB.column2;

(10) Merge query

SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

(11) AND & OR operators are used to filter records based on more than one condition.

(12) The WHERE clause is used to filter records.

(13) The ORDER BY keyword is used to sort the result set.

(14) The LIKE operator is used to search for the specified pattern in the column in the WHERE clause.

SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;

(15) Wildcards can be used to substitute for any other character in a string. (%: replace 0 or more characters, _: replace one character)

(16) The IN operator allows you to specify multiple values ​​in the WHERE clause.

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,…);
IN 与 = 的异同

The same point: both are used as one of the filter conditions in WHERE, and both have the same meaning.
The difference: IN can specify multiple values, which is equal to specifying a value

(17) The BETWEEN operator is used to select values ​​within the data range between two values.

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

(18) Alias

Column aliases:

SELECT column_name AS alias_name
FROM table_name;

Table aliases:

SELECT column_name(s)
FROM table_name AS alias_name;

(19) MySQL Date function

MySQL uses the following data types to store dates or date/time values ​​in the database:

DATE - Format: YYYY-MM-DD
DATETIME - Format: YYYY-MM-DD HH:MM:SS
TIMESTAMP - Format: YYYY-MM-DD HH:MM:SS
YEAR - Format: YYYY or YY

The most important date functions in MySQL are shown in the table below:

insert image description here
After we understand the execution sequence of sql statements, we can improve the query efficiency of sql statements by combining the execution sequence of sql statements when writing sql query statements.

Guess you like

Origin blog.csdn.net/YHLSunshine/article/details/130250808
Recommended