1、Oralce中数据库名 SID等的区别
数据库实例名是用于和操作系统进行联系的标识,就是说数据库和操作系统之间的交互用的是数据库实例名。实例名也被写入参数文件中,该参数为INSTANCE_NAME。数据库名和实例名可以相同也可以不同。
在一般情况下,数据库名和实例名是一对一的关系,但如果在Oracle并行服务器架构(Oracle实时应用集群中)中,数据库名和实例名是一对多的关系。
查询数据库名用select name from v$database;
查询实例名用show instance_name from v$instance;
也可以用show parameter instance查实例名;
数据类型:
Number(p,s) :类似浮点型,最大精度p,小数位s位
Varchar2(s) :可变长字符类型
date : 日期型
Char(s) :定长字符型
2、操作符
- 乘除的优先级高于加减。
- 同一优先级运算符从左向右执行。
- 括号内的运算先执行。
- 对于日期型数据, 做 *, / 运算不合法
比较运算:
等于用 单=不能用两个==
<>和!=都代表不等于
:=这是赋值
小知识点:
- 包含空值的数学表达式的值都为空值
- 别名使用双引号!
- oracle 中连接字符串使用 “||”, 而不是 java 中的 “+”
- 日期和字符只能在单引号中出现. 输出 last_name`s email is email
3、关键字
1、 distinct: 去重(删除重复行)
例:查询employees表中的department_id 的id
SELECT distinct department_id
FROM employees;
这样就把department_id查出的重复的删掉了。
2、describe: 查询表结构 (简写:desc)
desc tablename;
这样就查出tablename表中有哪些字段 name id org 等等
3、between ··· and ···· : 在两个值之间(包含边界)
例:查询工资在 5000 – 10000 之间的员工信息.
1). 使用 and
select *
from employees
where salary >= 5000 and salary <= 10000
2). 使用 BETWEEN … AND …, 注意: 包含边界!!
select *
from employees
where salary between 5000 and 10000
4、in: 等于值列表中的一个
例: 查询工资等于 6000, 7000, 8000, 9000, 10000 的员工信息
1). 使用 or
select *
from employees
where salary = 6000 or salary = 7000 or salary = 8000 or salary = 9000 or salary = 10000
2). 使用 in
select *
from employees
where salary in (6000, 7000, 8000, 9000, 10000)
5、like: 模糊查询
1 查询 LAST_NAME 中有 ‘o’ 字符的所有员工信息.
select *
from employees
where last_name like '%o%'
2 查询 LAST_NAME 中第二个字符是 ‘o’ 的所有员工信息.
select *
from employees
where last_name like '_o%'
3查询LAST_NAME 中有 'a’和’e’字符的员工信息
select *
from employees
where last_name like '%a%e%' or last_name like '%e%a%'
6、escape: 转义字符
3 查询 LAST_NAME 中含有 ‘_’ 字符的所有员工信息
2). 使用 escape 说明转义字符.
select *
from employees
where last_name like '%\_%' escape '\'
这里的’'可以也可以是别的符号:where last_name like ‘%#_%’ escape ‘#’
7、is null: 空值
例:1. 查询 COMMISSION_PCT 字段为空的所有员工信息
select last_name, commission_pct
from employees
where commission_pct is null
- 查询 COMMISSION_PCT 字段不为空的所有员工信息
select last_name, commission_pct
from employees
where commission_pct is not null
8、order by: 使子句排序
ASC(ascend): 升序
DESC(descend): 降序
ORDER BY 子句在SELECT语句的结尾。
例:
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date DESC ;
9、months_between(date1, date2): 返回两个日期之间的月份数
如果两个日期月份内天数相同,或者都是某个月的最后一天,返回一个整数,否则,返回数值带小数,以每天1/31月来计算月中剩余天数,
months_between(date1, date2),必须注意的是,date1与date2都为Date类型