基本使用
如何查看当前数据库状态
连接前:
ps -ef | grep ora_
ps -ef | grep asm_
oracle 3021 1 0 08:02 ? 00:00:00 ora_pmon_updb
ora_<进程名字>_<SID>
SQL * PLUS : sqlplus / as sysdba
链接后:
select open_mode from v$database;
read write ==> 读写
mounted ==>
read only ==>
read only with apply ==>只读并应用
如何查看当前的实例,多个实例如何做切换
查看当前的实例
连接前 : 依然可以使用ps -ef | grep ora 查看
连接后 :
select instance_name from v$instance;
多个实例之间进行切换
export ORACLE_SID=orcl
sqlplus / as sysdba
select instance_name from v$instance;
如何连接数据库 (SQL*PLUS)
sqlplus / as sysdba()
sqlplus : 表示关键字
/ :表示忽视用户名和密码 用户/密码
as .. : 作为dba登录
普通用户登录:
sqlplus scott/tengling
# sysdba : 启动关闭操作,修改删除数据库 创建spfile ......详情看文档
# sysoper : 启动关闭数据库,创建spfile ..详情查看文档
如何查看当前的用户
show user
sysoper --> public
sysdba --> sys
普通用户 --> 用户名
连接用户后如何做切换
不退出SQL * PLUS进行用户切换使用
conn 用户/密码 as (sysdba| sysoper)
切换普通用户: conn 用户/密码
#注意: 切换后检查一下当前用户 show user
查看当前用户下有那些对象
数据字典: -- system 表空间
三类:
dba_ :针对dba用户
all_ : 查看自己的对象,同时自己也可以查看有权限的
user_: 查看自己的
通常使用; dba_tables, dba_view , dba|all|user_indexes ,dba|all|user_objects
查看当前用户的对表
select * from tab;
描述对象
desc user_objects;
SQL> desc tab
Name Null Type
----------------------------------------- --------
TNAME NOT NULL VARCHAR2(30)
TABTYPE VARCHAR2(7)
CLUSTERID NUMBER
查看所有表
select object_name from user_objects where object_type='TABLE';
六. 如何查看对象
desc dictionary; 描述
SQL: 结构化的查询语言
分类:
DQL: --select
DML : -- insert ,delete,update ,merge
DDL : 数据定义语言 : -- create ,drop ,alter,truncate
TCL : 事物的控制语言 : --commit ,rollback ,savepoint
DCL : 数据控制语言 : grant,revoke
DQL : 数据库查询语言(select)
sql*plus 基础命令
l, (list) ==>查看缓冲区的命令 (一条命令)
r ,run,/ ==> 运行缓冲区的命令
c,(change)==> 做替换 c/旧行/新行
i,(input)==> 当前行插入新的一行 要制定当前行 l# 配合使用
a, (append )==> 追加命令
del (delete) # ==> 删除 ,如果不指定行,默认删除最后一行,删除可以有范围
cl buffer ==> 清空缓存区
cl scr ==> 清屏
spool off;
spool <path> [append] 命令保存到文本
get <path>==> 读取指定路径的语句到缓冲区
save <path> ==> 保存缓冲区中的语句到制定路径
start <path> ==> 执行制定路径下的查询语句
show all 显示所有参数
cl columns ==>清空列的别名
alter session set nls_date_format='yy'; 修改时间显示
修改参数后永久生效的方法:
1. vi /home/oracle/login.sql ==> set feeback 50 ....
2. vi /u01/oracle/11g /sqlplus/admin/glogin.sql (安装路径) ==> 编写
书写规则 :
1. 关键字不区分大小写, ‘ ’," " 需要区分大小写
2. 关键字不能分行缩写
3. 为了美观,可以换行书写
4. 遵循公司开发的命令习惯
5. 针对sqlplus ,以 ; 结束
语法 :
关键字 : select ,from ;
select column_name(列名) | * | alias(别名) | distinct | subquery...... from table_name | view | subquery .... ;
限制条件 where clause
select ...
from ...
where ...
比较条件:
=
!=
^=
<>
<
>
<=
>=
any : 大于其中一个
all : 大于全部
操作符:
in
between ....and
null
逻辑比较
not
and
or
模糊查询 like
% 0个或多个
_ 一个
练习:
1.查看员工表中员工和薪水的名字
select ename ,sal from emp;
2.查看部门表所有信息
select ename ,sal from emp;
3.查看员工一个季度的薪水
select ename,sal*3 from emp;
4. 查看员工一个季度的薪水,一个季度的薪水别名为TSL
select ename , sal *3 as tsl from emp;
5. 查看员工表中部门的列表 取重
select distinct deptno from emp;
select distinct (deptno) from emp;
select distinct (unique) from emp;
多列取重
select distinct job ,deptno from emp;
6. 将员工和员工ID 河在一个列去写
select empno,ename ,empno||ename from emp;
双竖线链接
select concat(ename,empno) from emp;
很少用,因为一次只用过一个
7. 显示 it 's ok
select 'it'||'''s ok ' from dual
select 'it''s ok ' from dual;
select q'/'s/' from dual;
8.查看员工编号
select empno from emp;
9.查看薪水等级表中所有的信息
select * from salgrade;
10.查看员工名字,职位,年薪
select ename,job,com from emp;
11.查看员工编号,薪水,奖金
select ename,job,com from emp;
12. 查看员工编号,每个季度薪水+奖金总和
select empno,sal+nvl(com,0) from emp;
13.显示姓名和编号(一组) 显示分行 ;
select ename||chr(10)||empno from emp;
14.查找10部门所有员工的名字,职位
select ename,job from emp where deptno=10;
15.查找薪水大于3000的员工
select * from emp where sal>3000;
16.查找薪水小于3000的员工
select * from emp where sal<3000;
17. 查找薪水等于3000的员工
select * from emp where sal=3000;
18. 查找薪水不等与3000的员工
select * from emp where sal!=3000;
select * from emp where sal ^=3000;
19. 查找名字等于BLAKE 的员工编号
select empno from emp where ename='BLAKE';
20. 查找入职时间为02-APR-81的员工姓名
select ename from emp where hiredate='02-APR-81'
调用函数to_char
21 . 查找员工工资在2000到3000的员工
select ename,sal from emp where sal between 2000 and 4000;
22 .查找员工工资不在2000到3000的员工
select ename,sal from emp where sal not between 2000 and 4000;
23. 查找员工部门在10或者20部门的员工
select ename,sal from emp where sal between 2000 and 4000;
24. 查找奖金为null的员工
select ename from emp where comm is null
25. 查找员工名字有A 的有那些?
select * from emp where ename like '%A%';
26.查找_特殊字符
select * from emp where ename like '%a_%' escape 'a'; escape ‘任意'
27.查找 %特殊字符
select * from emp where ename like '%a%%' escape 'a'; escape ‘任意'
28. 查找员工工资在2000到3000的员工
select ename,sal from emp where sal between 2000 and 4000;
29. 查找员工工资不在2000到3000的员工
select ename,sal from emp where sal not between 2000 and 4000;
30. 查找员工部门在10或者20部门的员工
select ename,sal from emp where sal between 2000 and 4000;
31. 查找奖金为null的员工
select ename from emp where comm is null
32. 查找员工名字有A 的有那些?
select * from emp where ename like '%A%';
33.查找_特殊字符
select * from emp where ename like '%a_%' escape 'a'; escape ‘任意'
34.查找 %特殊字符
select * from emp where ename like '%a%%' escape 'a'; escape ‘任意'
35.查找员工工资在2000到3000的员工
select ename,sal from emp where sal between 2000 and 4000;
36.查找员工工资不在2000到3000的员工
select ename,sal from emp where sal not between 2000 and 4000;
37.查找员工部门在10或者20部门的员工
select ename,sal from emp where sal between 2000 and 4000;
38.查找奖金为null的员工
select ename from emp where comm is null
39. 查找员工名字有A 的有那些?
select * from emp where ename like '%A%';
40.查找_特殊字符
select * from emp where ename like '%a_%' escape 'a'; escape ‘任意'
41.查找 %特殊字符
select * from emp where ename like '%a%%' escape 'a'; escape ‘任意'