MySQL的 DDL和DML和DQL的基本语法详解

前言                 

SQL语句,即结构化查询语言(Structured Query Language),是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统,同时也是数据库脚本文件的扩展名。       

SQL标准规定的SQL语句分为:DDL(Data Define Language 数据定义语言)、  DML(Data Manipulation Language 数据操作语言)、DQL(Data Query Language 数据查询语言)、DCL(Data Control Language 数据控制语言)。本文将详细介绍它们。

首先了解一下关于SQL语法的一些注意事项:

1. SQL 语句可以单行或多行书写,以分号结尾。

2. 可使用空格和缩进来增强语句的可读性。

3. MySQL 数据库的 SQL 语句不区分大小写,关键字建议使用大写。

4. 3 种注释

① 单行注释:  — 注释内容 或 # 注释内容(mysql 特有)

② 多行注释:  /* 注释 */

一、DDL(数据定义语言)

DDL语言:全面数据定义语言(Data Define Language),是用来定义和管理数据对象,如数据库,数据表等。DDL命令有CREATE(创建)、DROP(删除)、ALTER(修改)。

下面用代码给大家举例子:

— SQL语法不区分大小写
— 每一句结束的时候都要用一个分号;
# 库的操作
— 显示所有的库
show databases;
— 创建一个库
— create database 库名;
create database ku;
— 删除一个库
— drop database 库名;
drop database ku;
— 使用库
— use 库名;
use ku;
# 表的操作
— 查看库中所有的表
show tables;
— 建表
create table 表名(
字段名 类型 属性,
字段名 类型 属性,
….
字段名 类型 属性
);
create table tab_teacher(
tea_name varchar(10),
tea_sex char(1),
tea_birthday datetime,
tea_money decimal(20,1)
);
show tables;
— 查看表结构
desc tab_teacher;
show create table tab_teacher;
— ` 反引号 让关键词失效
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_ci

二、DML(数据操作语言)

DML语言:数据操作语言(Data Manipulation Language),是用于操作数据库对象中所包含的数据。DML命令有INSERT(增加)、DELETE(删除)、UPDATE(修改)。

下面用代码给大家举例子:

# DML 语句
— 新增
— 语法
— insert into 表名(字段名,字段名…字段名) values(值,值…值);
— 日期用字符串的形式表示
insert into student(sid,sname,birthday,ssex,classid) values(9,’张三’,’1999-1-1′,’男’,3);
insert into student(sid,ssex,classid) values(11,’男’,2);
— 让主键自增
insert into student(sname) values(“王桑”);
insert into student values(default,’老王’,’1970-6-1′,’男’,2);
insert into student values(null,’老王’,’1970-6-1′,’男’,2);
— 一次性插入多条数据
insert into student(sname,ssex) values(‘王帅帅’,’男’),(‘王靓靓’,’男’),(‘王妹妹’,’女’);
— 不常用的新增方式
— 表都要存在
create table stu1(
xingming varchar(10),
ssex varchar(2)
)
— insert into select
insert into stu1 select sname,ssex from student;
— 新建表的时候插入数据
— 新表不能存在
create table newstu select sname,birthday,ssex from student;
— 修改
— 语法
— update 表名 set 字段名=值,字段名=值… where 子句
update stu1 set xingming = ‘赵雷雷’;
update newstu set ssex= ‘女’ where sname=’老王’;
— 范围
update student set ssex = ‘女’,classid = 10 where sid >= 10 and sid <= 15;
— between 小数据 and 大数据
update student set ssex=’呵呵’,classid = 20 where sid between 10 and 15;
— 删除
— delete from 表名 where 子句
delete from stu1;
delete from student where sname = ‘老王’;
— 清空表
truncate 表名
truncate student;

三、DQL(数据查询语言)

DQL语言:数据查询语言(Data Query Language),是用于查询数据库数据。DQL命令有SELECT(查询)。

下面用代码给大家举例子:

# DQL
— 查询
— 查询表所有行和列的数据(得到的是一张虚拟表)
— select * from 表名;
select * from student;
— 查询指定字段
— select 字段名1,字段名2… from 表名;
select sid,sname,birthday,ssex,classid from student;
— 字段起别名
— select 旧字段名 as ‘新字段名’;
select sname as ‘姓名’, birthday ‘生日’,ssex 性别 from student;
— 去除重复 distinct
— select distinct 字段名… from 表名;
select distinct ssex,classid,sid from student;
— 带条件的查询 WHERE 子句
select * from student where ssex = ‘男’ and classid = 1;
— 生日 大于 1990-1-1 的学生
select * from student where birthday < ‘1990-1-1’;
— 模糊查询 like
insert into student(sname)
values(‘张三丰’),(‘张三’),(‘张三三’);
— 张字有关的数据
— 模糊符号 % 任意多的任意字符
select * from student where sname like ‘%张%’;
— 姓张的人
select * from student where sname like ‘张%’;
— 模糊符号_ 一个任意字符
select * from student where sname like ‘张__’;
— 学生编号是 2,5,6,8,9,20,300,4000
— in 在特定的范围内查找
select * from student where sid in (2,5,6,8,9,20,300,4000);
— 没有生日的学生 is 是对null的判断
select * from student where birthday is null;
select * from student where birthday is not null;
# 分组
— group by 字段
select count(1) from student where ssex = ‘男’;
select count(1) from student where ssex = ‘女’;
select ssex,count(sid) from student group by ssex;
— 每个班有多少学生
select classid,count(sid) from student group by classid;
— sc 每个学生的平均分
select sid,avg(score) 平均分, sum(score) 总成绩,max(score) 最高分, min(score) 最低分, count(*) 次数 from sc group by sid;

四、聚合函数

语法:之前我们做的查询都是横向查询,它们都是根据条件一行一行的进行判断,而使用聚合函数查询是纵向查询,它是对一列的值进行计算,然后返回一个结果值。聚合函数会忽略空值 NULL。

— 统计个数 count(字段)/字段可以写*、常量、任意字段名/count不统计数据为null的个数。

— 统计平均值 avg(字段)

— 统计最大值 max(字段)

— 统计最小值 min(字段)

— 统计总和 sum(字段)

eg: select count(*) 总个数, sum(score) 总成绩, avg(score) 平均分, max(score) 最高分, min(score) 最低分 from sc;

# 聚合函数
count(字段) — 统计个数
— 数字
avg(字段) — 平均值
sum(字段) — 总和
max(字段) — 最大值
min(字段) — 最小值
— count 统计个数
select count(*) from student where ssex = ‘男’;
select count(sname) from student where ssex = ‘男’;
select count(1) from student where ssex = ‘男’;
select count(‘a’) from student where ssex = ‘男’;
— count() 不统计null
select count(birthday) from student;
— avg 平均值
— 所有学生的平均分
select avg(score) from sc;
— sum 总成绩
select sum(score) from sc;
select count(*) 次数, sum(score) 总成绩, avg(score) 平均分, max(score) 最高分,min(score)最低分 from sc;

到此这篇关于MySQL的 DDL和DML和DQL的基本语法的文章就介绍到这了,更多相关MySQL的 DDL和DML和DQL内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!


数据运维技术 » MySQL的 DDL和DML和DQL的基本语法详解