SQL必知必會

如需要電子書,聯(lián)系我。

  • oracle只顯示前幾行

select prod_name from products where rownum <=5 ;

表的一個默認(rèn)字段

第一課創(chuàng)建表

字段后有空格许饿,數(shù)據(jù)類型需要空格來補(bǔ)全,空格是有函數(shù)可以去掉的宽档,第8課有

select * from Customers ;
select * from OrderItems  ;
select * from Orders ;
select * from Products ;
select * from Vendors ;

第二課rownum

select prod_id,prod_name,prod_price from products ;
select distinct vend_id from products;
select * from products ;
select distinct vend_id,prod_name from products;
select prod_name from products where rownum <=5 ;

第三課order by

order by 子句是一個select語句最后一個子句

order by的列可以是非檢索的列

select prod_name from products order by prod_name ;
select prod_name from products order by vend_id ;
select prod_id,prod_price,prod_name from products order by prod_price,prod_name;
select prod_id,prod_price,prod_name from products order by 2,3;
select prod_id,prod_price,prod_name from products order by prod_price desc,prod_name;

第四課where

where子句用的符號還有between\is null

將值與字符串進(jìn)行比較需要單引號图谷,與數(shù)值比較則不需要單引號

between 5 and 10 條碼需要兩個值,開始值與結(jié)束值逐哈,包括開始值與結(jié)束值

is null條碼判斷值是否為空

select prod_name,prod_price from products where prod_price = 3.49;
select prod_name,prod_price from products where prod_price < 10 ;
select prod_name,prod_price from products where prod_price <= 10 ;
select vend_id,prod_name from products where vend_id <> 'DLL01';
select prod_price,prod_name from products where prod_price between 5 and 10 ;
select cust_name from customers where cust_email is not null;

第五課高級數(shù)據(jù)過濾

or操作符只要前面的滿足后面的就不執(zhí)行了

有or又有and芬迄,and的優(yōu)先級更高,大多時候會借助括號解決

not用于否定后面的列

select prod_id,prod_price,prod_name from products where vend_id='DLL01' and prod_price <=4 ;
select prod_id,prod_price,prod_name from products where vend_id='DLL01' or vend_id='BRS01' ;
select prod_id,prod_price,prod_name from products where vend_id='DLL01' or vend_id='BRS01' and prod_price >= 10 ;
select prod_id,prod_price,prod_name from products where (vend_id='DLL01' or vend_id='BRS01') and prod_price >= 10 ;
select prod_name,prod_price from products where vend_id in ('DLL01','BRS01') order by prod_name ;
select prod_name,prod_price from products where vend_id='DLL01' or vend_id='BRS01' order by prod_name ;
select prod_name from products where not vend_id = 'DLL01' order by prod_name;
select prod_name from products where vend_id <> 'DLL01' order by prod_name ;

第六課like與通配符

like是謂詞

%不能匹配null

通配符使用起來很慢昂秃,應(yīng)該放在最后

select prod_id,prod_name from products where prod_name like 'Fish%';
select prod_id,prod_name from products where prod_name like '%bean bag%';
select prod_id,prod_name from products where prod_name like 'F%y%';/*后面有空格*/
select prod_id,prod_name from products where prod_name like '__ inch teddy bear%';
/*只有微軟件的數(shù)據(jù)庫才能用禀梳,可以在中括號里加^號來否定,其他的只能用not了*/
select cust_contact from customers where cust_contact like '[JM]%' order by cust_contact ;
select prod_id,prod_name from products where not prod_name like 'F%';

第七課計算字段

oracle用||拼接兩個列

rtrim()函數(shù)用于去除右邊空格

ltrim()函數(shù)用于去除左邊空格

trim()函數(shù)用于去除兩邊的空格

計算列肠骆,對列進(jìn)行計算算途,創(chuàng)建是新的列,可以重命名

select 可以用于運(yùn)算蚀腿,但是在這里不行

select rtrim(vend_name) || '(' || rtrim(vend_country) || ')' from vendors order by vend_name ;
select rtrim(vend_name) || '(' || rtrim(vend_country) || ')' as vend_title from vendors order by vend_name ;
select prod_id,quantity,item_price from orderitems where order_num='20008';
select prod_id,quantity,item_price,quantity*item_price as expanded_price from orderitems where order_num='20008';

第八課使用函數(shù)處理數(shù)據(jù)

upper()將小寫字母轉(zhuǎn)換成大寫字母

lower()將大寫字母轉(zhuǎn)換成小寫字母

rtrim()除去右邊空格

ltrim()除去左邊空格

length()字符串的長度

soundex()找出列類似的讀音

處理日期oracle沒有datepart()函數(shù)嘴瓤,只能用其他的了

to_char()函數(shù)取日期的部分

to_number()函數(shù)將取出的字符串類型日期轉(zhuǎn)換為數(shù)字類型

借助between and ,to_date()函數(shù)可以將字符串轉(zhuǎn)換成日期類型

還有比較多的數(shù)值處理函數(shù)

select vend_name, upper(vend_name) as vend_name_upcase from vendors order by vend_name ;
select vend_name, length(rtrim(vend_name)) as vend_name_length from vendors order by vend_name ;
select upper(vend_name) as vend_name_upcase,lower(upper(vend_name)) as vend_name_lower from vendors order by vend_name ;
select cust_contact from customers where soundex(cust_contact) = soundex('John Smith1');
select cust_name, cust_contact from customers where soundex(cust_contact) = soundex('Michael Green');
select order_num "訂單號",to_char(order_date,'MM')||'月'||to_char(order_date,'dd')||'日' as "日期" from orders where to_number(to_char(order_date,'YYYY')) = 2012 ;
select order_num from orders where order_date between to_date('2012/01/01','yyyy/MM/dd') and to_date('2012/12/31','yyyy/MM/dd') ;

第九課聚集函數(shù)

avg()返回某列的平均值

count()返回某列的行數(shù),參數(shù)為*\1則返回所有列莉钙,參數(shù)為某列則返回某列的非null值的個數(shù)

max()返回某列最大值,忽略null

min()返回某列最小值,忽略null

sum()返回某列值之和,忽略null

distinct參數(shù)用于指定不同的值,distinct后面應(yīng)該跟的是列名

select avg(prod_price) avg_price,count(*) num_items,max(prod_price) max_price,min(prod_price) min_price,sum(prod_price) total_price from products ;
select count(*) num_items,count(cust_email) email_items from customers ;
select sum(quantity)  from orderitems where order_num='20005';
select sum(quantity*item_price) as total_price from orderitems where order_num='20005';
select avg(distinct prod_price) as avg_pric from products where vend_id='DLL01';

第十課分組函數(shù)廓脆,group by\having

分組后使用聚集函數(shù)

group by可以對多個列進(jìn)行分組,select中使用了非計算列磁玉,都需要在group by中出現(xiàn)

oracle允許使用位置來指定分組

having與where使用相同狞贱,作用不同,where是作用在所有的行蜀涨,having是作用在分組內(nèi)

另一種說法瞎嬉,where是在分組前進(jìn)行過濾,group by是在分組后進(jìn)行過濾

使用group by時也一定要使用order by才能確認(rèn)順序

select vend_id,count(*) as num_prods from products group by vend_id ;
select prod_id,prod_name from products order by 1,2 ;
select * from orders
select cust_id,count(*) as orders from orders group by cust_id having count(*)>=2;
select vend_id,count(*) as num_prods from products where prod_price>=4 group by vend_id having count(*)>=2;
select order_num,count(*) as items from orderitems group by order_num having count(8)>=3 order by items,order_num;

第11課使用子查詢

子查詢總是從內(nèi)向外進(jìn)行處理

計算字段是子查詢語句的時候

下面使用的是完全限定列名厚柳,由表名與列名組合而成氧枣,意思是比較orders表中的cust_id與當(dāng)前正從customers表中檢索的cust_id

子查詢常用于in操作符與填充計算列中

購買RGAN01的顧客的信息

select order_num from orderitems where prod_id='RGAN01';
select cust_id from orders where order_num in ('20007','20008') ;
select cust_id from orders where order_num in (select order_num from orderitems where prod_id='RGAN01');
select cust_name,cust_contact from customers where cust_id in ('1000000004','1000000005');
select cust_name,cust_contact from customers where cust_id in (select cust_id from orders where order_num in (select order_num from orderitems where prod_id='RGAN01'));
/*對一個顧客執(zhí)行訂單數(shù)目及對全部顧客執(zhí)行訂單數(shù)目*/
select count(*) as orders from orders where cust_id='1000000001';
select cust_name,cust_state,(select count(*) from orders where orders.cust_id=customers.cust_id) as orders from customers order by cust_name ;

(重要)第12課聯(lián)結(jié)表join

關(guān)系數(shù)據(jù)庫的設(shè)計:一類數(shù)據(jù)一個表

用from聯(lián)結(jié)兩表的時候,如果沒有where則將第一個表的每一行與第二個表的每一行進(jìn)行聯(lián)結(jié)(叉聯(lián)結(jié)\笛卡兒積)

等值聯(lián)結(jié)\內(nèi)聯(lián)結(jié)

select vend_name,prod_name,prod_price from products,vendors where vendors.vend_id=products.vend_id ;
/*內(nèi)聯(lián)結(jié)别垮,指明聯(lián)結(jié)的類型*/
select vend_name,prod_name,prod_price from vendors inner join products on vendors.vend_id=products.vend_id ;
select prod_name,vend_name,prod_price,quantity from orderitems,products,vendors where products.vend_id=vendors.vend_id and products.prod_id=orderitems.prod_id and orderitems.order_num='20007';
/*只需要用到多個表的數(shù)據(jù)都可以用聯(lián)結(jié)查詢*/
select cust_name,cust_contact from customers,orders,orderitems where customers.cust_id=orders.cust_id and orders.order_num=orderitems.order_num and prod_id='RGAN01';
select cust_name,cust_contact from customers inner join orders on customers.cust_id=orders.cust_id inner join orderitems on orders.order_num=orderitems.order_num where orderitems.prod_id='RGAN01';

第13課創(chuàng)建高級聯(lián)結(jié)

自聯(lián)結(jié)便监、自然聯(lián)結(jié)inner join、外聯(lián)結(jié)left outer join\right outer join

全外聯(lián)結(jié)full outer join

別名的另一個作用是自聯(lián)結(jié)

可以多個表聯(lián)結(jié),況且聯(lián)結(jié)時可以使用多種不同的聯(lián)結(jié)

聯(lián)結(jié)時一定要使用正確的聯(lián)結(jié)條件

select rtrim(vend_name)||'('||rtrim(vend_country)||')' as vend_title from vendors order by vend_name ;
select cust_name,cust_contact from customers C ,orders O,orderitems OI where C.cust_id=O.cust_id and O.order_num=OI.order_num and OI.prod_id='RGAN01';
/*自聯(lián)結(jié)與子查詢對比烧董,自聯(lián)結(jié)會更快*/
select cust_id,cust_name,cust_contact,cust_email from customers where cust_name = (select cust_name from customers where cust_contact='Jim Jones');
select C1.cust_id,C1.cust_name,C1.cust_contact,C1.cust_email from customers C1,Customers C2 where C1.cust_name=C2.cust_name and C2.cust_contact='Jim Jones' ;
/*自然聯(lián)結(jié)毁靶,至少存在一列出現(xiàn)在兩個表中*/
select C.*,O.order_date,O.cust_id,OI.prod_id,OI.quantity,OI.item_price from customers C,orders O,orderitems OI where C.cust_id=O.cust_id and O.order_num = OI.order_num and OI.prod_id='RGAN01';
/*外聯(lián)結(jié),聯(lián)結(jié)在相關(guān)的表中沒有對應(yīng)行的行逊移,外聯(lián)結(jié)與內(nèi)聯(lián)結(jié)對比预吆。外聯(lián)結(jié)將沒有對應(yīng)的行的數(shù)據(jù)也進(jìn)行輸出*/
select C.cust_id, O.order_num from customers C inner join orders O on C.cust_id=O.cust_id ;
select C.cust_id, O.order_num from customers C left outer join orders O on C.cust_id=O.cust_id ;
/*全外關(guān)聯(lián),兩個表的沒有對應(yīng)的行的數(shù)據(jù)也進(jìn)行關(guān)聯(lián)*/
select customers.cust_id, orders.order_num from customers full outer join orders on customers.cust_id=orders.cust_id ;
/*內(nèi)聯(lián)與聚集函數(shù)一起用*/
select C.cust_id,count(O.order_num) num_order from customers C inner join orders O on C.cust_id=O.cust_id group by C.cust_id ;
select C.cust_id,count(O.order_num) num_order from customers C left outer join orders O on C.cust_id=O.cust_id group by C.cust_id ;

第14課組合查詢union

union all可以返回重復(fù)的匹配值

多個union的列要相同胳泉,順序沒有要求拐叉,有聚集函數(shù)也要相同

union的排序只需要在最后一個select語句中使用order by語句*/

對比兩種實(shí)現(xiàn)方法

select cust_name,cust_contact,cust_email from customers where cust_state in('IL','IN','MI');
select cust_name,cust_contact,cust_email from customers where cust_name='Fun4All';
select cust_name,cust_contact,cust_email from customers where cust_state in('IL','IN','MI')
union all
select cust_name,cust_contact,cust_email from customers where cust_name='Fun4All';
select cust_name,cust_contact,cust_email from customers where cust_state in('IL','IN','MI') or cust_name='Fun4All';
select cust_name,cust_contact,cust_email from customers where cust_state in('IL','IN','MI')
union
select cust_name,cust_contact,cust_email from customers where cust_name='Fun4All' order by cust_name desc;

第15課插入數(shù)據(jù)insert

插入完整的行

插入行的一部分

插入某些查詢的結(jié)果

insert select

select into

最簡單的insert語句,不安全扇商,因?yàn)楸斫Y(jié)構(gòu)變化將不能使用凤瘦,應(yīng)該指定列名,對比以下語句

指定了全部列名,這樣安全性高些案铺,列名與值的內(nèi)容可以按照任意次序出現(xiàn)蔬芥,保證前后對應(yīng)即可

insert into customers values('1000000006','Toy Land','123 Any Street','New York','NY','11111','USA',null,null);
insert into customers(cust_id,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country,cust_contact,cust_email) values('1000000006','Toy Land','123 Any Street','New York','NY','11111','USA',null,null);
/*插入查詢結(jié)果,selecr后的列名不重要,只會把select后的按位置插入*/
create table custnew as select * from customers ;
select * from custnew for update ;
insert into customers
    (cust_id,
     cust_name,
     cust_address,
     cust_city,
     cust_state,
     cust_zip,
     cust_country,
     cust_contact,
     cust_email)
    select * from custnew;
/*復(fù)制表控汉,第一種情況不適用于oracle*/
select * into custcopy from customers ;
create table custnew as select * from customers ;

第16課更新和刪除數(shù)據(jù)update/delete

使用update與delete時一定要注意笔诵,不要漏了where語句

若要清除一格的值,可以將其值設(shè)置為null

delete 刪除一行至所有行暇番,不刪除表

如果需要刪除所有行有更快的語法truncate table

drop才可以刪除表結(jié)構(gòu)

使用update\delete語句前先用select語句選出所有值檢查沒問題后再更改

update customers set cust_email='kim@thetoystore.com' where cust_id='1000000005' ;
update customers set cust_contact='Sam Roberts',cust_email='sam@toyland.com' where cust_id='1000000006';
update customers set cust_email=null where cust_id='1000000005';
delete from customers where cust_id='1000000012';
truncate table custnew ;/*0.046S*/
delete from custnew2 ;/*0.016S*/
drop table custnew ;

第17課創(chuàng)建和操縱表

列名唯一嗤放,數(shù)據(jù)類型難點(diǎn)思喊,后面的描述復(fù)雜

防止意外覆蓋表壁酬,在創(chuàng)建表之前需要對表進(jìn)行手動刪除

default 1是用于設(shè)置默認(rèn)值

default sysdate用于設(shè)置默認(rèn)時間

更新表(添加列)alter table,有數(shù)據(jù)時候更新不好,應(yīng)該在設(shè)計時就考慮好

如果表的結(jié)構(gòu)更改過大恨课,可以創(chuàng)建新的表存放舊內(nèi)容舆乔,替換掉

drop table無法恢復(fù)

rename to 重命名表名

CREATE TABLE Products
(
  prod_id    char(10)      NOT NULL ,
  vend_id    char(10)      NOT NULL ,
  prod_name  char(255)     NOT NULL ,
  prod_price decimal(8,2)  NOT NULL default 1,
  prod_desc  varchar(1000) NULL 
);
insert into pdm values(to_char(sysdate,'yyyy\MM\dd'));
alter table vendors add vend_phone char(20);
alter table vendors drop column vend_phone ;
drop table custnew ;
create table custnew as select * from customers ;
rename custnew to custcopy ;
select * from custcopy ;

第18課使用視圖

一個虛擬表,可以很方便地通過查到數(shù)據(jù)

視圖可能禁止使用order by

create view創(chuàng)建視圖

drop view viewname刪除視圖

創(chuàng)建出來的視圖就相當(dāng)于一個表

視圖是處理表后剂公,得到想要的數(shù)據(jù)的新的表

select cust_name,cust_contact,prod_id from customers,orders,orderitems where customers.cust_id=orders.cust_id and orders.order_num=orderitems.order_num and prod_id='RGAN01';
create view productcustomers as select cust_name,cust_contact,prod_id from customers,orders,orderitems where customers.cust_id=orders.cust_id and orders.order_num=orderitems.order_num;
select cust_name,cust_contact from productcustomers where prod_id='RGAN01';
select rtrim(vend_name)||'('||rtrim(vend_country)||')' as vend_title from vendors order by vend_name ;
create view vendorlocation as select rtrim(vend_name)||'('||rtrim(vend_country)||')' as vend_title from vendors order by vend_name ;
select * from vendorlocation ;
create view customeremaillist as select cust_id,cust_name,cust_email from customers where cust_email is not null ;
select * from customeremaillist ;
create view orderitemsexpanded as select order_num,prod_id,quantity,item_price,quantity*item_price as expanded_proce from orderitems;
select * from orderitemsexpanded where order_num=20008 ;

第19課使用存儲過程

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末希俩,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子纲辽,更是在濱河造成了極大的恐慌颜武,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,194評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拖吼,死亡現(xiàn)場離奇詭異鳞上,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)吊档,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評論 2 385
  • 文/潘曉璐 我一進(jìn)店門篙议,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事鬼贱∫魄樱” “怎么了?”我有些...
    開封第一講書人閱讀 156,780評論 0 346
  • 文/不壞的土叔 我叫張陵这难,是天一觀的道長舟误。 經(jīng)常有香客問我,道長雁佳,這世上最難降的妖魔是什么脐帝? 我笑而不...
    開封第一講書人閱讀 56,388評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮糖权,結(jié)果婚禮上堵腹,老公的妹妹穿的比我還像新娘。我一直安慰自己星澳,他們只是感情好疚顷,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,430評論 5 384
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著禁偎,像睡著了一般腿堤。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上如暖,一...
    開封第一講書人閱讀 49,764評論 1 290
  • 那天笆檀,我揣著相機(jī)與錄音,去河邊找鬼盒至。 笑死酗洒,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的枷遂。 我是一名探鬼主播樱衷,決...
    沈念sama閱讀 38,907評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼酒唉!你這毒婦竟也來了矩桂?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,679評論 0 266
  • 序言:老撾萬榮一對情侶失蹤痪伦,失蹤者是張志新(化名)和其女友劉穎侄榴,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體网沾,經(jīng)...
    沈念sama閱讀 44,122評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡癞蚕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,459評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了绅这。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片涣达。...
    茶點(diǎn)故事閱讀 38,605評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出度苔,到底是詐尸還是另有隱情匆篓,我是刑警寧澤,帶...
    沈念sama閱讀 34,270評論 4 329
  • 正文 年R本政府宣布寇窑,位于F島的核電站鸦概,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏甩骏。R本人自食惡果不足惜窗市,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,867評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望饮笛。 院中可真熱鬧咨察,春花似錦、人聲如沸福青。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,734評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽无午。三九已至媒役,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間宪迟,已是汗流浹背酣衷。 一陣腳步聲響...
    開封第一講書人閱讀 31,961評論 1 265
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留次泽,地道東北人穿仪。 一個月前我還...
    沈念sama閱讀 46,297評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像箕憾,于是被迫代替她去往敵國和親牡借。 傳聞我的和親對象是個殘疾皇子拳昌,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,472評論 2 348

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

  • 第三課: 排序檢索數(shù)據(jù) distinct關(guān)鍵字:distinct 列名1袭异,列名2,列名3DISTINCT 關(guān)鍵字會...
    VictorBXv閱讀 1,468評論 0 8
  • 表 存儲在表中的數(shù)據(jù)是同一種類型的數(shù)據(jù)或清單炬藤。 數(shù)據(jù)庫中的表有為一個名字來標(biāo)識自己御铃。 表具有一些特性,這些特性定義...
    蛐蛐囍閱讀 1,305評論 0 7
  • 注:這一系列的文章是《SQL必知必會》第四版的讀書筆記沈矿。 7.創(chuàng)建計算字段 什么是計算字段上真,怎么創(chuàng)建計算字段,以及...
    zuyuxia閱讀 332評論 0 0
  • 一羹膳、基本概念 數(shù)據(jù)庫睡互、表、列、行就珠、主鍵寇壳、外鍵、關(guān)鍵字 二妻怎、檢索 1.檢索列 SELECT prod_name(id...
    兀鏡曉Jingle閱讀 343評論 0 7
  • SQL 必知必會 在mac終端操作sqlite: cd 數(shù)據(jù)庫所在的目錄 sqlite3 數(shù)據(jù)庫文件名 //帶后綴...
    陳_振閱讀 749評論 0 0