最近学习Oracle数据库。把学到的东西做一个总结。
本文主要讲解一下几个方面:
1.OracleXE数据库安装注意事项
2.Oracke 数据库查询
数据库查询这章主要介绍一下几个方面:
1.全表查询2.条件查询3.特殊谓词4.排序5.函数(包括一些常用的内置函数和组函数)
我使用的Oracle 是OracleXE,相对来说是比较轻量级的。
OracleXE的安装需要主要一下几点:
1.Oracle数据库的安装
注意:oracle安装路径需要不能含有中文或者空格。
注意:oracle system用户 密码为123456
2.OracleXE数据库的卸载
从新执行安装程序,选择卸载项
3.激活测试账号
进入命令行窗口执行
第一步:输入sqlplus
输入用户名和密码
第二步:键入 alter user hr account unlock;
如此既可以将测试账号改为hr
密码也为hr
Oracke 数据库查询:
sql语法不区分大小写。
1.全表查询
select * from 表名
select employee_id,frist_name,last_name,email....
from 表名
注意:* 进行又有列的查询,运行效率偏低。可读性差
2.查询的定列的内容
select 列名1,列名2,...
from 表名
3.对列的内容进行运算 (+-/)
select last_name,first_name,salary*12
from employees;
4.列起别名 alias
select 列名 [as] 别名,列名 [as] 别名
from employees;
注意as关键字可以省略
多列内容的连接
||运算符 可以完成2个列 或者 多个列的内容拼接
多列内容的连接
||运算符 可以完成2个列 或者 多个列的内容拼接
select first_name||last_name as name , salary
from employees;
2.条件查询
a.比较查询 > < = != >= <=
select * from employees where first_name = 'Lex'
注意 字符串应用 ' '
b.逻辑运算 and or not
select * from employees where first_name = 'Lex' and salary = 17000
select * from employees where first_name = 'steven' or salary = 17000
c.特殊谓词
eg:查询工资为6000 或者 工资为9000 或者 工资为24000.
一般的写法为;
select * from employees where salary = 17000 or salary = 9000 or salary = 6000
in或者not in
in 表达或者的关系
select * from employees where salary in (6000,9000,24000)
select * from employees where salary not in (6000,9000,24000)
eg:查询工资为6000 至 12000 这个范围内的员工 (包含 6000,12000)
可以写为:
select * from employees where salary >=6000 and salary<=12000
between .... and 在一个范围 或者 区间中 (闭区间)
not between ..... and
select * from employees where salary between 6000 and 12000
select * from employees where salary not between 6000 and 12000
查询某一些列的内容为null 不能应用 = null 判断
is null is not null
select * from employees where commission_pct is not null
like 模糊查询
eg:查询所有姓李的老师
select * from t_teacher where name like'李%'
eg:查询所有姓李的老师,且名字只有两个字
select * from t_teacher where name like'李%_'
d.排序子句(order by)
select first_name,salary from employees order by salary
默认升序asc
降序查询:
select first_name,salary from employees order by salary desc
注意:
order by 字句必须书写在sql语句的最后
eg:
select first_name,salary from employees where salary>6000 order by salary desc
多列排序
eg:
select first_name,salary from employees where salary>6000 order by salary desc,first_name desc
先以salary排序,如果salary相同,在以first_name 排序(按字母逐个排序)。
e:函数
1.内置函数
sysdate 获取当前系统时间
eg:
select sysdate from dual
oracle中的虚表,哑表 dual
获得当前时间 select sysdate from dual;
to_char(日期,’日期格式’)
作用:把一个日期类型,转换成一个字符串
select to_char(sysdate,'mm') from dual
yyyy:年份
mm:月份
dd:月份中的天
day:星期
hh:小时
mi:分钟
ss:秒
eg:查询4月份入职的员工
select *from employees where to_char(hire_date,'mm') = '04'
eg:查询3月18日入职的员工
select *from employees where to_char(hire_date,'mm-dd') = to_char(sysdate,'mm-dd')
to_date(‘字符串’,‘日期格式’)
把字符串转换成日期
eg:
select to_date('2016-03-18','yyyy-mm-dd') from dual
to_date函数 主要应用在数据的插入过程。
2.组函数
针对于一组数据的操作。默认在不分组的情况下,会把一张表的数据划分成一个组。
a. 取最大值 max()
查询最高的工资
select max(salary) from employees
b.取最小值 min()
查询最低的工资
select min(salary) from employees
c.取平均值 avg()
select avg(salary) from employees
d.取数据的和 sum()
查询员工工资之和
select sum(salary) from employees
e.取条数 count()
查询全表的条数
select count(*) from employees
运行结果:统计非null的条数。
网友评论