Table of contents:
-
- 1. The basic concept of SQL
- Second, the basic syntax of SQL DDL
- Three, the basic syntax of SQL DQL
-
- 3.1 Query all & query some columns
- 3.2 Rename the column
- 3.3 Result deduplication
- 3.4 Add conditions to query results
- 3.5 Use of aggregate functions
- 3.6 Group query
- 3.7 Connection
- 3.7.1 Equivalent join
- 3.7.2 Self connection
- 3.7.3 Outer joins
- 3.7.8 Multi-table query
- 3.7.9 Nested queries
- 3.7.10 Subqueries with any all
- 3.7.11 having queries
- 3.7.12 Set query
- Fourth, the basic syntax of SQL DML
- Five, the basic syntax of SQL - data control function DCL
1. The basic concept of SQL
SQL is the abbreviation of Structured Query Language, which means Structured Query Language, which
is a language for querying or changing the data in the database management system
- Data definition language DDL (Data Ddefinition Language)
SQL data definition language is mainly used to define logical structures, including defining base tables, views and indexes. delete table definition table modify table - Data Query Language DQL (Data Query Language) The data query language of SQL is mainly used to query various data objects in the database.
- Data Manipulation Language DML (Data Manipulation Language)
SQL data manipulation language, used to change the data in the database, including inserting, deleting, modifying - Data control function DCL (Data Control Language)
SQL data control language, authorization to tables and views, description of integrity rules, and control statements such as transaction start and end.
Second, the basic syntax of SQL DDL
2.1 SQL data types
2.2 Schema definition & deletion
Schema: A layer of definition between database and tables.
2.3 Table definition & deletion & modification
2.4 Index creation & modification & deletion
When the amount of data is relatively large, the query takes a long time. Building an index can effectively reduce the time consumption. An
index can be built on one or more columns
Three, the basic syntax of SQL DQL
Now there are the following student course transcripts
3.1 Query all & query some columns
3.2 Rename the column
select name 姓名 from student;
select name as 姓名 from student;
3.3 Result deduplication
select distinct title from student
3.4 Add conditions to query results
Note that during fuzzy query,
add desc when searching in descending order, if not add the default ascending order
3.5 Use of aggregate functions
select count(title) from edu_course;
select count(distinct title) from edu_course;//去除重复再数个数
select AVG(price) from edu_course;
select Max(price) from edu_course;
select MIN(price) from edu_course;
3.6 Group query
3.7 Connection
3.7.1 Equivalent join
3.7.2 Self connection
3.7.3 Outer joins
Both are used when connecting sub-tables. If the field names used in the on clause are the same, that is, a.id = b.id, and you don’t want the result to have two id columns , you can use using to simplify Input, using(id)
added:
1.full join full connection: merge the values of left and right connections
2.union all does not deduplicate, union deduplication
3.7.8 Multi-table query
3.7.9 Nested queries
3.7.10 Subqueries with any all
Any:>Any means greater than at least one value, i.e. greater than the minimum value.
All: >All means greater than every value. In other words, it means greater than the maximum
3.7.11 having queries
3.7.12 Set query
And:
cross:
difference:
Fourth, the basic syntax of SQL DML
4.1 Data insertion & deletion & modification
insert:
modify:
delete:
Five, the basic syntax of SQL - data control function DCL
5.1 View
A view (VIEW) is also called a virtual table, that is, a virtual table. It is a logical representation of a set of data. Its essence is that a SELECT result set is assigned a
name, that is, the name of the view.
The view itself does not contain any data, it only contains a query statement mapped to the base table. When the data in the base table changes, the purpose of the view: to facilitate and
simplify data operations.
When our business needs to find multiple tables Data, at this time we may associate multiple tables for query processing.
Change the original table by updating the view: