- 查看當(dāng)前數(shù)據(jù)庫的名字
select name from v$database
2.查看當(dāng)前數(shù)據(jù)庫實(shí)例名
select instance_name from v$instance
show parameters instance_name;
一個(gè)數(shù)據(jù)庫可以有多個(gè)實(shí)例矫废,在做數(shù)據(jù)庫集群的時(shí)候可以用到
3.查看表空間的名字
select tablespace_name from dba_data_files
4.查看表空間里面有哪些表
select table_name from dba_tables where tablespace_name='USERS'
- 查看當(dāng)前數(shù)據(jù)庫的所有用戶
select * from dba_users
6.查看表屬于哪個(gè)表空間
select table_name,tablespace_name from all_tables where table_name='C'
7.查詢用戶有哪些操作權(quán)限
select * from user_sys_privs
8.oracle數(shù)據(jù)庫的三范式
數(shù)據(jù)庫的三范式
第一范式:不可能有重復(fù)的列缝呕,即存在的列不可再拆分
第二范式:非主屬性完全依賴于主關(guān)鍵字
即存在組合關(guān)鍵字中的字段決定非關(guān)鍵字的情況。
不能存在兩個(gè)主關(guān)鍵字來唯一標(biāo)識(shí)該實(shí)體
第三范式:屬性不依賴于其它非主屬性
不存在關(guān)鍵字段 → 非關(guān)鍵字段x → 非關(guān)鍵字段y 如部門表中存在 部門編號(hào)暑始,部門名稱廓握,部門位置
如果emp表中存在部門編號(hào)也就是說員工表中不能再有部門名稱和部門位置了
9.union union all intersect 和minus的區(qū)別
union兩個(gè)結(jié)果集的并集芋忿,會(huì)去除掉重復(fù),默認(rèn)會(huì)按查詢結(jié)果的第一列升序排列疾棵。
union all 兩個(gè)結(jié)果的并集不會(huì)去除重復(fù)戈钢,無序排列
intersect兩個(gè)結(jié)果的交集,如表s1和s2這個(gè)就表示在s1和s2中公共的數(shù)據(jù)
minus兩個(gè)結(jié)果集的差集是尔,如表s1和s2這個(gè)表示在s1中的數(shù)據(jù)但是在s2中不存在
10.nvl和nvl2的區(qū)別
nvl(expr1殉了,expr2)expr1為null時(shí)結(jié)果為expr2,值不為null時(shí)拟枚,結(jié)果為expr1
nvl2(expr1薪铜,expr2,expr3)expr1為null時(shí)結(jié)果為expr3,不為null時(shí)結(jié)果為expr2
11.oracle中常用的函數(shù)
length(字段名)查找字段的長(zhǎng)度 concat(a,b)或者a||b 把a(bǔ)和b連接起來
round(n)四舍五入保留到整數(shù)位 round(n,1)保留一位小數(shù) round(n恩溅,-1)保留整數(shù)位的第二位
instr(str,index)返回index在str中的位置隔箍,默認(rèn)從1開始 initcap(str)首字母大寫
lower(str)首字母小寫 substr(str,start,count截取字符串,從某個(gè)位置開始脚乡,count表示從某個(gè)位置開始截取幾位 extract(year from sysdate) 截取當(dāng)前時(shí)間的年份 extract(month from sysdate)
replace(str,a,b)用b來替換str中的a rpad(字段名蜒滩,長(zhǎng)度,擴(kuò)充值)有填充奶稠,如果該字段長(zhǎng)度小于規(guī)定長(zhǎng)度俯艰,就用擴(kuò)充值在右邊填充,rpad(str,7,'0') 如果str的長(zhǎng)度小于7锌订,就在str的后面補(bǔ)充0直到長(zhǎng)度為7,lpad左填充同理竹握。
ceil(15.6)向上取整,結(jié)果為16. ceil(-15.6)結(jié)果為-15
floor(15.6)向下取整辆飘,結(jié)果為15 floor(-15.6)結(jié)果為-16
mod(m,n)取余啦辐,mod(10,3)結(jié)果為1 power(n,p) 求n的p次冪 exp(n)一個(gè)數(shù)字的n次方根
trunc和round類似,只是trunc不會(huì)四舍五入 如trunc(15.6)結(jié)果為15蜈项,trunc(15.67,1)結(jié)果為15.6
to_char(日期,'yyyy-MM-dd') 日期轉(zhuǎn)換為字符串 to_number(str)字符串轉(zhuǎn)為數(shù)字芹关,字符串必須是數(shù)字字符串
to_date(str,'yyyy-MM-dd')字符串轉(zhuǎn)換為日期
sum()求和 ,count()計(jì)數(shù) avg()求平均數(shù) max()求最大值 min()求最小值
12.數(shù)據(jù)的導(dǎo)入和導(dǎo)出用來備份數(shù)據(jù)
exp scott/orcl file=G:\temps.dmp owner=scott consistent=y direct=y scott:用戶名 orcl:密碼 owner:用戶名
imp nhsr/nhsr file=G:\temp.dmp ignore=y fromuser=scott touser=nhsr nhsr:要導(dǎo)入數(shù)據(jù)用戶名 nhsr:要導(dǎo)入數(shù)據(jù)的密碼
fromuser:數(shù)據(jù)來源用戶 touser:要導(dǎo)入數(shù)據(jù)用戶名
13.oracle中case when 和decode函數(shù)的使用
兩者都是判斷條件函數(shù)战得,decode(字段名充边,條件1庸推,值1常侦,條件2浇冰,值2....)當(dāng)字段值為條件1時(shí),值為值1聋亡,當(dāng)字段值為條件2時(shí)肘习,值為值2,每個(gè)判斷條件對(duì)應(yīng)一個(gè)值坡倔,最后如果沒匹配成對(duì)的條件和值漂佩,結(jié)果相當(dāng)于else。和if else if,else一樣道理
select case when 條件1 then 值1
when 條件2 then 值2
.....else 值n
end 別名 from 表
14.數(shù)據(jù)庫死鎖查詢以及kill進(jìn)程語句
--(1)查看死鎖對(duì)象
select s.username,l.object_id,l.session_id,s.SERIAL#,s.machine,s.STATUS from vlocked_objectl
where s.sid = l.session_id;
--(2)查看具體操作對(duì)象
select sql_text from vsession where sid in(select session_id from v$locked_object )
);
--(3)死鎖處理語句(殺掉會(huì)話) 其中數(shù)字代表session_id,serial#
alter system kill session '393,63862
15.oralce創(chuàng)建用戶一系列命令
create tablespace cat_data datafile 'J:/arms/cat_data.dbf' size 2048m autoextend on next 50m maxsize unlimited;創(chuàng)建表空間
win+r cmd 輸入 sqlplus \ as sysdba 登錄dba用戶 罪塔;conn可以切換用戶
create user 用戶名 identified by default tablespace密碼 創(chuàng)建用戶并指定默認(rèn)的工作空間
grant connect,resource,dba,create session to 用戶名 授予用戶權(quán)限
alter user 用戶名 identified by 密碼 修改用戶名和密碼投蝉;
alter user 用戶名 account unlock 解鎖用戶名
16、分組查詢的兩種寫法:
select case
when degrees>60 and degrees<70 then '4'
when degrees>70 and degrees<80 then '3'
when degrees>80 and degrees<90 then '2'
when degrees>90 then '1'
else null end as 分?jǐn)?shù)段,
count(*) from score
group by
case
when degrees>60 and degrees<70 then '4'
when degrees>70 and degrees<80 then '3'
when degrees>80 and degrees<90 then '2'
when degrees>90 then '1'
else null end
select sum(case when degrees>60 and degrees<70 then 1 else 0 end) as "60<degrees<70",
sum(case when degrees>70 and degrees<80 then 1 else 0 end) as "70<degrees<80",
sum(case when degrees>80 and degrees<90 then 1 else 0 end) as "80<degrees<90",
sum(case when degrees>90 then 1 else 0 end) as "90<degrees"
from score