当前位置:网站首页>Basic syntax of MySQL DDL and DML and DQL
Basic syntax of MySQL DDL and DML and DQL
2022-07-26 13:56:00 【You don't understand my romantic feelings】
stay Mysql In the process of learning , More commonly used is :DML,DDL and DQL, The following is a detailed introduction to these common sentences , The commands commonly used in the three languages are shown in the following figure :

One 、DDL( Data customization statement )
1.creat: Usually use this command to create a database or create a data table , The grammar is as follows :
①create database Database name ; -- Create a database
②create table Table name (
Field name data type attribute constraint condition ,
Field name data type attribute constraint condition ,
.....
Field name data type attribute
);
When we build the data table , There are several points to pay attention to in the sentence :
a. Attributes and constraints are not necessary , It can be omitted
b. The last field is followed by no , Of
c. In the same table , Field names cannot be the same
The usage of these two statements is shown in the following figure ( About attributes and constraints in statements , I will post a detailed description later :

2.drop: This command is usually used to delete the data table , It will also be used with other commands to delete fields in the table , Common commands are as follows :
①drop table Table name -- Delete data table
3.alter: This command is usually used to modify the structure of the table
①alter table The old name of the table rename as The new name of the table ; -- Modify the name of the table
②alter table Table name modify Field name Column type 【 attribute 】; -- Modify field properties
③alter table Table name change Old field name new field name Column type 【 attribute 】 -- Modify field names and properties
It needs to be noted that the above modify and change Both statements can modify the properties of fields , The difference is change At the same time, you can modify the name of the field , however modify no way
④alter table Table name drop Field name ; -- Delete field
⑤alter table Table name add Field name type attribute ; -- Add fields
The simple usage of the above statements is shown in the following figure :

Two 、DML( Data operation statement )
1.insert: This statement is usually used to add data , Now let's add some data to the student table
①insert into Table name ( Field 1, Field 2.....)values ( Field 1 The value of the corresponding type , Field 2 The value of the corresponding type .....);
-- The new data ( One thing to note here is that we need to use string representation when adding date type data )

2.update: Modifying data
①update Table name set Field name = value ( Note the modified value ) where Conditions ; -- Modifying data

3.detele: Delete data
① delete from Table name where Conditions ;
②truncate 【table】 Table name ;-- This statement is very simple, which is to empty the data of a table , among table It can be omitted
3、 ... and 、DQL( Data query statement ) This statement is more important in the database , The specific sentences are as follows :
Single table query :
1. Simple query syntax
①select Field /* from Table name ; -- Query the corresponding data from a table (* Represents all data )
②select Field new field name from Table name : -- When querying field data, you can alias the field
③select distinct Field name from Table name ; -- You can reprocess the queried data

2.DQL Conditional query syntax ( There are several keywords about conditional query , The following examples are the simple usage of these keywords )
①where keyword ( Conditions of the query )
select Field name from Table name where Conditions ;
② like keyword ( Fuzzy query ): Often and %( Represents several characters )_( Represents a character ) Use it together
select Field name from Table name where Field like Conditions ;
③in keyword ( Range queries )
select Field name from Table name where Field in ( value 1, value 2....);

③null Value query : What needs to be noted here is , Yes null The keyword of value query should be is

④group by Group queries and having( Group query ): Often used with aggregate functions
select Field from Table name group by Field name having Conditions ;
Common aggregate functions : avg( Field name )—— averaging ,sum( Field name )—— Sum up ,max( Field name )—— For maximum , min( Field name )—— For the minimum , count( Field name )—— Sum up , These five aggregate functions are frequently used , The usage is not very complicated , Here is an example combined with grouping query :

⑤order by( Sort ): The main thing here is to match two keywords when sorting asc( Ascending sort , It can be omitted ) and desc( null , Do not omit , If omitted, the system will default to ascending )
select Field from Table name order by Field name desc/asc;

⑥limit( Restrict query keywords ): Usually used to limit the number of query pages
select Field from Table name limit n,m;-- Generally speaking, query from n( It doesn't contain n) Data starts , The length is m A few pieces of data , This keyword is often used to realize paging query of data , here n and m The value of can be expressed by formula
n=( Page number -1)* The amount of data displayed per page ,m= The amount of data displayed per page

3.DQL Advanced query statements
① Multiple tables associated query
Non equivalent query :select * from surface 1, surface 2;-- It should be noted that this kind of query method without judgment , The queried data will be duplicated , You may not get the data you want , So in most cases, we will use the method of equivalent joint query to query data .
Equivalent query :
a. Inline query :
Writing a :SELECT Field name FROM Table name ... where conditional AND conditional

Write two :select * from Table name inner join Table name on Equivalent condition judgment where Conditions ;

The above two methods can achieve the same query effect , We can choose which query method to use according to our own needs
b. Outreach query :
select * from Table name left join Table name on Equivalent judgment condition where Judge the condition ;-- Left joint query
select * from Table name right join Table name on Equivalent judgment condition where Judge the condition ;-- Right associated query

External query and internal query , Although we can get the data we want to query , There are still some differences between them :

As shown in the figure above :
a. Inline query :select * from surface A inner join surface B on Equivalent judgment condition ;
This inline query statement is actually a query surface A and surface B Shared data Set C
b. Left outer query :select * from surface A left jion surface B on Equivalent judgment condition ;
This leftist query statement is actually to jion The table on the left A As the main table , Take out the watch first A All data , then Then judge the condition according to the equivalence , Look up the table A And table B Common data sets C, So the result of the query is in the figure : surface A Data sets +C Data sets
c. Right query :select * from surface A right join surface B on Equivalent judgment condition ;
The principle of this right join query statement is similar to that of the left join , Just put join Table on the right B As the main table , Query the table first B All the data in , Then query the table according to the judgment conditions B And table A Common data sets C, So the result is in the figure : surface B Data sets +C Data sets
One more point , The basis for judging left and right couplets is jion The left and right of keywords are the main tables .
② Subquery
a.where Subquery ( Take the queried data set as where The latter condition judgment )
As shown in the figure below , Find out the student with the largest student number as where The conditions of judgment ,where How to write a subquery

b.from Subquery ( Treat the inner query results as a temporary table , For external queries , It is better to alias the temporary table when using )

c. exist Subquery (exists Follow the internal query , If this inner query is true , The outer query will take effect , The final result is the data of outer layer query )

d.any/some( These two sub queries must be used together with the condition evaluator )
select * from Table name where a>any(2,3,4); This statement is equivalent to the following
select * from Table name where a>2 or a>3 or a>4;

some and any The usage of is exactly the same , It's just a different name
e.all Subquery ( This seed query and any similar , Only the latter needs to meet all the conditions )
select * from Table name where a>all(2,3,4); This statement is equivalent to the following
select * from Table name where a>2 and a>3 and a>4; 
边栏推荐
- 力扣------字符串中的单词数
- Thoughts on the compilation of Dr. Shuo's opening report
- C language_ Combination of structure and array
- Plato Farm有望通过Elephant Swap,进一步向外拓展生态
- 大脑带来的启发:深度神经网络优化中突触整合原理介绍
- 数据泄漏、删除事件频发,企业应如何构建安全防线?
- Use the requests library to crawl web pages
- Inspiration from brain: introduction to synaptic integration principle in deep neural network optimization
- 【论文阅读】GRAW+:A Two-View Graph Propagation Method With Word Coupling for Readability Assessment
- @千行百业,一起乘云而上!
猜你喜欢

C language_ Combination of structure and array

Large and small end mode

Integer internal cache

Zhou Wei: look for non consensual investment opportunities to accompany the founding team that delays satisfaction

基于标签嵌入注意力机制的多任务文本分类模型

C语言_结构体和数组的结合

How to write the introduction of GIS method journals and papers?

【Oauth2】七、微信OAuth2授权登录

历时15年、拥有5亿用户的飞信,彻底死了

JS download files, filesaver.js export txt and Excel files
随机推荐
[dark horse morning post] many apps under bytek have been taken off the shelves; The leakage of deoxidizer in three squirrels caused pregnant women to eat by mistake; CBA claimed 406million yuan from
C语言_结构体指针变量引入
Tianyi cloud web application firewall (edge cloud version) supports the detection and interception of Apache spark shell command injection vulnerabilities
飞盘,2022年“黑红”顶流
TDSQL-C Serverless:助力初创企业实现降本增效
POM文件详解
The serialization class in unity is in JSON format
聚力打造四个“高地”,携手合作伙伴共铸国云!
Integer internal cache
2022-07-26 Daily: the first anniversary of the establishment of alphafold DB database, research on the lighting point of official promotion
WPS凭什么拒绝广告?
Frisbee, 2022 "black red" top stream
Huawei computer test ~ offset realizes string encryption
MySQL sets auto increment for existing primary keys
大脑带来的启发:深度神经网络优化中突触整合原理介绍
C language_ Structure pointer variable introduction
Unicorn, valued at $1.5 billion, was suddenly laid off, and another track was cold?
Official announcement! Edweisen group and Baidu xirang reached a deep co creation cooperation
gdb常用命令
Meeting seating and submission for approval of OA project