最近學(xué)習(xí)Oracle數(shù)據(jù)庫。把學(xué)到的東西做一個(gè)總結(jié)。
本文主要講解一下幾個(gè)方面:
1.OracleXE數(shù)據(jù)庫安裝注意事項(xiàng)
2.Oracke 數(shù)據(jù)庫查詢
數(shù)據(jù)庫查詢這章主要介紹一下幾個(gè)方面:
1.全表查詢2.條件查詢3.特殊謂詞4.排序5.函數(shù)(包括一些常用的內(nèi)置函數(shù)和組函數(shù))
我使用的Oracle 是OracleXE,相對(duì)來說是比較輕量級(jí)的初烘。
OracleXE的安裝需要主要一下幾點(diǎn):
1.Oracle數(shù)據(jù)庫的安裝
注意:oracle安裝路徑需要不能含有中文或者空格香椎。
注意:oracle system用戶 密碼為123456
2.OracleXE數(shù)據(jù)庫的卸載
從新執(zhí)行安裝程序,選擇卸載項(xiàng)
3.激活測(cè)試賬號(hào)
進(jìn)入命令行窗口執(zhí)行
第一步:輸入sqlplus
輸入用戶名和密碼
第二步:鍵入 alter user hr account unlock;
如此既可以將測(cè)試賬號(hào)改為hr
密碼也為hr
Oracke 數(shù)據(jù)庫查詢:
sql語法不區(qū)分大小寫随静。
1.全表查詢
select * from 表名
select employee_id,frist_name,last_name,email....
from 表名
注意:* 進(jìn)行又有列的查詢,運(yùn)行效率偏低吗讶。可讀性差
2.查詢的定列的內(nèi)容
select 列名1燎猛,列名2,...
from 表名
3.對(duì)列的內(nèi)容進(jìn)行運(yùn)算 (+-/)
select last_name,first_name,salary*12
from employees;
4.列起別名 alias
select 列名 [as] 別名照皆,列名 [as] 別名
from employees;
注意as關(guān)鍵字可以省略
多列內(nèi)容的連接
||運(yùn)算符 可以完成2個(gè)列 或者 多個(gè)列的內(nèi)容拼接
多列內(nèi)容的連接
||運(yùn)算符 可以完成2個(gè)列 或者 多個(gè)列的內(nèi)容拼接
select first_name||last_name as name , salary
from employees;
2.條件查詢
a.比較查詢 > < = != >= <=
select * from employees where first_name = 'Lex'
注意 字符串應(yīng)用 ' '
b.邏輯運(yùn)算 and or not
select * from employees where first_name = 'Lex' and salary = 17000
select * from employees where first_name = 'steven' or salary = 17000
c.特殊謂詞
eg:查詢工資為6000 或者 工資為9000 或者 工資為24000.
一般的寫法為重绷;
select * from employees where salary = 17000 or salary = 9000 or salary = 6000
in或者not in
in 表達(dá)或者的關(guān)系
select * from employees where salary in (6000,9000,24000)
select * from employees where salary not in (6000,9000,24000)
eg:查詢工資為6000 至 12000 這個(gè)范圍內(nèi)的員工 (包含 6000,12000)
可以寫為:
select * from employees where salary >=6000 and salary<=12000
between .... and 在一個(gè)范圍 或者 區(qū)間中 (閉區(qū)間)
not between ..... and
select * from employees where salary between 6000 and 12000
select * from employees where salary not between 6000 and 12000
查詢某一些列的內(nèi)容為null 不能應(yīng)用 = null 判斷
is null is not null
select * from employees where commission_pct is not null
like 模糊查詢
eg:查詢所有姓李的老師
select * from t_teacher where name like'李%'
eg:查詢所有姓李的老師,且名字只有兩個(gè)字
select * from t_teacher where name like'李%_'
d.排序子句(order by)
select first_name,salary from employees order by salary
默認(rèn)升序asc
降序查詢:
select first_name,salary from employees order by salary desc
注意:
order by 字句必須書寫在sql語句的最后
eg:
select first_name,salary from employees where salary>6000 order by salary desc
多列排序
eg:
select first_name,salary from employees where salary>6000 order by salary desc,first_name desc
先以salary排序膜毁,如果salary相同昭卓,在以first_name 排序(按字母逐個(gè)排序)。
e:函數(shù)
1.內(nèi)置函數(shù)
sysdate 獲取當(dāng)前系統(tǒng)時(shí)間
eg:
select sysdate from dual
oracle中的虛表瘟滨,啞表 dual
獲得當(dāng)前時(shí)間 select sysdate from dual;
to_char(日期,’日期格式’)
作用:把一個(gè)日期類型候醒,轉(zhuǎn)換成一個(gè)字符串
select to_char(sysdate,'mm') from dual
yyyy:年份
mm:月份
dd:月份中的天
day:星期
hh:小時(shí)
mi:分鐘
ss:秒
eg:查詢4月份入職的員工
select *from employees where to_char(hire_date,'mm') = '04'
eg:查詢3月18日入職的員工
select *from employees where to_char(hire_date,'mm-dd') = to_char(sysdate,'mm-dd')
to_date(‘字符串’,‘日期格式’)
把字符串轉(zhuǎn)換成日期
eg:
select to_date('2016-03-18','yyyy-mm-dd') from dual
to_date函數(shù) 主要應(yīng)用在數(shù)據(jù)的插入過程杂瘸。
2.組函數(shù)
針對(duì)于一組數(shù)據(jù)的操作倒淫。默認(rèn)在不分組的情況下,會(huì)把一張表的數(shù)據(jù)劃分成一個(gè)組败玉。
a. 取最大值 max()
查詢最高的工資
select max(salary) from employees
b.取最小值 min()
查詢最低的工資
select min(salary) from employees
c.取平均值 avg()
select avg(salary) from employees
d.取數(shù)據(jù)的和 sum()
查詢員工工資之和
select sum(salary) from employees
e.取條數(shù) count()
查詢?nèi)淼臈l數(shù)
select count(*) from employees
運(yùn)行結(jié)果:統(tǒng)計(jì)非null的條數(shù)敌土。