用戶和權限session |
|||||||||
1建立用戶oracle 管理員纔有權限建立用戶 sys/system編輯器
|
--管理員登陸ide conn sys/oracle@orcl as sysdba;函數 --建立用戶方案---必須管理員身份才能操做 spa create user username identified by password;命令行 create user wangwu identified by w123;code --刪除用戶方案對象 drop user username cascade;開發 ---修改密碼 alter user username identified by newpassword; |
||||||||
2 用戶解鎖上鎖 |
--用戶解鎖和上鎖---必須管理員身份才能操做 alter user username account lock; alter user scott account lock; alter user username account unlock; alter user scott account unlock; |
||||||||
3受權 通常開發只要給用戶授予connect 和resource 角色就夠了
|
3個默認 角色
|
||||||||
4回收權限 |
---回收權限(要在管理員身份下才能回收) revoke privilegename from username; revoke connect from wangwu; |
||||||||
5 權限傳遞 |
--受權(對象權限) grant select on scott.emp to ken; grant update on scott.emp to wangwu; --回收權限(對象權限) revoke select on scott.emp from ken; revoke update on scott.emp from wangwu;
---權限的傳遞 with admin option傳遞系統權限 with grant option 傳遞對象權限 create user userone identified by one; create user usertwo identified by two; grant create session to userone with admin option; --with admin option 在userone下面能夠 將 create session 系統權限受權給其餘用戶 grant select on scott.emp to userone with grant option ; --with grant option 在userone下面能夠 將 select 對象權限受權給其餘用戶 |
||||||||
6 規則
|
---規則 profile create profile notLoginDay limit failed_login_attempts 3 password_lock_time 100; --添加規則 alter user scott profile notLoginDay; |
||||||||
7字符函數 |
/*字符函數*/ --當沒有表能夠用個的時候oracle自帶一個虛表dual -- || 表示鏈接符號 將字符串鏈接到一塊兒 Lower(char):將字符串轉化爲小寫格式 ?將全部員工的名字按小寫的方式顯示 select lower(ename),lower(job) from emp; Upper(char):將字符轉化爲大寫的格式 select upper('abcd') from dual; Length(char):返回字符串的長度 ?顯示正好爲5個字符員工的姓名 select * from emp where length(ename)=5; Substr(char,m,n):取字符串的子串 m 表示開始的位置 n 字符個數 ?顯示全部員工姓名的前三個字符 select ename ,substr(ename,1,3) from emp; Trim,Ltrim,Rtrim:去掉空格 select '=='|| trim(' abc ') ||'==' from dual; ?以首字母大寫的方式顯示全部員工 select ename, substr(ename,0,1)|| lower( substr(ename,2,length(ename)-1) ) from emp; Replace (char1,search_string,replace_string) ? 顯示全部的員工姓名將A替換成「我是A」 select ename, replace(ename,'A','我是A')as result from emp; Instr(char1,char2,[,n[,m]])取字符串的位置 n 開始位置 m 第幾個char2 ? 從第二個字符開始找到員工姓名中A所在的位置 select ename, Instr(ename,'A',1,2) from emp; |
||||||||
8數學函數 |
/*數學函數*/ Round(n,[m]) 四捨五入,若是省掉m,則四捨五入到整數,若是m是整數,則四捨五入到小數點的m位置後,若是是m負數,則 四捨五入到小數點m的位前 ? 73.2564 四捨五入到小數點後2位 select round(73.2564,2) from dual; trunc(n,[m]) 用戶截取數字,若是省略m,就截去小數部分,若是m是整數就截取到小數點後m位後,若是m是負數,則截取到小數點的前m位 ? 73.2564 截取到小數點後3位 select trunc(73.2564,3) from dual; Mod(n,[m])取模 ? 8%3=? select mod(8,3) from dual; Floor(n) 向下取整 ? 7.5846 向下取整 Ceil(n) 向上取整 ?7.5846 向上取整 select floor(7.1289) ,Ceil(7.1289) from dual; |
||||||||
9/*日期函數*/ |
1 sysdate :返回系統時間 select sysdate from dual; 2 current_date 返回當前時間 select current_date from dual; 3 Next_day(date,week) 給定時間的下一個星期是幾號 /*week 數字和英文*/ select next_day(sysdate,'MONDAY') from dual; 4 add_months(d,n):返回在時間d上加n個月的時間 select add_months( sysdate,12 ) from dual; 5 last_day(d):返回指定日期所在月份的最後一天 select last_day( sysdate) from dual; ?查找已經入職362個月多的員工 ?顯示滿30年份服務年限的員工的姓名和受僱日期 ?每一個員工加入公司的天數 ?下個星期一是多少號 ?找出各月倒數第三天受僱的全部員工 |
||||||||
轉換函數
|
/* 轉換函數 時間---->字符 字符---->數字 字符---->時間 */ /*to_char( 時間,格式)*/ ---------------------時間格式---------------------- 格式: yyyy:顯示年份 yy :顯示年份後2位 mm:月份 dd:號 hh /hh24:小時 mi分 ss秒 select to_char(sysdate,'yy-mm-dd hh24:mi:ss') from dual; -----------------貨幣格式-------------------- 9 :顯示數字 0 :不足則補0 . :顯示小數點 ,; 顯示分隔符 $ : 顯示美圓符號 L : 顯示本地貨幣 C : 顯示國際貨幣符號 G : 制定位置顯示分割符號 D : 制定位置顯示小數點符號 ?薪水指訂貨幣符號
8756984.555 $8,756,984.555 $9,999,999.99 L9,999,999.99 select to_char(8756984.5,'L9,999,999.99') from dual; select to_char(8756984.5,'$9G999G999D99') from dual; ---------------字符轉日期 to_date('2010-10-10','yyyy-mm-dd')------------------------- select to_date('2017-2-27','yyyy-mm-dd') from dual; insert into emp values( 9900, 'ZHANGSAN','CLERK',7698,to_date('2010-10-10','yyyy-mm-dd'),2500.00 ,0.00,30); ---------------字符轉數字 To_number --------------------------------------------------- select to_number('001')*8 from dual; |
||||||||
|
-------------------------彙集函數 count,sum,avg,max,min -------------------------------------------------- ---count ---統計總行數 select count(*) from emp; select count(empno) from emp; --統計員工總人數 ---sum-----統計總和 select sum(sal) as total from emp; --統計全部人員的工資總和 --avg------統計平均數 select avg(sal) from emp; --統計全部人員的工資平均值 --max--最大值---min最小值 select max(sal) as maxSal ,min(sal) as minSal from emp; ---聚合函數不能放到where 做爲條件 select * from emp where sal avg(sal)--錯; select * from emp where sal>(select avg(sal) from emp);---對 --顯示當前用戶- select user from dual; ---decode 布爾判斷函數 ------ ---查詢出CLERK 的人員數量 select sum( decode(job,'CLERK',1,0) ) from emp; ---查詢各個工種有多少人 select sum( decode(job,'CLERK',1,0) ) as CLERK, sum( decode(job,'SALESMAN',1,0) ) as SALESMAN, sum( decode(job,'MANAGER',1,0) ) as MANAGER, sum( decode(job,'ANALYST',1,0) ) as ANALYST, sum( decode(job,'PRESIDENT',1,0) ) as PRESIDENT,count(*) as total from emp; |
||||||||
-nvl(column,default) 非空 |
----------nvl(column,default) 非空-------------------------------------- ---------------------emp表中全部員工在元獎金的基礎上增長50元獎金 update emp set comm=nvl(comm ,0 ) +50 ; |
||||||||
數據導出備份和導入 |
--------------------------數據導出備份和導入 -------------------------注意 導出和導入 必須是CMD 命令行下操做,而不是SQL編輯器中 ---導出表 exp 導出關鍵字 userid用戶權限 file 保存的位置 tables 表 exp userid=scott/tiger@orcl tables=(dept,emp) file=c:/scottTables.dmp --導出方案 exp userid=scott/tiger@orcl owner=scott file=d:/scott.dmp --管理員幫忙導出scott方案 exp userid=sys/oracle@orcl owner=scott file=d:/scott.dmp --導入表 imp導入關鍵字 imp userid=scott/tiger@orcl file=c:/scottTables.dmp --導入方案 imp userid=scott/tiger@orcl file=d:/scott.dmp --管理員導入 管理員導出過的方案 imp userid=sys/oracle@orcl file=d:/scott.dmp fromuser=sys touser=scott |