当前位置:网站首页>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; 
边栏推荐
- Tianjin emergency response Bureau and central enterprises in Tianjin signed an agreement to deepen the construction of emergency linkage mechanism
- php使用sqlserver
- 上一次听到易趣,还是上一次
- Latest battle report: Ten certifications and five best practices
- TDSQL-C Serverless:助力初创企业实现降本增效
- Ten thousand words long article, talking about the blueprint of enterprise digital modeling
- IDEA(warning)No artifacts configured
- 重押海外:阿里、京东、顺丰再拼“内力”
- Flink SQL(三) 连接到外部系统System和JDBC
- 一文学透MySQL表的创建和约束
猜你喜欢

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

循环队列(c语言实现)

Canvas upload image Base64 with cropping function jcrop.js

Pytoch learning notes (II) the use of neural networks

Seven steps to copywriting script ---- document team collaborative management

Win11+vs2019 configuration yolox

【Oauth2】五、OAuth2LoginAuthenticationFilter

IDEA(warning)No artifacts configured

Ros2 learning (1) introduction to ros2

上一次听到易趣,还是上一次
随机推荐
Multithreaded completable future usage
Frisbee, 2022 "black red" top stream
Team research and development from ants' foraging process (Reprint)
估值15亿美元的独角兽被爆裁员,又一赛道遇冷?
Redis learning notes
Why does WPS refuse advertising?
Concept and handling of exceptions
周伟:寻找非共识性投资机会,陪伴延迟满足的创始团队
向路由组件传递参数
【Oauth2】七、微信OAuth2授权登录
Latest battle report: Ten certifications and five best practices
白帽子揭秘:互联网千亿黑产吓退马斯克
[paper reading] raw+:a two view graph propagation method with word coupling for readability assessment
php使用sqlserver
2022-07-26 Daily: the first anniversary of the establishment of alphafold DB database, research on the lighting point of official promotion
Flink SQL (III) connects to the external system system and JDBC
基于多任务深度学习的实体和事件联合抽取模型
Tdsql-c serverless: help start-ups achieve cost reduction and efficiency increase
MySQL's practice of SQL analysis and optimization from the index principle
Polymorphic case - making drinks