*****************
文件操作命令
*****************
===========start和@
說明:運行sql腳本
案例:sql>@ d:\a.sql 或者 sql>START d:\a.sql
===========edit
說明:該命令可以編輯指定的sql腳本
案例:sql>edit d:\a.sql
===========spool
說明:該命令可以將sql*plus屏幕上的內容輸出到指定的文件中去
******************
登錄操作命令scott tigger
******************
===========清屏命令
clear;
===========設置行長
set linesize 120;
===========打開時間 如:(10:53:05 SQL>)
set time on
===========打開執(zhí)行語句時間
set timing on;
===========顯示用戶
show user
===========連接用戶(conn,connect無區(qū)別)
conn system/manager
connect system/manager
conn system/manager as sysdba
===========創(chuàng)建用戶(必須是有創(chuàng)建用戶權限的登陸者,才能創(chuàng)建)
create user xiaoming identified by m123;
===========更改密碼(在oracle自帶的SQL Plus下使用)--回車后,根據(jù)提示做
password xiaoming;
passw
===========刪除用戶
--單一刪除用戶
drop user xiaoming
--連帶把用戶建的表也刪除
drop user xiaoming cascade
===========授權用戶(connect普通,dba最大,resource表空間建表)
grant connect to xiaoming
grant dba to xiaoming
grant resource to xiaoming
===========授權查詢其他表(在sys,system,emp表所屬人可以授權)
grant select on emp to xiaoming授予查詢權限
..... update .. ... .. ........授予更新權限
..... all? ? .. ... .. ........授予所有權限
===========對象授權(讓下一用戶,可以繼續(xù)傳遞這種權限給其他人)
grant select on emp to xiaoming with grant option
===========系統(tǒng)授權(對象和系統(tǒng)有區(qū)別一個是grant一個是admin)
system給xiaoming授權時:
grant connect to xiaoming with admin option;
===========收回權限(在sys,system,emp表所屬人可以授權)
revoke select on emp to xiaoming授予查詢權限
.....? update .. ... .. ........授予更新權限
.....? all? ? .. ... .. ........授予所有權限
============賬戶鎖定
--設定文件
create profile lock_account
limit failed_login_attempts 3 password_lock_time 2;
--把鎖賦給用戶
alter user xiaoming profile lock_account
--把用戶解鎖
alter user xiaoming account unlock
===========終止口令(設定用戶每10天延遲兩天必須改密碼)
--設定文件
create profile myprofile
limit password_life_time 10
password_grace_time 2;
--口令賦給用戶
alter user xiaoming profile myprofile
===========口令歷史(指定時間內必須更改密碼,而且不能和以前一樣)
--設定文件
create profile password_history limit
password_life_time 10 password_grace_time 2
password_reuse_time 10
--口令賦給用戶
alter user xiaoming profile password_history
===========刪除設定文件
drop profile filename
===========查詢非本用戶下的授權表
select * from scott.emp;
===========看表結構
desc test
*********************************
表的管理
*********************************
============字符型
char 定長 最大2000字符
char(10) 那么用不用10個都是占10個,但是查詢時候快,適合做身份證
varchar2(20) 用多少空間算多少空間
============數(shù)字類型
number
-10的38次方到10的38次方
number(5,2)表示一個小數(shù)的5位整數(shù),2位小數(shù)
-999.99---999.9
number(5)
-99999 - 99999
=============日期類型
date???? ? 包括年月日時分秒
timestamp? 更加精確的
=============圖片、聲音、視頻
blob???? ? 二進制數(shù)據(jù)? 可以存放圖片/聲音 4G
***********************************************
=============創(chuàng)建表(有resource權限才能創(chuàng)建表)
create table test(userId varchar2(30),userName varchar2(30))
=============添加一個字段
alter table student add(classid number(2));
=============修改字段長度
alter table student modify (xm varchar2(30));
=============修改字段類型或者名字
alter table student modify (xm char(30));
=============刪除一個字段
alter table student drop column sal;
=============修改表的名字
rename student to stu;
=============刪除表
drop table student;
=============插入數(shù)據(jù)(時間類型需注意)
insert into student values(1,'小明','男','11-12月-1997',2453.2,14);
=============更改日期格式
alter session set nls_date_format='yyyy-mm-dd';
--也可以插入的時候設置
('13-03月-1988')
to_date('1988-12-12','yyyy-mm-dd')
to_date('1989/12/12','yyyy/mm/dd')
=============指定字段插入
insert into student(studentNumber,studentName,sex) values(123,'紅紅',女);
?
**********************
增刪查改控制
**********************
=============瘋狂復制插入
insert into student(studentNumber,studentName,sex) select * from student;
=============插入多行數(shù)據(jù)
insert into kkk (Myid,myname,mydept) select empno,ename,deptno from emp where deptno = 10;
=============刪除數(shù)據(jù)
--刪除所有記錄,表結構還在,寫日志,可以回復,速度慢
delete from student;
--刪除表的結構和數(shù)據(jù)
drop table student;
--刪除一條記錄
delete from student where xh='1001';
--刪除表中的所有記錄,表結構還在,不寫日志,無法找回刪除的記錄,速度快。
truncate table student;
==============改一個字段
update student set sex='女' where xh='1001';
==============子查詢,多條件,更新
update emp set (job,sal,comm)=(select job,sal,comm from emp where ename='SMITH') where ename='SCOTT';
==============修改多個字段
update student set sex='男',birthday='1989-04-01'
where xh='1001';
=============查詢?yōu)榭张c不為空
select * from student where birthday is null;
select * from student where birthday is not null;
=============查詢指定列
select ename,sal,job from emp;
=============條件查詢(where)
select ename,sal,job from emp where ename='SMITH';
=============條件查詢(where,<)
select ename,hiredate from emp where hiredate>'1-1月-1982';
=============條件查詢(where,and,< = >)
select ename,sal from emp where sal>=2000 and sal<=2500;
=============條件查詢(like % _)..一個下劃線代表一個字符
select ename,sal from emp where ename like '__O%';
=============條件查詢(like %)
select ename,sal from emp where ename like 'S%';
=============條件查詢(or in)..in可以代替or使用
select * from emp where empno=123 or empno=456 or...;
select * from emp where empno in(123,456,...);
=============條件查詢(null,not null)..空查詢
select * from emp where mgr is null;
select * from emp where mgr is not null;
=============條件查詢(or and)
select * from emp where (sal > 500 or job = 'MANAGER') and ename like 'J%';
=============條件查詢(order by)按照 什么什么排序 順序排列
select * from emp order by sal;
=============條件查詢(order by desc)倒敘
select * from emp order by sal desc;
=============條件查詢(order by asc desc)一個升序一個降序
select * from emp order by deptno asc,sal desc;
=============條件查詢(order by as)多字段 as 別名 按照別名排序
select ename,(sal+nvl(comm,0))*13 as "年薪" from emp order by "年薪";
=============條件查詢(數(shù)據(jù)分組max min avg sum count)
select max(sal),min(sal) from emp;
=============條件查詢(max min 子查詢)
select ename,sal from emp where sal=(select max(sal) from emp);
select ename,sal from emp where sal=(select max(sal) from emp) or sal = (select min(sal) from emp);
=============條件查詢(單表 單行子查詢 =)
select * from emp where deptno = (select deptno from emp where ename = 'SMITH');
=============條件查詢(單表 多行子查詢 in)
select * from emp where job in (select distinct job from emp where deptno = 10);
=============條件查詢(單表 多行子查詢 all)大于all大于全部
select ename,sal,deptno from emp where sal > all(select sal from emp where deptno = 30);
=============條件查詢(單表 多行子查詢any)大于any 值大于期中一個就可以了
select ename,sal,deptno from emp where sal > any(select sal from emp where deptno = 30);
=============條件查詢(單表 多行 多列查詢)
select * from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH');
=============條件查詢(單表 多行 max)
select ename,sal,deptno from emp where sal > (select max(sal) from emp where deptno = 30);
=============條件查詢(from 里面使用子查詢)內嵌試圖
select a1.ename,a1.sal,a1.deptno,a2.mysal
from emp a1,(select deptno,avg(sal) mysql from emp) group by deptno) a2
where a1.deptno = a2.deptno and a1.sal > a2.mysal;
=============條件查詢(三種分頁之 rownum)
--查詢視圖
select * from emp;
--第一次分頁(分尾)
select a1.*,rownum rn from (select * from emp) a1 where rownum <=10;
--第二次分頁(分頭)
select * from (select a1.*,rownum rn from (selct * from emp) a1 where rownum <= 10) where rn >= 5;
select * from (select a1.*,rownum rn from emp a1 where rownum < 10) where rn >=5;
select a2.* from (select a1.*,rownum rn from emp a1 where rownum < 10) a2 where rn >=5;
=============條件查詢(三種分頁之 rowid 分頁)
select * from t_xiaoxi where rowid in(select rid from (select fownum rn,rid from(select rowid rid,cid from
t_xiaoxi order by cid desc) where rownum < 10000) where rn > 9980) roder by cid desc;
=============條件查詢(合并查詢union,union all,intersect,minus)
select ename,sal,job from emp where sal > 2500 union
select ename,sal,job from emp where job='manager';
=============條件查詢(查詢結果,創(chuàng)建表)
create table myemp2 (id,ename) as select empno,ename from emp;
=============條件查詢(avg)平均查詢
select avg(sal) from emp;
=============條件查詢(avg 子查詢)
select * from emp where sal > (select avg(sal) from emp);
=============條件查詢(group by)【 group by, having, order by】使用順序
--如果下面的deptno不是在后面group by中出現(xiàn),那么就得刪了它,因為它和前面的函數(shù)不屬于同一列
select avg(sal),max(sal),deptno from emp group by deptno;
select avg(sal),max(sal),deptno,jop from emp group by deptno,job;
=============條件查詢(having)having 對分組進行篩選
select avg(sal),max(sal),deptno from emp group by deptno having avg(avg) > 2000;
=============條件查詢(多表查詢)
--查詢兩個表 建立= 以作鏈接
select e.ename,e.sal,d.dname from emp e,dept d where e.deptno = d.deptno;
=============條件查詢(多表 and)
select e.ename,e.sal,d.name from emp e,dept d where e.deptno = d.deptno and d.deptno = 10;
=============條件查詢(多表 between)between 在什么范圍內
select a1.ename,a1.sal,a2.grade from emp a1,salgrade a2 where a1.sal between a2.losal and a2.hisal;
=============條件查詢(多表 order by)
select a1.ename,a2.dname,a1.sal from emp a1,empt a2 where a1.deptno = a2.deptno order by a1.deptno;
=============條件查詢(一表 多用 and)
select worker.ename,boss.ename from emp worker,emp boss where worker.mgr = boss.empno;
select worker.ename,boss.ename from emp worker,emp boss where worker.mgr = boss.empno and worker.ename='FORD'
=============算數(shù)查詢
select sal*13,ename from emp;
=============查詢取別名
select sal*13 "年工資" ,ename from emp;
=============查詢nvl處理null函數(shù)
select sal*13+nvl(comm,0)*13 "年工資",ename,comm from emp;
=============刪除重復的
select distinct deptno,job from emp;
**********************
sql函數(shù)
**********************
==============字符函數(shù)
--將字符串轉化為小寫
lower(char)
select lower(ename) from emp;
--將字符串轉換為大寫
upper(char)
select upper(ename) from emp;
--返回字符串的長度
length(char)
select * from emp where length(ename)=5;
--取字符串的字串(從1開始取 取3個字符)
substr(char,m,n)
select substr(ename,1,3) from emp;
--替換函數(shù)
replace(char1,search_string,replace_string)
select replace(ename,'A','老鼠') from emp;
--找字串的位置
instr(char1,char2,[,n[,m]])
--合并字符串(||合并符號)
?select upper(substr(ename,1,1)) ||lower(substr(ename,2,length(ename)-1)) from emp;
==============數(shù)學函數(shù)
包括cos,cosh,exp,ln,log,sin,sinh,sqrt,tan,tanh,acos,asin,atan,round
--round(n,[m])四舍五入
select round(sal) from emp;
select round(comm,1),comm from emp;
意思:55.7 55.66
--trunc(n,[m])截取數(shù)字
select trunc(comm,1),comm from emp;
意思:55.6 55.66
--mod(m,n)取摸
select sal(10,2) from emp;
--floor(n)返回小于或是等于n的最大整數(shù)
select floor(comm),comm from emp;
--ceil(n)返回大于或是等于n的最小正數(shù)
select ceil(comm),comm from emp;
--abs(n) 返回數(shù)字n的絕對值
--select abs(-13) from dual;
--acos(n) 返回數(shù)字的反余旋
--atan(n) 返回數(shù)字反正切
--cos(n)
--exp(n)? 返回e的n次冪
--log(m,n) 返回對數(shù)值
--power(m,n)返回m的n次冪
==============日期函數(shù)
--oracle默認日期格式
dd-mon-yy 既12-7月-1988
--sysdate返回系統(tǒng)時間
select sysdate from emp;
--hirdate(xx,8)加上8個月
select * from emp where sysdate > add_months(hiredate,8);
select trunc(sysdate-hiredate) "入職天數(shù)",ename from emp;
--last_day(hiredate)返回本月的最后一天
select hiredate,last_day(hiredate) from emp;
select hiredate,ename from emp where last_day(hiredate)-2=hiredate;
==============轉換函數(shù)(類型之間自動轉換)
--例子
create table t1(id int);
insert into t1 values('10')
create table t2(id varchar2(10));
insert into t2 values(1);
--to_char(hiredate,"YYYY-mm-dd hh24:mi:ss")
select ename,to_char(hiredate,'YYYY-mm-dd hh24:mi:ss') from emp;
--to_char(sal,"L99999.99")
select ename,to_char(hiredate,'YYYY-mm-dd hh24:mi:ss'),to_char(sal,'L99999.99') from emp;
找出1980入職的
select * from emp where to_char(hiredate,'YYYY') = 1980;
----------------------------
yy:兩位數(shù)字的年份 2004--04?
yyyy:四位數(shù)字的年份 2004年?
mm:兩位數(shù)字的年份????????? ?
dd:2位數(shù)字的天 30-->30?? ?
hh24:8點 --》20????????? ?
hh12:8點 --》08????????? ?
mi,ss --》顯示分鐘\秒 ?
<---------------------------->
9:顯示數(shù)字,并忽略前面0
0:顯示數(shù)字,位數(shù)不足用0補充
.:在指定位置顯示小數(shù)點
,:在指定位置顯示逗號
$:在數(shù)字前面加美元*to_char(sal,'$99,999.99')*?
L:本地的字符集符號
C:國際貨幣符號
G:在指定位置顯示組分隔符
D:在指定位置顯示小數(shù)點符號
<---------------------------->
==============sys_context('userenv','db_name')
1) terminal:當前回話客戶所對應的中斷標識符
2) lanuage:語言
3) db_name:當前數(shù)據(jù)庫名稱
4) nls_date_format:當前回話客戶所對應的日期格式
5) session_user:當前回話客戶所對應的數(shù)據(jù)庫用戶名
6) current_schema:當前回話客戶所對應默認方案名
7) host:返回數(shù)據(jù)庫所在主機的名稱
查詢所使用的是哪個數(shù)據(jù)庫
select sys_context('userenv','db_name') from dual;
**********************
事務管理
**********************
=============提交事務(讓事務提交,也就是刪了保存點)
commit
=============建立保存點
savepoint a1;
savepoint a2;
=============返回事務
--返回第一個保存點
rollback to a1;
--返回第二個保存點
rollback to a2;
--返回所有設置的保存點
rollback;
==============只讀事務
set transaction read only;
**********************
數(shù)據(jù)庫管理
**********************
=========登錄方式
sys 必須以sysdba 或者 sysoper形式登錄 不能以normal方式登錄數(shù)據(jù)庫
system 可以以normal方式登錄
=========關閉數(shù)據(jù)庫啟動數(shù)據(jù)庫
--關閉
shutdown;
--開啟
startup;
=========顯示參數(shù)
show parameter;
=========備份和恢復(導出導入)oracle->bin->exp.exe(并把此文件路徑復制給dos下執(zhí)行)
--邏輯備份(open下)
導出自己的一張表
exp
userid=scott/tigger@orcl tables(emp) file=d:\e1.demp
導出自己的多張表
exp
userid=scott/tigger@orcl tables(emp,dept) file=d:\e1.demp
導出其他方案的表
exp_full_database的權限,比如system就可以導出scott的表
exp
userid=system/manager@orcl tables=(scott.emp)? file=d:\e1.demp
導出表結構(無數(shù)據(jù)只有結構)
exp
userid=scott/tigger@orcl tables=(emp)? file=d:\e1.demp rows = n
直接導出方式(常用作處理大表)
exp
userid=scott/tigger@orcl talbes=(emp) file=d:\e1.demp direct = y
導出方案
exp
userid=scott/tigger@orcl owner=scott file=d:\e1.demp
導出其他方案
exp
userid=system/manager@orcl owner(system,scott) file=d:\e1.demp
--物理備份(任何下)
<------------------------->
導出分為:導出表,導出方案,導出數(shù)據(jù)庫三種方式
導出exp命令,常用選項
userid:用于指定執(zhí)行導出操作的用戶名,口令,連接字符串
tables:用于指定執(zhí)行導出操作的表
owner: 用于指定執(zhí)行導出操作的方案
full=y:用于指定執(zhí)行導出操作的數(shù)據(jù)庫
inctype:用于指定執(zhí)行導出操作的增量類型
rows:用于指定執(zhí)行導出操作是否要導出表中的數(shù)據(jù)
file:用于指定導出文件名
<------------------------->
**********************
數(shù)據(jù)恢復
**********************
=========1:建立回滾點
savepoint aa;
=========2:刪除數(shù)據(jù)(可回復刪除)
delete from student;
=========3:恢復數(shù)據(jù)
rollback to aa;
=========自增長歸零
alter table onehref auto_increment = 0;
**********************
java操作oracle數(shù)據(jù)庫
**********************
=========jdbc_odbc連接
--第一步
控制面板->管理工具->數(shù)據(jù)源ODBC->添加Oracle in OraDb10_home1->完成->OK->確定
--第二部java代碼
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection ct = DriverManager.getConnection("jdbc:odbc:test","scott","tigger");
=========jdbc連接
--第一步
加載jar包
--第二步
import java.sql.*;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection ct = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tigger")
=========java里面控制事務
--設置不能默認提交(默認不讓執(zhí)行一句提交一句)
ct.setAutoCommit(false);
--提交事務(執(zhí)行完sql語句提交)
ct.commit();
--異常回滾(在發(fā)生異常里面寫)
ct.rollback();