当前位置:网站首页>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 !
边栏推荐
- VS1003调试例程
- Hc-05 Bluetooth module debugging slave mode and master mode experience
- 云原生—运行时环境
- 05 pyechars 基本图表(示例代码+效果图)
- Initialization examples of several modes of mma8452q
- Science 重磅:AI设计蛋白质再获突破,可设计特定功能性蛋白质
- Machine learning practice - neural network-21
- 连通块&&食物链——(并查集小结)
- Summary: golang's ide:vscode usage
- XIII Actual combat - the role of common dependence
猜你喜欢

How can non-standard automation equipment enterprises do well in product quality management with the help of ERP system?

快速读入

C for循环内定义int i变量出现的重定义问题

Summary: golang's ide:vscode usage

Machine learning practice - integrated learning-23

Problem solving during copilot trial

Hc-05 Bluetooth module debugging slave mode and master mode experience

非标自动化设备企业如何借助ERP系统,做好产品质量管理?

Scala transformation, filtering, grouping, sorting

大模型哪家强?OpenBMB发布BMList给你答案!
随机推荐
LeetCode84 柱状图中最大的矩形
STM32F103 several special pins are used as ordinary io. Precautions and data loss of backup register 1,2
leetcode 376. Wiggle Subsequence
Leetcode394 string decoding
Using JSON in C language projects
XIII Actual combat - the role of common dependence
AI制药的数据之困,分子建模能解吗?
The input string contains an array of numbers and non characters, such as a123x456. Take the consecutive numbers as an integer, store them in an array in turn, such as 123 in a[0], 456 in a[1], and ou
03 pyechars rectangular coordinate system chart (example code + effect drawing)
Sliding Window
界面控件Telerik UI for WPF - 如何使用RadSpreadsheet记录或评论
Leetcode 1518. wine change
Under what circumstances can the company dismiss employees
Machine learning practice - neural network-21
苏黎世联邦理工学院 | 具有可变形注意Transformer 的基于参考的图像超分辨率(ECCV2022))
C structure use
Force buckle 315 calculates the number of elements smaller than the current element on the right
Aopmai biological has passed the registration: the half year revenue is 147million, and Guoshou Chengda and Dachen are shareholders
FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be depreca
[cute new problem solving] climb stairs