常見(jiàn)注釋
- -- 很少支持
-
行內(nèi)注釋
- /**/段落注釋
基礎(chǔ)語(yǔ)法
SELECT
檢索數(shù)據(jù)
| 語(yǔ)法 |
作用 |
例子 |
釋義 |
| select |
查找列,并返回行 |
select prod_name from products; #可使用,分隔列名來(lái)查找多個(gè)列。 |
查找prod_name列,并返回其下的所有行,在products表中。 |
| * |
通配符 |
select * from products; |
查找所有列并返回所有行,在products表中。 |
| distinct |
返回不重復(fù)的值 |
select distinct vend_id from products; #不可配合通配符使用除非所有列完全相同 |
查找vend_id列并返回其下所有行中不重復(fù)的值,在products表中。 |
| limit |
限制 |
select prod_name from products limit 5,5; |
查找prod_name列并返回其下第5行起5行的值,在products表中。 |
排序檢索數(shù)據(jù)
| 語(yǔ)法 |
作用 |
例子 |
釋義 |
| order by |
排序 |
select prod_id,prod_price,prod_name from Products order by 2; #默認(rèn)升序(ASC)排列 #指定按多個(gè)列排列時(shí):僅當(dāng)指定的第一列中有重復(fù)元素時(shí),才對(duì)其(存在重復(fù)值的)按指定的下一列進(jìn)行排序。 |
即按照查找的第二個(gè)列進(jìn)行排序,也可指定列名(prod_price) |
| desc |
降序 |
select prod_id,prod_price,prod_name from Products order by 2 DESC,3 desc; |
即按照查找的第二個(gè)列進(jìn)行降序排序,desc僅對(duì)其前的列有效; |
過(guò)濾數(shù)據(jù)
| 語(yǔ)法 |
作用 |
例子 |
釋義 |
| where |
在客戶端過(guò)濾數(shù)據(jù) |
select * from Products where prod_price >= 5.99 order by prod_price desc; #同排序操作一同使用時(shí),不得位于排序操作之前#支持<>=!=等操作,其中<>操作等同于!= 例: select * from Products where prod_id <>'fc' order by prod_price desc; #過(guò)濾字符串不區(qū)分大小寫(xiě) |
1.查找所有列,在Products表中,并返回prod_price >=5.99的所有行 2.查找所有列,在Products表中,并返回除prod_id = "fc"之外的所有行 |
| between |
值的范圍過(guò)濾 |
select prod_name,prod_price from Products where prod_price between 4 and 10 |
查找prod_name,prod_price兩列在Products表中,并返回prod_price值為4-10范圍內(nèi)的的的所有行 |
| is |
可用來(lái)檢查null(空值) |
select prod_name,prod_price from Products where prod_price is null |
返回所有沒(méi)有價(jià)格的商品 |
| and,or |
邏輯操作符 and且 or與,這里是短路的 |
select * from Products where vend_vend_id ='1001' and prod_price <=4; #and的優(yōu)先級(jí)比or要高,and,or共同使用時(shí)為避免錯(cuò)誤應(yīng)用()明確分組, #也可使用in代替or,例: select prod_name,prod_price from products where vend_id in('1001','1002') order by prod_name 等同于: select prod_name,prod_price from products where vend_id = '1001' or vend_id = '1002' order by prod_name |
返回所有vend_vend_id ='1001' 且 prod_price <=4;的行 |
| not |
否定其后的條件 |
select prod_name,prod_price from products where not vend_id in('1001','1002') order by prod_name |
可與in連用,返回vend_id=1001 vend_id=1002外的所有行 |
|
|
|
|
通配符搜索
| 語(yǔ)法 |
作用 |
例子 |
| % |
匹配0、1或多個(gè)字符包含空格。不會(huì)匹配到null |
select prod_name from products where prod_name like 'f%%' |
| _ |
匹配單個(gè)字符,包含空格 |
select prod_name from products where prod_name like 'fuse_' |
| rtrim()ltrim() |
去除右邊、左邊空格 |
|
創(chuàng)建計(jì)算字段
select prod_id ,quantity,item_price,quantity*item_price as expanded_price
from orderitems
where order_num = 20008;
#如上創(chuàng)建了一個(gè)expanded_price字段(quantity*item_price的結(jié)果的別名),其僅在此時(shí)有效而不會(huì)存放到表中。
使用函數(shù)
select vend_name, upper(vend_name) as vend_name_upcase
from vendors
#將vend_name列下的所有行以大寫(xiě)形式返回
select avg(prod_price) as avg_peice from products where vend_id ='1001'
#返回平均值
select count(*) as num_cust from customers
#返回長(zhǎng)度(數(shù)目),也可對(duì)列表中特定值進(jìn)行計(jì)數(shù)
分組
select vend_id,count(*) as num_prods from products #對(duì)vend_id每行進(jìn)行計(jì)數(shù)
group by vend_id;#按照vend_id排序并分組
select cust_id,count(*) as orders
from orders
group by cust_id
having count(*)>=2#過(guò)濾分組中>=2的,having支持where的所有操作
select order_num,count(*) as items
from orderitems group by order_num
having count(*) >=3
order by items,order_num desc#對(duì)分組依照選定的列進(jìn)行排序
子句查詢
select cust_name,cust_contact from customers where cust_id =(select cust_id
from orders
where order_num = (select order_num from orderitems where prod_id = 'jp2000'));
#由內(nèi)而外,哈哈
等效于:
select order_num
from orderitems
where prod_id = 'jp2000';
select cust_id
from orders
where order_num =20006
select cust_name,cust_contact from customers where cust_id =10003
聯(lián)結(jié)(返回不在同一個(gè)表中的行)
/*等值語(yǔ)法*/
select vend_name, prod_name,prod_price
from vendors,products
where vendors.vend_id=products.vend_id;#此處過(guò)濾聯(lián)結(jié)條件。
#如沒(méi)有聯(lián)結(jié)條件過(guò)濾,將檢索出“笛卡爾積”:表1行數(shù)*表2行數(shù)
/*規(guī)范語(yǔ)法*/
select vend_name, prod_name,prod_price
from vendors inner join products
on vendors.vend_id=products.vend_id
自聯(lián)結(jié)(比子查詢更快)
/*子查詢*/
select cust_id, cust_name, cust_contact
from customers
where cust_name = (select cust_name from customers where cust_contact ='jim jones');
/*自聯(lián)結(jié)*/
select c1.cust_id, c1.cust_name,c1.cust_contact
from customers as c1,customers as c2#不以別名進(jìn)行會(huì)引發(fā)錯(cuò)誤
where c1.cust_name=c2.cust_name and c2.cust_contact='jim jones'#聯(lián)結(jié)cust_name與c2.cust_name ,并過(guò)濾cust_contact='jim jones'的行
/*(c1的cust_name同c2相同,找到了c2的cust_contact='jim jones'也就相當(dāng)于找到了c1cust_contact='jim jones',知道了cust_contact='jim jones'就可知道cust_id)*/
組合查詢
select cust_name,cust_contact,cust_email,cust_state
from customers
where cust_state in('il','in','mi')
union #組合上下select多個(gè)select之間需要多個(gè)union分隔,union默認(rèn)排除重復(fù),union all則不排除
select cust_name,cust_contact,cust_email,cust_state#union中每個(gè)查詢必須包含相同的列、表達(dá)式、或聚集函數(shù)
from customers
where cust_name ='wascals'
order by cust_name;#不能分別對(duì)每條union指定不同的排序
INSERT
依賴(lài)于次序的插入
/*在得知列的次序后才可使用此方式添加,若發(fā)生了列的次序變動(dòng)此添加方式將不安全*/
insert into customers
values('1000000006','toy land','123 any street','new york','ny', '11111','usa',null, null);
#依賴(lài)于次序的插入,必須為每一列提供一個(gè)值,如某列無(wú)添加則應(yīng)寫(xiě)上null
提供列名的插入
insert into customers(cust_id,cust_contact,cust_email,cust_name,cust_address,cust_city,cust_state,cust_zip)
#必須為提供了列名的列給出一個(gè)值
values(null, null,'1000000006','toy land','123 any street','new york','ny', '11111');
從另一個(gè)表插入
insert into 表名(列名)
select 列名
from 表名
where 過(guò)濾
復(fù)制一個(gè)表
/*sql*/
select *
into custcopy
from customers;
/* mysql*/
create table custcopy as
select *
from customers;
UPDATE
更新單個(gè)列
update customers
set cust_email = 'kim@@thetoystore.com'
where cust_id = '100000000005'#如不指定,將更新customers表cust_email列下的所有行
更新多個(gè)列
update customers
set cust_email = 'kim@@thetoystore.com',cust_contact='sam roberts'
where cust_id = '100000000006'
DELLETE
delete from customers
where cust_id = '1000000006'#刪除此行,不過(guò)濾則刪除所有行
#update刪除列
#truncate刪除表
添加刪除列&&表
添加表
/*添加表時(shí)為防止覆蓋,應(yīng)刪除表后再進(jìn)行添加*/
create table orderitems
(order_num integer not null,
order_item integer not null,
prod_id char(10) not null,
quantity integer not null default 1,#設(shè)置quantity列下的行默認(rèn)值為1
item_price decimal(8,2) not null);#not null即不允許填入null,默認(rèn)可填入null,只有為 not null的列方可為主鍵及唯一標(biāo)識(shí)
/*add列*/
alter table vendors
add vend_phone char (20);
/*del列*/
alter table vendors
drop column vend_phone;#此操作不可逆
/*刪除表*/
drop table custcopy;#此操作不可逆
視圖
create view#創(chuàng)建視圖
drop view 視圖名#刪除視圖
/*以視圖簡(jiǎn)化聯(lián)結(jié),創(chuàng)建視圖*/
create view productcustomers as
select cust_name , cust_contact,prod_id
from customers,orders,orderitems
where customers.cust_id=orders.cust_id
and orderitems.order_num=orders.order_num;
/*可對(duì)視圖采取與表相同的查詢操作*/
select *
from productcustomers;
/*一個(gè)視圖過(guò)濾查詢例子*/
create view customeremaillist as
select cust_id, cust_name,cust_email
from customers
where cust_email is not null;#返回查詢中所有cust_email不為空的,并將其添加到視圖中
/*視圖計(jì)算字段例子*/
create view orderitemsexpanded as
select order_num,prod_id,quantity,item_price,quantity*item_price,quantity*item_price as
expanded_price
from orderitems
事務(wù)管理
/*撤銷(xiāo)整體*/
start transaction ;
-- 標(biāo)識(shí)事務(wù)處理塊,塊中內(nèi)容未執(zhí)行完則整體撤銷(xiāo)
/*撤銷(xiāo)部分操作*/
savepoint delete1;#標(biāo)識(shí)
rollback to delete1;返回標(biāo)識(shí)delete1
|