当前位置:网站首页>DDL,DQL,DML语句
DDL,DQL,DML语句
2022-07-26 18:24:00 【不2002】
首先sql语句不区分大小写
DDL
# 库的操作
-- 显示所有的库
show tables;
-- 创建一个库
-- create database 库名;
create database hehe;
-- 删除一个库
-- drop database 库名;
drop database hehe;
-- 使用库
-- use 库名;
use hehe;
# 表的操作
-- 查看库中所有的表
show tables;
-- 删除表 drop table 表名
-- 创建表
create table tab_teacher (
t_name varchar (10) COMMENT '姓名',
t_sex char (1),
t_birthday datetime,
t_money decimal (10, 5)
);
-- 查看表结构
-- 一,desc 表名;
-- 二,show create table 表名;
show create table Student;

DML语句
数据增加 关键字 INSERT,主要功能是给数据库表增加记录。
数据删除 关键字 DELETE,主要功能室删除数据库表中已有的记录。可以依 照条件去做修改。
数据修改 关键字是 UPDATE,主要功能是修改数据库表中已有的记录。可
以根据条件去做修改。
新增
insert into 表名(字段名,字段名...字段名) values(值,值...值);
一次性插入多条数据
Insert into 表名(字段名,字段名) values(值,值),(值,值);
不常用的新增方式
第一种 要求表存在
语法
insert into 往该表里增值 select sname,ssex from student
第二种 表不存在
建表时插入数据
语法
create table 新表名 select sname,birthday,ssex from student
修改
语法
update 表名 set 字段名=值,字段名=值... where 子句
WHERE子句,后面判断为true执行前面查询为false不执行

删除
语法
delete from 表名 where 子句
三种删除方法
- Drop 表名 该方法会把该表的表结构删除
- Delete from 表名 该方法不会删除表结构,会删除所有表内容,但不会删除索引
- truncate 表名 该方法会删除所有表内容,会删除索引,不会删除表结构
mysql8 新特性
CREATE TABLE jsltab(
id INT PRIMARY KEY AUTO_INCREMENT,
a INT ,
b INT ,
c INT GENERATED ALWAYS AS (a + b) VIRTUAL
)
DQL
单表查询
语法
select * from 表名;
*号代表所有,也可以写需要展示的字段名
Select 字段名,字段名… 表名;
distinct去除重复
注意事项:distinct 如果单独使用会去除该字段相同的 如果有多个字段,只会去除全部字段都相同的内容
用法
select distinct 字段名1,字段名2…. from 表名;
带条件的查询 WHERE 子句
语法
select * from 表名 where 字段名 between 3 and 7;
模糊查询 LIKE
-- 模糊符号 % 任意多的任意字符
-- 模糊符号_ 一个任意字符
语法
select 字段名 from 表名 where 字段名 like '%张%';
select 字段名 from 表名 where 字段名 like '张__';
in 在特定的范围查找
select * from 表名 where 字段名 IN(1,3,4,6,9);
null 不做运算所以在左null查询时 把=号,换做is 或 is not
聚合函数
-- count() 括号里可以填常量或字段名或* 不统计null 统计个数
select count(' ') from student;
-- avg(字段名) 统计平均数
select avg(score) from sc;
-- sum(字段名) 统计总和
select sum(score) from sc;
-- max(字段名) 统计最大数
select max(score) from sc;
-- min(字段名) 统计最小数
select min(score) from sc;
-- 联合使用
select count(*) 个数,avg(score) 平均分,sum(score) 总分,max(score) 最大值,min(score) 最小值
from sc;
;
分组
-- group by 根据该字段分组
语法
select classid,count(sid) from student group by classid;
起别名 as '别名' 或直接 '别名' 再或者 别名
查询语法规则

WHERE子句
WHERE条件子句不是必须的;
• WHERE子句,可以给查询增加条件;
• 条件:为筛选条件,如不指定则修改该表的所有数据。
Group by 分组
1、对所有的数据进行分组统计;
2、分组的依据字段可以有多个,并
依次分组。
Having
与GROUP BY结合使用,进行分组
后的数据筛选。
Order by 排序
• ORDER BY 表示对SELECT语句查询得到的结果,按字段名进行排序;
• DESC表示排序的顺序为降序,ASC表示排序的顺序为升序;
Limit 分页(limit(n,m))
• LIMIT关键字是MySQL特有关键字;
• LIMIT限制SELECT返回结果的行数;
• n 从第几个元素开始,m 表示显示记录的数量;
•“[ ]”包含的内容可以省略。
高级dql语句 多表联查
等值联查
示例:
select student.* from student,class,sc,teacher,course where student.sid = sc.sid and sc.cid = course.cid
and student.classid = class.classid and teacher.Tid = course.tid and tname = '张三'
内联 inner join 表名 on 表关系
外联 主表 left join 副表 on 表关系
副表 fight join 主表 on 表关系
链接查询 union
示例
select * from student left join class on student.classid = class.classid
where class.classid is null
union
select * from student right join class on student.classid = class.classid
where student.classid is null
union all 是求两个查询的并集,但是不会把重复的过滤掉,而是全部显示出来
where子查询 (内部查询)
示例:
select * from student where sid in(
select max(sid) from student group by classid
)
from子查询
select classname,人数 from class
left join
(select classid,count(*) 人数 from student group by classid)t1
on class.classid = t1.classid
边栏推荐
- 客户案例 | 聚焦流程体验,助银行企业APP迭代
- 最后一篇博客
- Distributed transaction Seata
- 密码一致,总显示如下图
- torch.unsqueeze() squeeze() expand() repeat()用法及比较
- Usage scenarios for automated testing
- If the key is forgotten and multiple devices have different keys, how does the cloud synchronize
- Typescript stage learning
- 【YOLOv5】--详细版训练自己的数据集 保姆级学习日志记录 手把手教程
- 查看容器的几种方式
猜你喜欢

Description of MDM separation of powers and classification and grading authority

MySQL教程:MySQL数据库学习宝典(从入门到精通)

Aof & RDB of J2 redis

如何保护电子商务网站免受网络攻击?
![[MySQL from introduction to proficiency] [advanced chapter] (VIII) clustered index & non clustered index & joint index](/img/18/1644301d575fad0a0d0f15932487d0.png)
[MySQL from introduction to proficiency] [advanced chapter] (VIII) clustered index & non clustered index & joint index

安全测试与功能测试、渗透测试你知道有什么区别吗?

Network protocol: tcp/ip protocol

这22个绘图(可视化)方法很重要,值得收藏!

C language - Introduction - syntax - string (11)

什么是服务器集群?海外服务器集群的优势?
随机推荐
从6月25日考试之后,看新考纲如何复习PMP
Cannot find current proxy: Set ‘exposeProxy‘ property on Advised to ‘true‘ to make it available
MongoDB stats统计集合占用空间大小
Pychart loads CONDA to create a pytorch virtual environment and reports an error. It is normal on the CONDA command line
C#获取本地时间/系统时间
客户案例 | 聚焦流程体验,助银行企业APP迭代
Gongfu developer community is settled! On July 30!
Reentrantlock learning --- basic method
ReentrantLock学习之---基本属性
LeetCode笔记:Weekly Contest 303
Wechat applet plug-in -- wxml to canvas (generate pictures)
用低代码搭建千人食品制造企业高效管理系统案例分析
EN 1504-7混凝土结构保护和修理用产品钢筋防腐—CE认证
首席信息官引导业务变革的指南
第九章 实用建模技术
那些年解的疑难性能问题 --- ext4碎片整理
Still using xshell? You are out. I recommend a more modern terminal connection tool
2022 build enterprise level data governance system
Volatile keyword of JVM memory model
Mongodb stats counts the space occupied by the collection