plsql筆記

查詢:

select employee_name from employees;

select employee_name from employees where employee_age > 25;

select e.employee_name, s.salary from employees e, salary s where e.employee_id = ?s.employee_id;

select?distinct?e.employee_name, s.salary from employees e, salary s where e.employee_id = ?s.employee_id;

distinct關(guān)鍵字用于獲取結(jié)果集中列e.employee_name 與 s.salary的唯一性組合(distinct相當于去重

select?e.employee_id,?e.employee_name,sum(s.salary) ?total_salary?from employees e, salary s where e.employee_id = ?s.employee_id?group by e.employee_id, e.employee_name;

select?e.employee_id,?e.employee_name,?sum(s.salary) ?total_salary?from employees e, salary s where e.employee_id = ?s.employee_id?group by e.employee_id, e.employee_name?having (sum(s.salary)) > 10000;

select distinct e.employee_name, s.salary from employees e, salary s where e.employee_id = s.employee_id?order by s.salary desc;

select?e.employee_id,?e.employee_name,?sum(s.salary) ?total_salary?from employees e, salary s where e.employee_id = ?s.employee_id?group by e.employee_id, e.employee_name?order by total_salary desc;

order by 子句與distinct關(guān)鍵字同時使用時,也必須遵循一個規(guī)則--------order by 子句所指定的排序列必須出現(xiàn)在select表達式中。

select * from employees where employee_id in (select employee_id from salary);子查詢

聯(lián)合語句:union(并集且去重),union all(并集不去重掀宋,效率高) , intersect(交集), minus(差集)

聯(lián)合語句可以進行混合運算,優(yōu)先級相同帅矗,依次運行。

select student_id, student_name, student_age from a_students?union?select student_id, student_name, student_age from b_students;

兩個查詢的列數(shù)且數(shù)據(jù)類型必須相同煞烫。

minus是集合間的減法運算,該運算返回第一個集合中存在的累颂,而第二個集合中不存在的記錄滞详。

連接:

自然連接natural join:原則,兩個數(shù)據(jù)源的共有列紊馏,且具有相同的列值料饥。

select * from employees?natural join?salary.

不能為公共列employee_id添加限定詞,即不能寫成employees.employee_id朱监。

對于自然連接外的其他列可以使用限定詞岸啡。

例如:

select employee_id, e.employee_name, s.month, s.salary from employees e?natural join?salary s ;

內(nèi)連接inner join:

select e.employee_id, e.employee_name, s.month, s.salary from employees e ?(inner/可省略) join?salary s?on?e.employee_id = s.employee_id

on為指定搜索條件。

此舉與where結(jié)果相同赫编,當設(shè)計表比較多時使用join比較好巡蘸。

select * from employees join salary on ... join company on ... join sales on ...;

外連接left join和right join:

左連接即以左表為基礎(chǔ),右連接即以右表為基礎(chǔ)擂送。

select e.employee_id, e.employee_name, s.month, s.salary from employees e?left?join?salary s?on?e.employee_id = s.employee_id

本表以employees為基礎(chǔ)悦荒,將salary表連接到本表,這樣有些沒有領(lǐng)工資的員工也會出現(xiàn)子啊結(jié)果中嘹吨。

簡寫:

select e.employee_id, e.employee_name, s.month, s.salary from employees e,salary s?where?e.employee_id = s.employee_id(+)

s.employee_id(+)即表示salary為附屬表搬味,當多個數(shù)據(jù)源連接是最好不使用簡寫。

完全連接full join:是一個左連接和一個右連接的組合,最后去除重復(fù)記錄碰纬。結(jié)果與兩個數(shù)據(jù)源順序無關(guān)萍聊。

select e.employee_id, e.employee_name, s.month, s.salary from employees e ?full join?salary s?where?e.employee_id = s.employee_id

層次化查詢(遍歷樹):

select market_id, market_name from market?start with?market_name = '亞洲'connect by prior?market_id = parent_market_id;

start_with為起始條件,connect by 指定如何遞歸下一條記錄悦析,且遵循深度優(yōu)先搜索策略寿桨,prior指前一條記錄。prior?market_id即為前一條記錄的market_id 等于下一條記錄的parent_market_id她按。

插入數(shù)據(jù):

insert into?students (student_id,student_name)values(1, '扎古斯那')

批量插入:

insert ?into c_students(student_id, student_name) select student_id, student_name from students where student_id < 10;

更新數(shù)據(jù):

update 表名 set 列1=“hhh”牛隅, 列2 = “1111” ?where 。酌泰。媒佣。

刪除數(shù)據(jù):

delete from students where student_id > 10;

truncate table students;

truncate table刪除全部數(shù)據(jù),且不可回滾恢復(fù)陵刹。

創(chuàng)建數(shù)據(jù)庫表:

create table?student (student_name varchar 數(shù)據(jù)類型默伍,student_age varchar 數(shù)據(jù)類型,衰琐。也糊。。羡宙。)tablespace?users.

增加字段

alter table?student?add(class_id number);

修改數(shù)據(jù)類型

alter table?student?modify(class_id varchar2(20) );

刪除已有列

alter table student?drop column class_id;

字段重命名:

alter table studentrename column student_id to id;

表空間轉(zhuǎn)移

alter table student?move tablespace users;

刪除數(shù)據(jù)庫表

drop table student;

drop table student?cascade constraints; ?cascade constraints將與本表主鍵用作外鍵的其他表的約束刪除狸剃,否則只刪除student時無法刪除。

字符處理方法:

向左補全字符lpad()函數(shù):

lpad(string, padded_length, [pad_string])

select lpad('1', 4, '0') employee_no from dual; 結(jié)果為0001狗热;

向右補全字符串-----rpad()函數(shù):

select rpad('1', 4, '*') ?employee_no from dual;結(jié)果為1***钞馁;

返回字符串的小寫-----lower()函數(shù):

select ?username, password from dba_users where?lower(username) = 'system';

返回字符串的大些形式----upper()函數(shù):

select ?username, password from dba_users where?upper(username) = 'system';

首字母大寫initcap():

select initcap ('we all like bike') new_string from dual;結(jié)果為We All Like Bike;

返回字符串長度length()函數(shù):

select length('12345') len from dual;

截取字符串substr()函數(shù):

substr(string, start_index, length)

select substr('123456789', 2, 3) sub_string from dual; 結(jié)果為234,oracle字符串下表從1開始

獲取字符串出現(xiàn)的位置---instr()函數(shù)

instr (string, sub_string, start_index, times)

select instr('123456789', '56', 2, 3) position from dual;從第2個字符搜索‘56’匿刮,直到第三次出現(xiàn)‘56’僧凰,因為沒有,所以結(jié)果為0熟丸;

刪除字符串左側(cè)空格-----ltrim()函數(shù):

select ltrim(' ? ? ? ?000') new_str from dual;

刪除字符串右側(cè)空格-----rtrim()函數(shù):

select ltrim('000 ? ? ? ?') new_str from dual;

刪除字符串兩側(cè)空格-----trim()函數(shù):

select trim(' ? ? ? 000 ? ? ? ') new_str from dual;

串聯(lián)字符串-----concat()函數(shù):

select concat('hello', ' ?word') new_str from dual;結(jié)果為hello ?word训措,concat只有兩個參數(shù),只能連接兩個字符串光羞。

翻譯字符串-----translate()函數(shù):

反轉(zhuǎn)字符串-----reverse()函數(shù):

select reverse('abcdfgh') from dual绩鸣;結(jié)果為hgfedcba

復(fù)雜數(shù)據(jù)處理

求最大值-----max()函數(shù):

select max(employee_age) max_age from employee;

求最小值-----min()函數(shù):

select min(employee_name) min_name from employees;

求平均值-----avg()函數(shù):

select avg(employee_age) avg_age from employees;

求和-----sum()函數(shù):

select sum(employee_age) sum_age from employees;

統(tǒng)計記錄數(shù)-----count()函數(shù):

統(tǒng)計單列: select count(employee_name) ?count_name, count(employee_position) ?count_position from employees;

統(tǒng)計所有列:select count(*) from employees;

利用count(1)進行統(tǒng)計:select count(1) from employees;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市纱兑,隨后出現(xiàn)的幾起案子全闷,更是在濱河造成了極大的恐慌,老刑警劉巖萍启,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件总珠,死亡現(xiàn)場離奇詭異屏鳍,居然都是意外死亡,警方通過查閱死者的電腦和手機局服,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進店門钓瞭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人淫奔,你說我怎么就攤上這事山涡。” “怎么了唆迁?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵鸭丛,是天一觀的道長。 經(jīng)常有香客問我唐责,道長鳞溉,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任鼠哥,我火速辦了婚禮熟菲,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘朴恳。我一直安慰自己抄罕,他們只是感情好,可當我...
    茶點故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布于颖。 她就那樣靜靜地躺著呆贿,像睡著了一般。 火紅的嫁衣襯著肌膚如雪森渐。 梳的紋絲不亂的頭發(fā)上做入,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天,我揣著相機與錄音章母,去河邊找鬼。 笑死翩剪,一個胖子當著我的面吹牛乳怎,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播前弯,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蚪缀,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了恕出?” 一聲冷哼從身側(cè)響起询枚,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎浙巫,沒想到半個月后金蜀,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體刷后,經(jīng)...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年渊抄,在試婚紗的時候發(fā)現(xiàn)自己被綠了尝胆。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,745評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡护桦,死狀恐怖含衔,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情二庵,我是刑警寧澤贪染,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站催享,受9級特大地震影響杭隙,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜睡陪,卻給世界環(huán)境...
    茶點故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一寺渗、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧兰迫,春花似錦信殊、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至据德,卻和暖如春鳄乏,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背棘利。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工橱野, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人善玫。 一個月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓水援,卻偏偏與公主長得像,于是被迫代替她去往敵國和親茅郎。 傳聞我的和親對象是個殘疾皇子蜗元,可洞房花燭夜當晚...
    茶點故事閱讀 44,652評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 查詢: select employee_name from employees; select employee_...
    雨一流閱讀 249評論 0 1
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法系冗,內(nèi)部類的語法奕扣,繼承相關(guān)的語法,異常的語法掌敬,線程的語...
    子非魚_t_閱讀 31,624評論 18 399
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 3,810評論 0 11
  • 目錄 簡介 在Android中存儲數(shù)據(jù)有時會用到數(shù)據(jù)庫惯豆,Android給我們提供了 一系列的API來操作數(shù)據(jù)庫池磁,非...
    慕涵盛華閱讀 1,014評論 1 2
  • 1、Check規(guī)則 Check (Agebetween15and30 )把年齡限制在15~30歲之間 2循帐、新SQL...
    姜海濤閱讀 826評論 0 4