欢迎投稿

今日深度:

Oracle数据库-DML(数据操作语言),oracle-dml

Oracle数据库-DML(数据操作语言),oracle-dml


DML:数据操作语言
注意:增删改查中查询最为重要,是核心,也最有含金量。
就是说,这节!很重要

一。select【查询】
先说select 的语法结构[最简版]
1.查询所有字段
select * from 表名;
2.查询指定字段
select 字段名,字段名,字段名 from 表名;
3.获取系统时间【通过sysdate来获取系统时间】
select sysdate from dual;

二。算术操作符【+ - * /】
这节也很简单
列如:
select studentname,(studentage/2)+2-18/3 from t_student;
首先他也是执行算术操作符优先级的,其他的没什么了

三。别名
首先说别名就要注意两点
1.别名要用双引号括起来
2.通过as起别名

起别名是分给字段与表两个的
首先是字段
还是用刚刚的例子
select studentname "stuName",(studentage/2)+2-18/3 "age" from t_student;
这里面的"stuName"与"age"是可以换成汉字的
或者是查询所有的信息
select studentname as "stuName",(studentage/2)+2-18/3 as "age" from t_student;
然后说表起别名
select s.字段名1,s.字段名2 from 表名 s;

四。连接操作 ||
【在oracle中字符串用单引号括起来】
列如:
select 字段名1||':'||字段名2 "编写的字段名字" from 表名;
这样他就输出的是:以“编写的名字”为字段名,表里面是“字段名1:字段名2”
||其实起到的效果相当于java里的+

五。where子句【where后面加查询条件】
where子句里右分两组
1.比较操作符【=,>,<,>=,<=,!=,<>】
2.逻辑操作符【or,and,not,in,like,between...and,is null】

先说说比较操作符,
这里面其实都很简单,但有两个需要说一下其他的举个例子就一带而过了
列:
查询年龄为20岁的学生的信息
select * from t_student where studentage=20;
而这里面主要说“!=”与“< >”
这两个都是不等于的意思

列如:
查询年龄不等于20的学生的信息
(他是有三种解决方案的)
select * from t_student where studentage<>20;
select * from t_student where studentage!=20;
select * from t_student where studentage>20 or studentage<20;

然后说说逻辑操作符
首先说这些单词都是什么意思
or(或者),and(并且),not(不),in(在。。里面),like(像),
beteen。。and。。(在。。之间),null(空值),default(默认)


那我们就再举个例子
如:查询学生姓名为张三并且年龄大于20岁的人
select * from t_student where studentname='张三' and studentage>20;
或者:查询年龄为18,27,30的学生的信息
select * from t_student where studentage in (18, 27, 30);
select * from t_student where studentage=18 or? studentage=27 or studentage=30;
这样类似的方法,其他的我就不细说了,还是主要说说一些不太好理解的
like(像)可以理解为:模糊查询
他是有俩符号的
%:代表0个或多个字符
_:代表任意一个字符

怎么用?还是用例子说话
查询所有姓张的学生的信息
select * from t_student where studentname like '张%';
select * from t_student where studentname like '张__';
或者是包涵某个字的,这些是都可以实现找到的

再说说between...and...
还是例子说话
查询年龄在18-25之间的学生信息
select * from t_student where studentage between 18 and 25;
也可以是不在这个年龄段的
select * from t_student where studentage not between 18 and 25;
总之,代码是千变万化的,要学会大胆想象与实际操作
然后就是空值null
列:查询生日为空的学生的信息
select * from t_student where birthday is null;
当然他也可以和别的搭配
如:和is null是为空值,is not null是不为空值,
或者not null不为空值,等等,


六。order by 排序
排序。也分俩
sc:升序[默认]
desc:降序

不废话,上例子:
按照年龄对学生进行降序排列
select * from t_student order by studentage asc;
select * from t_student order by studentage desc;(升序)
按照年龄对学生进行降序排列并且按照体重进行升序排列[多字段排序]
select * from t_student order by studentage desc,weight asc;

七。聚合函数
额。。这个就分的多咯
min():最小值
max(): 最大值
avg(): 平均值
sum(): 求和
count(): 总个数

例如:
查询班级中最小的年龄
select min(studentage) from t_student;
最大值,最小值,求和和平均值都一样,我就不凑字了
直接看总个数
班级中学生的个数

select count(*) from t_student;
select count(id) from t_student;--实战中
select count(1) from t_student;--实战中

有人要问了*是什么?
上面忘了说,*代表的是全部字段
还有,那为什么id和1也可以实现与*相同的作用?
为什么在别处找不到或没见过别人用?
因为这是在数据库管理员在写程序时看到的
照他解释,1与id也可以实现一样的作用,
但要注意,id是自己定义出来的字段,
还要自己去定义添加数字,不是凭空出来的哦、、、


在这里我插一句
教大家一个单词decode(值,值1,结果1,值2,结果2,最终结果)
列如:
select decode(10,1,'a',2,'b',3,'c','kong') from dual;
意思很简单,在值10中,符合哪个输出哪个值后对应的结果,否则,输出最终结果
比如还可以这样用:
select decode(sex,1,'男','女') as sex,count(id) from t_student group by sex;

八。数学函数
分五个:
abs( ) 绝对值
ceil()向上取整
floor( ) 向下取整
trunc( )截断函数
round( )四舍五入函数
就拿abs()说

select abs(-3) from dual;
select abs(studentage-100) from t_student;
向上取整与向下取整和四舍五入就不用我说了吧?
trunc()截取
select trunc(3.98) from dual;\\输出的是3
select trunc(3.98, 1) from dual;\\输出的是3.9(保留一位小数)
select trunc(334.98, -1) from dual;\\输出的是330(向前取整!!)

九。字符串函数
重点来了啊!

1.concat(字符串1,字符串2)

连接两个字符串
eg:select concat(studentname,'来到飞狐教育')||'实战教学' from 表名;

2.initcap('字符串')
使字符串首字母变为大写

3.instr(源字符串,要搜索的字符)
在一个字符串中搜索指定的字符,返回发现指定的字符的位置
里面的字符可以多写,但里面的要用逗号隔开,相当于java中indexOf

4.length(字符串)
返回字符串的长度

5.lower(字符串)
返回字符串,并将所有的字符小写

6.upper(字符串)
返回字符串,并将所有的字符大写

7.rpad|LPAD(源字符串,总长度,填充的字符)
在字符串的右(左)边粘贴字符
EG:select replace('abcdeaa', 'a', 'M') from dual;\\输出为MbcdeMM

8.rtrim|ltrim (源字符串,查找的字符串)
删除右(左)边出现的字符串
eg:select ltrim('abcd','ab') from dual;\\输出为:bc

9.substr(源字符串,开始位置,要截取的个数)
取子字符串,从start开始,取count个
eg:select substr('abcd',1,2) from dual;\\输出为:ab
select substr('abcd',2,2) from dual;\\输出为:bc

10.replace(源字符串,要查找的字符串,替换的字符串)
替换字符串

11.reverse(源字符串)
反转字符串中的每个字符
这次的不好理解,我都写出了他们的格式了

十。转换函数
1.to_char():将其转换为字符串类型
在实战中通常通过to_char()将日期类型转换为字符串类型

eg:
select to_char(sysdate, 'yyyy-MM-dd') from dual;\\输出为:当前时间的年月日
select to_char(sysdate, 'yyyy-MM-dd HH:mi:ss') from dual;\\输出为:当前时间的年月日时分秒(12小时制)
select to_char(sysdate, 'yyyy-MM-dd HH24:mi:ss') from dual;\\输出为:当前时间的年月日时分秒(24小时制)
2.to_date():将其转换为日期类型
在实战中通常通过to_date()将字符串类型转换为日期类型

eg:
select to_date('2017-10-13', 'yyyy-MM-dd') from dual;\\输出为:2017\10\13
select to_date('2017-10-13 15:23:10', 'yyyy-MM-dd HH24:mi:ss') from dual;\\输出为:2017/10/13 15:23:10

十一。日期和时间函数
三个
1.add_months()增加或者减去月份
eg:

select add_months(sysdate, 2) from dual;\\输出为:当前时间加两个月的时间

2.last_day()返回所在月份的最后一天

eg:

select add_months(sysdate, -2) from dual;\\输出为:当前时间减两个月的时间

3.next_day()给出日期和星期之后计算下一个星期的日期
eg:
select add_months(to_date('2017-02-10','yyyy-MM-dd'), -2) from dual;\\输出为:2016/12/10

十二。其他函数
1.nvl(值1,值2) 如果值1不为空则显示值1,否则显示值2

举个例子:
select nvl(birthday,sysdate) from t_student;
\\输出为:如果birthday不为空则显示birthday,否则显示sysdate(当前时间的意思)

2.nvl2(值1,值2,值3) 如果值1不为空则显示值2,如果为空显示值3
eg:
select nvl2(birthday,sysdate,to_date('2017-02-10', 'yyyy-MM-dd')) from t_student;

\\输出为:如果birthday不为空则显示sysdate(当前时间的意思),如果为空显示2017-02-10

www.htsjk.Com true http://www.htsjk.com/oracle/24359.html NewsArticle Oracle数据库-DML(数据操作语言),oracle-dml DML:数据操作语言 注意:增删改查中查询最为重要,是核心,也最有含金量。 就是说,这节!很重要 一。select【查询】 先说select 的语法结构...
评论暂时关闭