Database—DML
待整理
5.DML常用语句:
1.insert:插入记录
2.update:修改记录
3.delete:删除记录
4.select:查询记录
6.insert:
1.语法:
1.insert into 表名 values(值1,值2··· ···);
2.insert into 表名(列名1,列名2···) values(值1,值2···);
3.insert into 表名 查询语句
2.例句:
1.insert into books(bookname,bookprice) select name,price from libary //将libary中的name,price添加到books中
7.select:
1.语法:
1.select 列名|表达式|*
2.from 表名
3.where 条件语句 //分组前过滤
4.group by 列名|表达式 //分组
5.having 条件语句 //分组后过滤
6.order by 排序 [asc,desc] //排序
7.limit 起点,记录数 //截取
2.条件子句中可以使用的谓词
1.>,<,!=,=,<=,>=
2.in
3.not
4.between and:连续的区间
5.is null,is not null:不能使用=null来判断是否为空
6.like 模糊查询
1._ :任意一个字符
2.% :任意多个字符
7.and or:逻辑运行符
3.聚合函数(集合函数):
1.count(列名l*):记数
2.sum() :求和
3.avg() :平均数
4.max() :最大数
5.min() :最小值
4.where与having的区别:where是在分组之前进行过滤,having是在分组之后进行过滤,因此在having子句中可以使用分组之后的结果
5.例句:
1.select distinct bookname,'hello' 哈喽 from books //查询不重复的书名并在其后加一列名为哈喽,内容为hello的列
2.select distinct bookname as "书 名",bookid //将列名改为“书名”显示,as可以省略,书名中间无空格时" "可以省略
3.select * from `user` where name like '%阳%'; //name中含有“阳”的元素,‘--阳’代表name中为XX阳的元素
4.select SUM(salary),count(*),avg(salary),count(DISTINCT deptnu) 'sort of deptnu' from `user`
5.select u.deptnu,d.dname,SUM(salary),count(*) from `user` u,dept d where u.deptnu=d.deptnu group by u.deptnu //分组必须和聚合搭配使用;多条语句分组,将多个列元素都相同的分为一组,列名之间用逗号隔开;使用分组时只能select聚合函数,select列则报错(mysql中不报错,但查得数据是错误的)
6.select deptnu,SUM(salary),count(*) from `user` group by deptnu having COUNT(*)>2
7.select * from `user` order by salary,deptnu //先按照salary,再按照deptnu从小到大排序
8.select * from `user` order by salary desc //按照salary从大到小排序
9.select * from `user` order by salary desc,deptnu limit 0,3 //截取结果,从0开始,截取三个
8.update:更新语句
1.语法:update 表名 set 列名1=值,列名2=值 where 条件
2.例句:
1.update `user` set salary=10000,deptnu=4 where name='陈衍舟'
2.update `user` set salary=salary+100 //所有人的salary加100
3.update `user` set name=lower(name) //所有人的名字改为小写
9.delete的语法:
delete from 表名 where 条件
高级查询:
1.表连接:
1.连接方式:
1.select *|表达式|列 from table1 t1,table2 t2
where 连接条件
2.标准的sql-99语句
2.笛卡尔积:两个集合的乘积,即表一的每一条记录(如10条)与表二的每一条记录(如5条)都关联,结果为50条
3.表的别名:上述的t1就是table1的别名,进行连接条件判断时,可以通过别名调用列名
4.多表连接:三个表连接一般至少要两个条件
5.非等值连接:where 列名 between and
6.标准的sql-99
1.语法:
select * from table1
连接方式
table2
on 连接条件
2.分类:
1.内连接:完全依赖于连接条件;inner join
2.外连接:保证其中一边表的数据一定会被查出来,另一边没有则用null填充
1.左外连接:左边的表一定被查出来;left (outer) join
2.右外连接:右边···;right [outer] join
3.全外连接:两边···;full [outern] join ;mysql不支持
3.自连接:
7.例句:
1.select * from `user`,salaryclass where salary BETWEEN low_salary and high_salary //查询每条记录salary对应的等级
2.select * from dept d left outer join `user` u on u.deptnu=d.deptnu //左外连接
2.子查询(嵌套):
1.在一个查询语句中包含另一个查询语句
2.注释:-- 注释语句
3.例句:
1.select * from `user` where deptnu=(select deptnu from dept where dname='开发部') //查找开发部在user中对应的记录
2.select * from `user` where salary>(select AVG(salary) from `user`) //查找所有大于平均工资的记录
3.select * from `user` where salary in (select max(salary) from `user` where deptnu=3) //比所有deptnu=3的salary都大的salary
4.select * from `user` where deptnu=1 or deptnu=2
5.select * from `user` where deptnu in|any|all|not in(select deptnu from dept where dname in('开发部','销售部'))
6.select * from `dept` where deptnu not in(select DISTINCT deptnu from `user`) //查找没有人的部门
7.select count(*),u.deptnu,dname from `user` u,dept d where u.deptnu=d.deptnu group by u.deptnu having count(*)>(select count(*) from `user` where u.deptnu=1) //查找人数比deptnu=1多的记录
8.select * from (select deptnu,count(*),avg(salary) sal from `user` group by deptnu) a where a.sal>10000 //查出分组后平均salary大于10000的记录
4.位置:
1.where:
2.having:
3.from:
4.select:不常用
5.常用谓词:因为> <,>=,=,<=只能比较一个唯一值,而不对多个查询记录进行比较,因此推出了一些用于集合操作的谓词
1.all:
2.any:
3.in:
4.not in:
6.exists(存在)|not exist:
1.语法:select * from 表名 where exists(查询语句)
2.例:
1.select * from tb_class where classid in(select classid from tb_student) //先查里面
2.select * from tb_class c where exists(select classid from tb_student s where c.classid=s.classid) //先查外面,再查括号里的内容看结果返回的是true或false,查询出有学生的班级,外面少用exist更合适,否则用in性能更好
3.函数(自查API):
1.字符函数:
2.数值函数:
3.时间函数:
1.根据出生日期计算年龄:
1.select year(SUBTIME(NOW(),SBIRTH)) age from student
2.select DATEDIFF(NOW(),SBIRTH)) age from student
4.加密:
5.系统函数:
6.case when:实现java的分支
1.(可以完成行变列的查询)
2.例句:select ACCOUNTID,name,case when remain<1000 then '丝丝' when remain>10000 then'富豪' else '中产阶级' end from ACCOUNT
Database—DML相关推荐
- 76 道 Oracle Goldengate 面试问题
基础 12c新特性 性能 Troubleshoot 其它 1. Oracle Goldengate 支持部署到哪些拓扑? GoldenGate supports the following topol ...
- SQL SERVER – Beginning of SQL Server Architecture – Terminology – Guest Post
AUGUST 30, 2012 BY PINAL DAVE SQL SERVER – Beginning of SQL Server Architecture – Terminology – Gues ...
- oracle 数据库回闪,各种数据库闪回的总结
本帖最后由 guoyJoe 于 2013-3-26 21:15 编辑 一.Fashback Query闪回查询:Books-->APP-->Application Developer's ...
- sql server 面试_SQL Server审核面试问题
sql server 面试 In this article, we will discuss a number of common and important SQL Server Audit que ...
- PLSQL Developer几个可能的隐患
PL/SQL Developer是一个集成开发环境,由Allround Automations公司开发,专门面向Oracle数据库.作为一款第三方工具,早就被广大Oracle开发和运维人员所熟知.相比 ...
- 【ogg Reference Part II Oracle GoldenGate Microservices Architecture】5 Oracle GoldenGate AdminClient
您可以使用Oracle GoldenGate MA使用HTML用户界面配置和管理数据复制. 本部分描述可以与Oracle GoldenGate微服务体系结构一起使用的命令.参数和选项. 5 Oracl ...
- Database之SQLSever:T-SQL数据语言操作(数据定义语句DDL、数据操作语句DML、数据控制语句DCL、其他基本语句、流程控制语句、批处理语句)概念及其相关案例之详细攻略
Database之SQLSever:T-SQL数据语言操作(数据定义语句DDL.数据操作语句DML.数据控制语句DCL.其他基本语句.流程控制语句.批处理语句)概念及其相关案例之详细攻略 目录 T-S ...
- 【数据库基础知识二】数据库DataBase(简称DB),数据库基本概念、DDL:数据定义语言,用来操作数据库对象、DML:数据操作语言,用来操作表中的数据
14天阅读挑战赛 目录 数据库基本概念 数据库DataBase(简称DB) 数据库管理系统Database management system(简称DBMS) 如何操作DBMS? 连接数据库的方式: ...
- Service Mesh是大方向,那Database Mesh呢?
在微服务和云原生大潮的卷席之下,服务化一直以来是人们关注的重点.但服务化之后,真正绕不开的数据访问却鲜有论道.尽管目前的关系型数据库远达不到云原生的要求,并且对分布式的不友好在长期以来也饱受诟病,但不 ...
- Azure SQL Database (19) Stretch Database 概览
<Windows Azure Platform 系列文章目录> Azure SQL Database (19) Stretch Database 概览 Azure SQL Da ...
最新文章
- 泼点冷水,P2P借款限额是不是想的太美好?
- 诚意租房网blog2
- 天龙八部3d最新服务器,天龙八部3DIOS正版服务器整合互通公告
- MapReduce入门2-流量监控
- 记录一次IDEA开发JavaWeb项目时JS中文乱码排错方法
- 胜过对手,先从微笑开始
- 实战Node:幼教平台幼教资源部分实现
- 《团队-团队编程项目作业名称-最终程序》
- [ openwrt ] 添加一个通过GPIO控制的LED
- 校园智能安防监控解决方案
- Cpu调优 mpstat 命令
- 英语四六级选择题自动判卷算法
- 安科瑞电力监控系统在教学演示中的应用
- 18:(基于对抗的学习)Learning a Self-Expressive Network for Subspace Clustering
- 抗扰度试验--EMS--电压暂降、短时中断--DIP
- 致远A8+协同管理软件存在远程Getshell漏洞
- 【翻译】Towards Real-World Prohibited Item Detection: A Large-Scale X-ray Benchmark
- 对2023015期双色球号码的可能性进行计算
- TFX发展简史/《Towards ML Engineering: A Brief History Of TensorFlow Extended (TFX)》
- kali下hydra的使用
热门文章
- win7更新服务器证书,ie浏览器网站安全证书更新方法介绍
- 关于项目一直updating indexes不停的解决方案
- 播放音乐的html代码,音乐播放器-html代码
- 【渝粤题库】陕西师范大学200531 英语测试 作业(高起本、专升本)
- C/C++音乐播放(亲测有效)
- MDK 使用 ST-Link 下载出现 target dll has been cancelled 的错误的解决方法
- 康托尔悖论:大全集不存在,即包含一切集合的集合是否存在
- 揭开Angular 7的神秘面纱
- 内核SIP ALG学习指引和基本实现原理(分析BCM方案实现)
- 制造业数字化转型的意义是什么?