当前位置:网站首页>Basic syntax of MySQL DDL and DML and DQL
Basic syntax of MySQL DDL and DML and DQL
2022-07-23 22:13:00 【cherich.】
Preface
SQL sentence , Structured query language (Structured Query Language), Is a special purpose programming language , Is a database query and programming language , For data access and query 、 Update and manage relational database system , It is also the extension of database script file .
SQL standard-specified SQL Sentences are divided into :DDL(Data Define Language Data definition language )、 DML(Data Manipulation Language Data operation language )、DQL(Data Query Language Data query language )、DCL(Data Control Language Data control language ). This article will introduce them in detail .
First, learn about SQL Some grammatical considerations :
1. SQL Statements can be written in one or more lines , It ends with a semicolon .
2. You can use spaces and indents to enhance the readability of statements .
3. MySQL Database SQL Statement is case insensitive , It is recommended to use uppercase .
4. 3 Species notes
① Single-line comments : -- The comment or # The comment (mysql specific )
② Multiline comment : /* notes */
One 、DDL( Data definition language )
DDL Language : Comprehensive data definition language (Data Define Language), It is used to define and manage data objects , Such as a database , Data sheets, etc .DDL Command has CREATE( establish )、DROP( Delete )、ALTER( modify ).
Let's give you an example with code :
-- SQL Syntax is case-insensitive
-- Use a semicolon at the end of each sentence ;
# Operation of the library
-- Show all libraries
show databases;
-- Create a library
-- create database Library name ;
create database ku;
-- Delete a library
-- drop database Library name ;
drop database ku;
-- Use the library
-- use Library name ;
use ku;
# The operation of the table
-- Look at all the tables in the library
show tables;
-- Build table
create table Table name (
Field name type attribute ,
Field name type attribute ,
....
Field name type attribute
);
create table tab_teacher(
tea_name varchar(10),
tea_sex char(1),
tea_birthday datetime,
tea_money decimal(20,1)
);
show tables;
-- View table structure
desc tab_teacher;
show create table tab_teacher;
-- ` The quotation marks Disable keywords
CREATE TABLE `tab_teacher` (
`tea_name` varchar(10) DEFAULT NULL,
`tea_sex` char(1) DEFAULT NULL,
`tea_birthday` datetime DEFAULT NULL,
`tea_money` decimal(20,1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_0900_ai_ciTwo 、DML( Data operation language )
DML Language : Data operation language (Data Manipulation Language), It is used to operate the data contained in the database object .DML Command has INSERT( increase )、DELETE( Delete )、UPDATE( modify ).
Let's give you an example with code :
# DML sentence
-- newly added
-- grammar
-- insert into Table name ( Field name , Field name ... Field name ) values( value , value ... value );
-- The date is expressed as a string
insert into student(sid,sname,birthday,ssex,classid) values(9,' Zhang San ','1999-1-1',' male ',3);
insert into student(sid,ssex,classid) values(11,' male ',2);
-- Let the primary key increase
insert into student(sname) values(" Wang sang ");
insert into student values(default,' Lao Wang ','1970-6-1',' male ',2);
insert into student values(null,' Lao Wang ','1970-6-1',' male ',2);
-- Insert multiple pieces of data at once
insert into student(sname,ssex) values(' Wang Shuai ',' male '),(' Wang Liangliang ',' male '),(' Sister Wang ',' Woman ');
-- Unusual new methods
-- Tables must exist
create table stu1(
xingming varchar(10),
ssex varchar(2)
)
-- insert into select
insert into stu1 select sname,ssex from student;
-- Insert data when creating a new table
-- New table cannot exist
create table newstu select sname,birthday,ssex from student;
-- modify
-- grammar
-- update Table name set Field name = value , Field name = value ... where Clause
update stu1 set xingming = ' Zhao Leilei ';
update newstu set ssex= ' Woman ' where sname=' Lao Wang ';
-- Range
update student set ssex = ' Woman ',classid = 10 where sid >= 10 and sid <= 15;
-- between Little data and big data
update student set ssex=' ha-ha ',classid = 20 where sid between 10 and 15;
-- Delete
-- delete from Table name where Clause
delete from stu1;
delete from student where sname = ' Lao Wang ';
-- Clear the table
truncate Table name
truncate student;3、 ... and 、DQL( Data query language )
DQL Language : Data query language (Data Query Language), It is used to query database data .DQL Command has SELECT( Inquire about ).
Let's give you an example with code :
# DQL
-- Inquire about
-- Query the data of all rows and columns of the table ( What you get is a virtual table )
-- select * from Table name ;
select * from student;
-- Query the specified field
-- select Field name 1, Field name 2... from Table name ;
select sid,sname,birthday,ssex,classid from student;
-- The field is aliased
-- select Old field name as ' new field name ';
select sname as ' full name ', birthday ' Birthday ',ssex Gender from student;
-- Remove duplication distinct
-- select distinct Field name ... from Table name ;
select distinct ssex,classid,sid from student;
-- Conditional inquiry WHERE Clause
select * from student where ssex = ' male ' and classid = 1;
-- Birthday Greater than 1990-1-1 Of the students
select * from student where birthday < '1990-1-1';
-- Fuzzy query like
insert into student(sname)
values(' Zhang Sanfeng '),(' Zhang San '),(' Zhang Sansan ');
-- Zhang Zi related data
-- Fuzzy symbols % Any number of any characters
select * from student where sname like '% Zhang %';
-- Zhang
select * from student where sname like ' Zhang %';
-- Fuzzy symbols _ An arbitrary character
select * from student where sname like ' Zhang __';
-- The student number is 2,5,6,8,9,20,300,4000
-- in Find in a specific range
select * from student where sid in (2,5,6,8,9,20,300,4000);
-- Students without birthdays is It's right null The judgment of the
select * from student where birthday is null;
select * from student where birthday is not null;
# grouping
-- group by Field
select count(1) from student where ssex = ' male ';
select count(1) from student where ssex = ' Woman ';
select ssex,count(sid) from student group by ssex;
-- How many students are there in each class
select classid,count(sid) from student group by classid;
-- sc The average score of each student
select sid,avg(score) average , sum(score) Total score ,max(score) The highest , min(score) Lowest score , count(*) frequency from sc group by sid;
Four 、 Aggregate functions
grammar : Before we do the query is horizontal query , They are judged line by line according to the conditions , The aggregate function query is vertical query , It calculates the values of a column , And then return a result value . Aggregate functions ignore null values NULL.
-- Number of Statistics count( Field )/ Fields can be written *、 Constant 、 Any field name /count The statistical data is null The number of .
-- Statistical average avg( Field )
-- Statistical maximum max( Field )
-- Statistical minimum min( Field )
-- Statistical sum sum( Field )
eg: select count(*) Total number , sum(score) Total score , avg(score) average , max(score) The highest , min(score) Lowest score from sc;
# Aggregate functions
count( Field ) -- Number of Statistics
-- Numbers
avg( Field ) -- Average
sum( Field ) -- The sum of the
max( Field ) -- Maximum
min( Field ) -- minimum value
-- count Number of Statistics
select count(*) from student where ssex = ' male ';
select count(sname) from student where ssex = ' male ';
select count(1) from student where ssex = ' male ';
select count('a') from student where ssex = ' male ';
-- count() No statistics null
select count(birthday) from student;
-- avg Average
-- The average score of all students
select avg(score) from sc;
-- sum Total score
select sum(score) from sc;
select count(*) frequency , sum(score) Total score , avg(score) average , max(score) The highest ,min(score) Lowest score from sc;边栏推荐
- Programmation JDBC pour MySQL
- 怎么开户买收益百分之六的理财产品呢?
- Application of performance test knowledge to actual combat
- JDBC programming of MySQL
- Comparison of open source distributed link tracking
- How to completely force the killing of background unrelated processes?
- Is it safe to open a securities account online?
- 小说里的编程 【连载之十七】元宇宙里月亮弯弯
- MySQL如何对SQL做prepare预处理(解决IN查询SQL预处理仅能查询出一条记录的问题)
- Can Verilog of synthetizable be integrated
猜你喜欢
随机推荐
El upload realizes the preview of uploaded files
数据库系统概论第五版课后习题——第一章 绪论
I, AI doctoral student, online crowdfunding research topic
Jedis 6 - Introduction and difference between redisson and jedis
【AcWing】周赛
LeetCode高频题62. 不同路径:机器人从左上角到右下角的路径有多少条?纯概率排列组合问题,而不是动态规划题
JS——事件代理和应用场景
DBSCAN point cloud clustering
Sudoku written for once and for all
U++学习笔记 TSubclassOf()
Altium Designer - schematic diagram of Arduino uno & PCB diagram (self-made Arduino board)
Preliminary discussion on POC compilation
接口测试
STM32单片机使用ADC功能驱动手指检测心跳模块
达梦数据库tools包中的工具(操作达梦数据库)
U++ 学习笔记 控制物体Scale
10道面试基础笔试题,你能对几题?
Introduction to database system fifth edition after class exercises - Chapter 1 Introduction
Use of [golang learning notes] package
Multithreading problem: why should we not use multithreading to read and write the same socket connection?









