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