[Introduction to Database System] Summary of SQL Statements

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

insert image description here

2.2 Schema definition & deletion

Schema: A layer of definition between database and tables.
insert image description here
insert image description here

2.3 Table definition & deletion & modification

insert image description here
insert image description here
insert image description here

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
insert image description here
insert image description here

Three, the basic syntax of SQL DQL

Now there are the following student course transcripts
insert image description here

3.1 Query all & query some columns

insert image description here

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

insert image description here
Note that during fuzzy query,
insert image description here
insert image description here
add desc when searching in descending order, if not add the default ascending order
insert image description here

3.5 Use of aggregate functions

insert image description here

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

insert image description here

3.7 Connection

3.7.1 Equivalent join

insert image description here

3.7.2 Self connection

insert image description here

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)
insert image description here
added:
1.full join full connection: merge the values ​​of left and right connections
insert image description here
2.union all does not deduplicate, union deduplication

3.7.8 Multi-table query

insert image description here

3.7.9 Nested queries

insert image description here

3.7.10 Subqueries with any all

insert image description here

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

insert image description here
insert image description here

3.7.11 having queries

insert image description here

3.7.12 Set query

And:
insert image description here
cross: insert image description here
difference:
insert image description here

Fourth, the basic syntax of SQL DML

4.1 Data insertion & deletion & modification

insert:
insert image description here
modify:
insert image description here
delete:
insert image description here

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.
insert image description here
insert image description here
insert image description here

Change the original table by updating the view:
insert image description here

Guess you like

Origin blog.csdn.net/qq_42425551/article/details/124060786