Oracle编程学习笔记

Sql语句 含义 备注
DESC EMP; 查询表结构 EMP为表名
SELECT table_name FROM user_tables; 查看当前用户下的所有表的名字
select*from emp; 查询emp表中的所有记录
DELETE FROM 表名称 WHERE 列名称 = 值 DELETE 语句用于删除表中的行。 DELETE FROM Person WHERE LastName = 'Wilson' 
DELETE  FROM table_name where id=1 删除id为1 的记录
DELETE  FROM table_name 所有表记录删除
drop table subject; 删除表subject
select Ename,job from emp; 查询ename和job这两个字段的值(列)
SELECT DISTINCT 列名称 FROM 表名称 去除查询出来的重复值 DISTINCT只能在select后边,去重的时候,要在一整行记录都重复的情况下才会去重,否则打破数据对应关系
select id*2 as id2 from emp 查询id*2的值显示为id2 as也可以用空格代替,给表取别名的时候只能用空格

eg:--查询编号  姓名  年薪  月薪   日薪   
select EMPNO AS 编号, ENAME AS 姓名,SAL*12 AS 年薪,SAL as 月薪,SAL/30 AS 日薪 from EMP; 
|| 拼接字符串 select '编号:'|| empno ||'姓名:'|| ename ||'月薪'||sal 描述
from emp ;
ORDER BY  用于对结果集进行排序。 select  * from emp order by id  DESC   按照ID降序排序                       
  ASC是升序(默认)
DESC是降序
is noll、is not null 判断null
=  null 判断值是否为null
a>200 and     c  <300  或者 between 200 and 300 范围查询【闭区间】
IN(10,20,30) 取10,20或者30
NOT IN(10,20) 不取10或20
like 模糊查询% 
表示任意个任意字符
_  (下划线)表示一个任意字符
select * from emp where job like '%S%';      包含S的
select * from emp where job like '_S%';        第二个字符为S的
select * from emp where job like 'S%';       以S开头的
select * from emp where job like '%S';      以S结尾的
SELECT table_name FROM user_tables; 查看当前用户下的所有表的名字
UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值 Update 语句用于修改表中的数据。 update emp SET  sal=400 where empno=7369;
NVL(列,数值)
NVL(列,'文本')
如果这个之值为空,则替换为数值
如果这个之值为空,则替换为文本
select sal+nvl(comm,0) as 工资 from emp;
select distinct student.name,grade.score from subject,grade,student where grade.score=90 and STUDENT.id=1;
多表关联查询
单行函数
字符函数 upper(str) 把str变为大写
lower(str) 把str变为小写
initcap(str) 把str首字母变大写
replace(str,f,s) 把f换为s
length(str) 长度
substr(str,index1,index2) 截取    左闭右开区间
substr(str,3) 截取3到结尾
substr(str,-3) 截取后三位
ASCII('a') 获取字符ASCII码
CHAR(100) 把ASCII码转化为字符
Ltrim(str) 去除str左边空白
trim(str) 去除str两边空白字符
Rtrim(str) 去除str右边空白
LPAD 填充       select LPAD('cc',10,'*')          用*代替cc左边不足10位的字符
RPAD 填充       select RPAD('cc',10,'*')          用*代替cc右边边不足10位的字符
instr(‘hello’,‘ll’) 返回ll在hello中的索引
concat    和    || 拼接
数值型函数 round(1.234) 四舍五入保留整数
round(1.234,2) 四舍五入保留2位小数
trunc(1.23) 直接舍弃小数
trunc(1.23,2) 保留两位小数截取
MOD(10,3) 10/3的余数(取模运算)
Ceil(45.67) 向上取整
Floor(45.56) 向下取整
日期函数 systimestamp 获取当前时间
add_months(sysdate,3) 在sysdate日期之后3个月的日期
add_months(sysdate,-3) 在sysdate日期之前3个月的日期
next_day(sysdate,'Sunday') 下一个星期天的日期
last_day(sysdate) 返回本月最后一天
Months_between(sysdate,hiredate) 返回两个日期的月差
extract(year from sysdate) 从sysdate中获取年份
extract(month from sysdate) 从sysdate中获取月份
extract(day from sysdate) 从sysdate中获取天
extract(hour from systimestamp) 从sysdate中获取时
extract(minute from systimestamp) 从sysdate中获取分
extract(second from systimestamp) 从sysdate中获取秒
to_timestamp(‘2018-12-12 12:3:4’,‘yyyy-mm-dd  hh24:mi:ss’) 格式化时间格式(字符串转日期)
转换函数 to_char(sysdate,‘yyyy-mm-dd  hh24:mi:ss’) 将日期格式转化为字符串
to_char(1234567,'999,999,999,999') 将格式转化为货币格式,如果为9则不会补0,0则不会
to_number('8') 将字符8转换为数字

猜你喜欢

转载自blog.csdn.net/caidingnu/article/details/80999909
今日推荐