当前位置:网站首页>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;边栏推荐
- [golang learning notes] simple use of flag package, command line parsing
- Pulsar open source message queue_ Understand pulsar --- pulsar work notes 001
- Wangxuegang video coding -- mediacodec coding and decoding
- Sudoku written for once and for all
- experimental design
- Jedis 6 - Introduction and difference between redisson and jedis
- Microsoft SQL Server数据库语言及功能使用(十三)
- 人生总需要一点激情
- JS object array de duplication
- How to implement desktop lyrics in pyqt
猜你喜欢

PCL error: error c2589 "(": "::" illegal mark on the right)

El select drop-down box multi selection remote search anti display
![[mathematical modeling summer training] location of distribution center](/img/d5/c9b4de6750a7ed080c194250629467.png)
[mathematical modeling summer training] location of distribution center

记第一次挖洞交洞历程
![[hiflow] Tencent cloud's new generation of automation assistant, which I used to complete the enterprise epidemic prompt (no code)](/img/8a/52ef97e43c4b06e08ab826f4e46501.png)
[hiflow] Tencent cloud's new generation of automation assistant, which I used to complete the enterprise epidemic prompt (no code)

JMeter performance comprehensive practice - sign in and batch sign in

存储结构和管理盘。有点像装win98要先分区格式化硬盘

Multithreading problem: why should we not use multithreading to read and write the same socket connection?

Can Verilog of synthetizable be integrated

Introduction to database system fifth edition after class exercises - Chapter 1 Introduction
随机推荐
记忆化搜索 - DP
Redis common commands correspond to redisson object operations
U++ 学习笔记 控制物体Scale
初探POC编写
Application of performance test knowledge to actual combat
U++ 事件
Financial products with an annual yield of 6%
多线程问题:为什么不应该使用多线程读写同一个socket连接?
数据库系统概论第五版课后习题——第一章 绪论
Introduction to database system fifth edition after class exercises - Chapter 1 Introduction
Programmation JDBC pour MySQL
Cookie 和 Session
Sixty final review questions of software architecture
小说里的编程 【连载之二十】元宇宙里月亮弯弯
PCL error: error c2589 "(": "::" illegal mark on the right)
实验设计
YOLO7 口罩识别实战
did you register the component correctly
experimental design
Machine learning exercises -- right rate regression