当前位置:网站首页>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;边栏推荐
- 小说里的编程 【连载之十八】元宇宙里月亮弯弯
- STM32单片机使用ADC功能驱动手指检测心跳模块
- How to implement desktop lyrics in pyqt
- ADB 命令结合 monkey 的简单使用,超详细
- 【HiFlow】腾讯云新一代自动化助手,我用它完成了企业疫情提示(无代码)
- 软件测试工作内容太简单怎么办?
- Basic character axis binding and mapping binding of u++ learning notes
- lambda學習(sort後面的Comparator的使用,collection後使用Collectors.groupingBy分組)
- Golang invalid argument to intn报错的解决
- El upload realizes the preview of uploaded files
猜你喜欢

MySQL index transaction

STM32 MCU uses ADC function to drive finger heartbeat detection module

Taobao assistant is disabled. What is the reason for using the big Taoying import data package to upload the baby prompt "the main image is required and cannot be empty"? How to solve it?

10道面试基础笔试题,你能对几题?

ONEFLOW V0.8.0 officially released

性能测试知识应用于实战

Mqtt connection, subscription and publishing can be realized without mqtt C library

Memory search - DP

Tools in the tools package of Damon database (operate Damon database)

Leetcode high frequency question 53. maximum subarray sum, continuous subarray with maximum sum, return its maximum sum
随机推荐
Leetcode high frequency question 62. different paths: how many paths does the robot have from the upper left corner to the lower right corner? Pure probability permutation and combination problem, not
Financial products with an annual yield of 6%
机器学习习题——对率回归
lambda學習(sort後面的Comparator的使用,collection後使用Collectors.groupingBy分組)
STM32 MCU uses ADC function to drive finger heartbeat detection module
Can Verilog of synthetizable be integrated
Neo4j应用
Still worrying about xshell cracking, try tabby
DeFi项目的盈利逻辑 2021-04-26
02.网页结构相关知识补充
为了一劳永逸而写的数独
SQL injection attack
I, AI doctoral student, online crowdfunding research topic
pulsar开源消息队列_了解Pulsar---Pulsar工作笔记001
【AcWing】周赛
实时监控Mysql数据库变化_进行数据同步_了解Canal_---Canal工作笔记001
LeetCode高频题53. 最大子数组和,具有最大和的连续子数组,返回其最大和
[golang learning notes] is parameter transfer in go language value transfer or reference transfer
Comment forcer complètement le meurtre de processus indépendants de l'arrière - plan?
小说里的编程 【连载之二十】元宇宙里月亮弯弯