欢迎投稿

今日深度:

Oracle,

Oracle,


--创建用户
create user xxx --创建用户xxx
identified by xxx --认证密码 xxx
account unlock; --用户是否开放 锁定或者解锁 lock\unlock

--授予权限给用户
grant connect,resource to xxx; --授予 连接\资源 to xxx;

--查询语法
select * from emp;

--别名用法
--别名中,有没有双引号的区别就在于别名中有没有特殊的符号或者关键字.
select ename as name,hiredate "日期" from emp;

--消除重复的数据
--加关键字(distinct)可消除重复的行,如果查询多列的必须保证多列都重复才能去掉重复
select distinct job from emp;

--查询中四则运算
--查询每个雇员的年薪
--sql中支持四则运算"+ - * /"
select ename,sal from emp; --正常查询数据
select ename,sal*12 from emp; --查询sal列乘12的结果
select ename,sal*12 income from emp; --乘12结果 列别名显示数据

--什么是空值?
--
/*
空值是无效的,未指定的,未知的或不可预知的值
空值不是空格或者0
注意:包含null的表达式都为null
空值永远不等于空值
*/

--连接符\\
--mysql中字符串连接查询
select concaat(id,'学号 ',cardno,'的卡号') from card;
--oracle中字符串连接查询
select '编号是:' || empno || '的雇员,姓名是:'|| ename || ',工作是:'||job from emp;

--条件查询
--使用where关键字对结果进行过滤
select * from emp where condition(s);

--非空和空的限制
select * from emp where comm is not null; --查询 comm字段值不为空的.

select * from emp where comm is null; --查询 comm字段值为空的

select * from emp where comm is not null and sal > 1500; --查询 comm字段值不为空和sal字段值大于1500的

select * from emp where comm is not null or sal > 1500; --查询comm字段值不为空或者sal字段值大于1500的

select * from emp where comm is null and not(sal>1500); --查询comm字段值是空的和不包括sal>1500

--范围限制
select * from emp where sal > 1500 and sal < 3000; --查询sal字段值大于1500 和 sql字段值小于 3000的

select * from emp where sal between 1500 and 3000; --查询sal字段值1500到3000之间的

--在oracle中的查询条件中查询条件的值是区分大小写的
select * from emp where ename = 'smith'; --小写
select * from emp where ename = 'SMITH'; --大写

--查询指定的范围
--使用 in 关键字
select * from emp where empno in (7369,7499,7521); --查询雇员编号是7369,7499,7521的具体信息

select * from emp where ename in ('SMITH','ALLEN','WARD'); --查询雇员姓名是:'SMITH','ALLEN','WARD'的信息

--模糊查询
--使用关键字LIKE完成
--LIKE中主要使用的两种通配符
--"%" :可以匹配任意长度的内容
--"-" :可以匹配一个长度的内容
select * from emp where ename like '_M%'; --查询雇员姓名中第二个字符包含"M"的雇员

select * from emp where ename like '%%'; --like中如果没有通配符表示查询全部

select * from emp where ename like '%M%'; --查询名字中带有"M"的雇员

--Oracle 中不等号的用法有两种形式"<>"和"!="
select * from emp where empno <> 7369; --查询编号不是7369的雇员信息

select * from emp where empno != 7369; --查询编号不是7369的雇员信息

--Order by 对结果排序
--order by 列明 指定ASC 是升序排列 可以不指定 默认排序是升序排列
select * from emp order by sal; --查询雇员的工资从低到高

--指定 DESC 是降序排列
select * from emp order by sal desc; --查询雇员的工资从高到低

--如果存在多个排序字段可以用逗号分隔
select * from emp order by sal asc,hiredate desc; --查询雇员的工资从低到高的基础上进行日期降序

--排序中空值问题
--可以使用nulls first,nulls last来指定null值显示的位置
select * from emp order by sal nulls first; --查询雇员的工资从低到高 的基础上把空值放到最前

select * from emp order by sal desc nulls last; --查询雇员的工资从高到低 的基础上把空值放到读数据后的末尾

--单行函数
--字符串的连接可以使用concat可以使用"||"建议使用"||"
concat('hello','world')
select concat ('hello','world') from dual; --接收字符输入返回字符或者数值,dual是伪表

--字符串的截取,使用substr,第一个参数是源字符串,第二个参数是开始索引,第三个参数长度,开始的索引使用1和0效果相同
substr('hello',1,3)
select substr('hello',0,3) from dual; --截取字符串hello从索引0位开始截取3位

--获取字符串的长度
length('hello')
select length('hello')from dual; --获取字符串的长度 从伪表显示

--字符串替换,第一个参数是源字符串,第二个参数被替换的字符串,第三个是替换字符串
replace('hello','o','x')
select replace('hello','o','x') from dual;

--日期函数
/*
Oracle中的日期:
Oracle中的日期型数据实际含有两个值:日期和时间
默认的日期格式是DD-MON-RR
日期的数字运算
在日期上加上或减去一个数字结果仍为日期
两个日期相减返回日期之间相差的天数
可以用数字除24
*/
--日期函数示例
--查询雇员进入公司的天数(sysdate-入职日期)/7就是周数
select ename,round((sysdate -hiredate)/7) from emp; --查询雇员进入公司的周数

--获得两个时间段中的月数:MONTHS_BETWEEN()
select ename,round(months_between(sysdate,hiredate))from emp; --查询所有雇员进入公司的月数

--获得几个月后的日期:ADD_MONTHS()
select add_months(sysdate,3) from dual; --求出三个月后的日期

/*
转换函数
*/

 

www.htsjk.Com true http://www.htsjk.com/oracle/28027.html NewsArticle Oracle, --创建用户 create user xxx --创建用户xxx identified by xxx --认证密码 xxx account unlock; --用户是否开放 锁定或者解锁 lock\unlock --授予权限给用户 grant connect,resource to xxx; --授予 连接\资源...
相关文章
    暂无相关文章
评论暂时关闭