当前位置:网站首页>SQL most commonly used basic operation syntax
SQL most commonly used basic operation syntax
2022-07-28 12:53:00 【it_ hao528】
One 、 brief introduction
SQL It's English Structured Query Language Abbreviation , Structured query language , A programming language for operating relational databases , It is a unified standard for defining and operating all relational databases , For the same need , There may be some differences in each way of database operation , This is called a “ dialect ”.
General grammar :SQL Statements can be written in one or more lines , With a semicolon ; ending .MySQL Database SQL Statement is case insensitive . You can add comments , Where single line comments use -- The comment perhaps # The comment (MySQL specific ) Multiline comments use /* The comment */
Two 、 Database operation statement
1、 Inquire about
Query all databases
show databases;
2、 establish
Create database
create database Database name ;
Create database ( Judge , Create... If it doesn't exist , This method is generally used to avoid mistakes )
create database if not exists Database name ;
3、 Delete
Delete database
drop database Database name ;
Delete database ( Judge , Delete... If it exists , Generally, this method is used to delete to avoid errors )
drop database if exists Database name ;
4、 Use
View currently used databases
select database();
Using a database
use Database name ;
3、 ... and 、 Table operation statement
1、 Inquire about
Query the names of all tables in the current database
show tables;
Query table structure
desc The name of the table ;
2、 establish
Create table
create table Table name (
Field name 1 data type 1,
Field name 1 data type 1,
...
Field name n data type n
);
Be careful : You cannot add a comma at the end of the last set of data ,
MySQL Support multiple data types , Here is a table :
3、 Delete
Delete table
drop table Table name ;
Delete table ( Judge , Delete... If it exists , Generally, this method is used to delete to avoid errors )
drop table if exists The name of the table ;
4、 modify
Modify the name of the table
alter table Table name rename to The new name of the table ;
Add a column
alter table Table name add Name data type ;
Modify column data type
alter table Table name modify Name New data types ;
Change column name and data type
alter table Table name change Name New column names New data types ;
Delete column
alter table Table name drop Name ;
Four 、 Table data operation statement
1、 Add data
Add data to the specified column
insert into Table name ( Name 1, Name 2, ...) values( value 1, value 2, ...);
Add data to all columns
insert into Table name values( value 1, value 2, ...);
Batch add data
insert into Table name ( Name 1, Name 2, ...) values( value 1, value 2, ...), ( value 1, value 2, ...), ...;
insert into Table name values( value 1, value 2, ...), ( value 1, value 2, ...), ...;
2、 Modify table data
Modify the specified column data ( Be careful : If no condition is added in the modification statement , Then all data in the corresponding column will be modified )
update Table name set Name 1= value 1, Name 2= value 2, ... where Conditions ;
3、 Delete table data
Delete data ( Be careful : Delete statements without conditions , Then all data will be deleted )
delete from Table name where Conditions ;
4、 Query table data
a. Basic query
Query multiple fields
select Name 1, Name 2, ... from Table name ; -- Query the specified column data
select * from Table name ; -- Query all column data
Remove duplicate record queries
select distinct Name 1, Name 2, ... from Table name ;
List alias queries (as You can omit it , The output column name is the alias of )
select Name 1 as Alias , Name 2, ... from Table name ;
b. Conditions of the query
according to where Conditions of the query
select Field list from Table name where List of conditions ;
The table below for where Common post condition symbols and functions 
c. Sort query
sort order :
ASC: Ascending order ( The default value is )
DESC: Descending order
select Field list from Table name order by Sort field names 1 sort order 1, Sort field names 2 sort order 2, ...;
Be careful : If there are multiple sorting conditions , When the condition values of the current edge are the same , Will be sorted according to the second condition
d. Aggregate function query
Concept : Take a column of data as a whole , Do the longitudinal calculation .
Aggregate functions are classified in the following table 
The grammar is as follows :
select Aggregate function name ( Name ) from Table name ;
e. Group query
select Field list from Table name where Conditions before grouping are limited group by Group field name having Conditional filtering after grouping ;
Be careful : After grouping , The query fields are aggregate functions and grouping fields , Querying other fields makes no sense where and having The difference between :
The timing of execution is different :where Is qualified before grouping , dissatisfaction where Conditions , They don't participate in the grouping , and having Is to filter the results after grouping .
Judging conditions are different :where Aggregate functions cannot be judged ,having Sure .
Execution order :where > Aggregate functions > having
f. Paging query
select Field list from Table name limit Starting index , Number of query entries ;
Be careful : Starting index from 0 Start , Calculation formula : Starting index = ( The current page number - 1) * Number of entries per page
5、 ... and 、SQL constraint
1、 Concept and classification
Concept : Constraints are rules that act on columns in a table , Used to limit the data added to the table ; The existence of constraints ensures the correctness of data in the database 、 Effectiveness and integrity .
The classification is shown in the figure below 
2、 grammar
a. Non empty constraint
Add a non NULL constraint when creating a table
create table Table name (
Name data type not null,
...
);
Add a non empty constraint after creating the table
alter table Table name modify Field name data type not null;
Delete non empty constraints
alter table Table name modify Field name data type ;
b. Unique constraint
Add unique constraints when creating tables
create table Table name (
Name data type unique,
...
);
Add unique constraints after creating the table
alter table Table name modify Field name data type unique;
Delete unique constraint
alter table Table name drop index Field name ;
c. Primary key constraint
Add a primary key constraint when creating a table
create table Table name (
Name data type primary key,
...
);
create table Table name (
-- auto_increment: When no value is specified, the self growth
Name data type primary key auto_increment,
...
);
Add the primary key constraint after creating the table
alter table Table name add primary key( Field name );
Delete primary key constraint
alter table Table name drop primary key;
d. Default constraint
Add default constraints when creating tables
create table Table name (
Name data type default The default value is ,
...
);
Add the default constraint after creating the table
alter table Table name alter Name set default The default value is ;
Delete default constraint
alter table Table name alter Name drop default;
e. Foreign key constraints
Add foreign key constraints when creating tables
create table Table name (
Name data type ,
...
-- Foreign key names are defined for yourself
constraint Name of the foreign key foreign key( Foreign key column name ) references Main table ( Name of main table )
);
Add foreign key constraints after creating the table
alter table Table name add constraint Name of the foreign key foreign key( Foreign key column name ) references Main table ( Name of main table );
Delete foreign key constraint
alter table Table name drop foreign key Name of the foreign key ;
6、 ... and 、 Multi-table query
Multi-table query : Query data from multiple tables . It is mainly divided into : Join queries and subqueries , Connection query is divided into inner connection query and outer connection query .
1、 Internal connection query
-- Implicit inner join
select Field list from surface A, surface B,... where Conditions ;
-- Display inner connection inner It can be omitted
select Field list from surface A inner join surface B on Conditions ;
Inner join query is equivalent to query table A And table B Intersection data of
2、 External connection query
-- The left outer join outer It can be omitted
select Field list from surface A left outer join surface B on Conditions ;
-- Right connection outer It can be omitted
select Field list from surface A right outer join surface B on Conditions ;
Left outer join query is equivalent to query table A All data and intersection data
Right outer join query is equivalent to query table B All data and intersection data
In use, it is generally enough to use the left outer connection , Want to query the table B All data and intersection data , Just put the table A And table B Just change the position .
3、 Subquery
Nested query in query , Call this nested query subquery .
According to the query results , Different functions can be divided into : Single row single row 、 Multi row single row 、 Multiple rows and columns .
a. Single row single row
Subquery as condition value , Use =!=>< Wait for conditional judgment
select Field list from Table name where Field name = ( Subquery );
b. Multi row single row
Subquery as condition value , Use in And other keywords for conditional judgment
select Field list from Table name where Field name in ( Subquery );
c. Multiple rows and columns
Subquery as virtual table
select Field list from ( Subquery ) where Conditions ;
All of these are SQL Common operation syntax , Just go ahead !
边栏推荐
- 机器学习基础-支持向量机 SVM-17
- Leetcode206 reverse linked list
- Connected Block & food chain - (summary of parallel search set)
- [half understood] zero value copy
- Leetcode 1518. wine change
- Four authentic postures after suffering and trauma, Zizek
- 05 pyechars basic chart (example code + effect diagram)
- Unity 安装 Device Simulator
- FlexPro软件:生产、研究和开发中的测量数据分析
- Did kafaka lose the message
猜你喜欢

云原生—运行时环境

Merge sort

The usage and Simulation Implementation of vector in STL

Use json.stringify() to format data

The largest rectangle in leetcode84 histogram

AVL tree (balanced search tree)

快速读入

Did kafaka lose the message

Design a thread pool

Baidu map API adds information window circularly. The window only opens at the last window position and the window information content is the same solution
随机推荐
Chapter IX rest Service Security
Monotonic stack
苏黎世联邦理工学院 | 具有可变形注意Transformer 的基于参考的图像超分辨率(ECCV2022))
Redefinition problem of defining int i variable in C for loop
Flexpro software: measurement data analysis in production, research and development
VS code更新后不在原来位置
01 introduction to pyechars features, version and installation
Science heavyweight: AI design protein has made another breakthrough, and it can design specific functional proteins
[base] what is the optimization of optimization performance?
How can non-standard automation equipment enterprises do well in product quality management with the help of ERP system?
利用依赖包直接实现分页、SQL语句
Machine learning practice - logistic regression-19
Review the IO stream again, and have an in-depth understanding of serialization and deserialization
Rolling update strategy of deployment.
Summary: golang's ide:vscode usage
leetcode 376. Wiggle Subsequence
Uniapp 应用开机自启插件 Ba-Autoboot
云原生—运行时环境
Machine learning practice - integrated learning-23
Block reversal (summer vacation daily question 7)