当前位置:网站首页>Basic syntax of MySQL DDL and DML and DQL
Basic syntax of MySQL DDL and DML and DQL
2022-07-24 03:20:00 【Broad and green riverside grass】
SQL classification :
DDL(Data Definition Languages) sentence : Data definition language , These statements define different data segments 、 database 、 surface 、 Column 、 Index and other database objects . Common sentence keywords mainly include create、drop、alter etc. .
DML(Data Manipulation Languages) sentence : Data manipulation statement , Used to add 、 Delete 、 Update and query database records , And check data integrity . Common sentence keywords mainly include insert、delete、update and select etc. .
DQL (Data Query Language) sentence : Data query language , Reserved words SELECT yes DQL( Also all SQL) The most used verb , other DQL The common reserved words are WHERE,ORDER BY,GROUP BY and HAVING.
DDL Is the abbreviation of data defined language , Simply speaking , It is to create objects inside the database 、 Delete 、 Language for operations such as modification . It and DML The biggest difference between languages is DML Just operate on the internal data of the table , It does not involve the definition of tables , Structural modification , It will not involve other objects .DDL The statement is more by the database administrator (DBA) Use , Developers generally rarely use .
1. Create database
create database Database name ;
use Database name ; Using a database
show databases; have access to SHOW Statement to check what database currently exists on the server
show create table Table name ; With SQL Statement to show the table structure
2. Delete database
drop database Database name ;
3: Create table
The basic syntax for creating a table in the database is as follows :
CREATE TABLE tablename(
column_name_1 column_type_1 constraints,
column_name_2 column_type_2 constraints,
....
column_name_n column_type_n constraints
)
4: Delete table
DROP TABLE Table name
5: Modify table
(1): Modify table type , The grammar is as follows :
ALTER TABLE Table name MODIFY Field name New properties
(2) Add table field , The grammar is as follows
ALTER TABLE Table name ADD Field name attribute ;
(3) Delete table fields , The grammar is as follows :
ALTER TABLE Table name DROP Field name ;
(4) Field renaming , The grammar is as follows :
ALTER TABLE Table name CHANGE Old field name New field name New properties ;
(6) Change table name , The grammar is as follows :
ALTER TABLE Old table name as New table name ;
DML sentence
DML(Data Manipulation Language) sentence : Data manipulation statement .
purpose : Used to add 、 modify 、 Delete and query database records , And check data integrity .
DML The object of operation is the data of the library table ( Record ).
It mainly includes inserting (insert)、 to update (update)、 Delete (delete) And query (select).
DML Statement is the most frequently used operation by developers .
1. insert record
Insert a record
insert into Table name ( Field name , Field name ,..., Field name ) values(' Corresponding value ',' Corresponding value ',...,' Corresponding value ');
2. Update record
Update single table data
update Table name ser Field one = New value , Field 2 = New value ;
3. Simple query records
Simple condition query . Symbol “*” Query all fields on behalf of , If you just want to see some of these columns , Then write the column name of the table .
select * from Table name ;
select Field name from Table name ;
4. Delete record
Delete a single table record
delect from Table name ;
explain : No addition where Condition rule Delete full table data
DQL be called “ Data retrieval statements ”, To get data from a table , Determine how the data is presented in the application . among , According to the structure and relationship of the table, it can be divided into single table query and multiple Table associated query .
Query syntax rules :
SELECT [DISTINCT] {*| surface 1.*|[ surface 1. Field 1 [as Field alias 1] [, surface 1. Field 2[as Field alias 2]][, …]]}
FROM surface 1 [ as Table alias ] [ left|right|inner join surface 2 on Between tables
The relationship between ] [ WHERE ] [ GROUP BY ]
[ HAVING] [ ORDER BY] [ LIMIT {[ Position offset ,] Row number }] ;
-- WHERE Conditional clause
select * from Table name where Conditions ;
LIKE keyword
select * from Table name where Field LIKE Conditions ;
IN keyword
Common aggregate functions
AVG(col)
Returns the average value of the specified column
COUNT(col)
Returns the value of the specified column NULL The number of values
MIN(col)
Returns the minimum value of the specified column
MAX(col)
Returns the maximum value of the specified column
SUM(col)
Returns the sum of all values in the specified column
GROUP BY and HAVING grouping
LIMIT keyword
select * from Table name LIMIT [n,m]
MySql8 New keywords OFFSET
select * from Table name limit on offset n;
senior DQL:
1. Multiple tables associated query :
case when then end sentence
Statement is used to query conditions that meet a variety of conditions
similar java Medium if...else, There are also queries for row to column conversion , This is on select After Clause , It acts as a field . There are two specific uses , One is a simple functional form , The other is the form of expression .
-- Simple functional form
select (case score when 'a' then ' good ' else ' good ' end) as score from student;
-- Expression form
select (case when score between 85 and 90 then ' good ' else ' good ' end)
-- case when then end
-- Simple case
select tid,tname,
case tsex
when 1 then ' male '
when 0 then ' Woman '
else ' Unknown '
end tsex,
tbirthday, taddress
from teacher
-- Search for case
select tid,tname,
case
when tsex > 1 then ' Unknown '
when tsex = 1 then ' male '
when tsex < 1 then ' Woman '
else ' There can be no situation '
end
,
tbirthday,taddress from teacherstay case There can also be nested sub queries and other complex uses .
(case when then end) Namely select A field to be queried later , When using, it can be enclosed in parentheses, which is more readable , You can use aggregate functions for it , Alias , duplicate removal , Wait for the operation .
边栏推荐
- 微信公众号在线客服接入发方法和功能详解
- JVM initial
- Example of producer consumer code implemented by the destructor framework without lock
- I developed an app similar to wechat runnable applet with fluent
- [super complete sorting] Cisco and Huawei order comparison memo, take it away without thanks! Anytime, anywhere
- Keras deep learning practice (15) -- realize Yolo target detection from scratch
- C. Minimum Ties-Educational Codeforces Round 104 (Rated for Div. 2)
- Cache component status when Vue components are switched, that is, dynamic components keep alive dynamic components and asynchronous components
- Daily gossip (I)
- CMT registration - Google Scholar ID, semantic scholar ID, and DBLP ID
猜你喜欢

openEuler 资源利用率提升之道 01:概论

In the future, when the interviewer asks why you don't recommend using select *, please answer him loudly!

21st day of written test mandatory training

MySql的DDL和DML和DQL的基本语法

Xiaodi and Xiaohui

Ugui source code analysis - stencilmaterial

JS small game running bear and cat source code

Talk about the application of FIFO

The first edition of Niuke brush question series (automorphic number, return the number of prime numbers less than N, and the first character only appears once)

Microsoft win11/10 package manager Winget will support the installation of applications from zip files
随机推荐
水题: 接雨水
Insist on accompanying study
IDEA Clone的项目报Cannot resolve symbol ‘Override‘
FTP服务与配置
[MySQL learning] install and use multiple versions of MySQL, MySQL 8 and MySQL 5.7 at the same time, compressed version
An introductory example of structure and combinatorial ideas in go language
Interviewer: if the order is not paid within 30 minutes after it is generated, it will be automatically cancelled. How to realize it?
拉格朗日插值法
Rules for generating 13 digit barcode EAN-13 Code: the thirteenth digit is the verification code obtained by calculating the first twelve digits.
Example of producer consumer code implemented by the destructor framework without lock
A series of problems of dp+ backtracking segmentation palindrome string
Bingbing learning notes: basic operation of vim tool
The implementation in unity determines whether missing or null
Hcip day 9 notes (OSPF routing feedback, routing policy, and Configuration Guide)
Acwing 4498. pointer (DFS)
移动通信的新定义:R&SCMX500 将提高5G设备的IP数据吞吐量
CMT registration - Google Scholar ID, semantic scholar ID, and DBLP ID
Hcip --- BGP comprehensive experiment
Talk about the application of FIFO
CMT 注册——Google Scholar Id,Semantic Scholar Id,和 DBLP Id