對于數(shù)據(jù)庫服務(wù)器,現(xiàn)在市面上流行的有很多,個人使用過的有Oracle、Mysql和DB2,個人最喜歡的就是Mysql,開源界神一樣的作品之一,雖然,被Oracle收購之后更新速度明顯慢了下來,但是依然被市場廣泛使用(黑一下甲骨文)。
SQL是目前使用最為廣泛的數(shù)據(jù)庫語言之一。這里,我總結(jié)了在數(shù)據(jù)庫上,用SQL語言對數(shù)據(jù)排序、過濾和分組,以及表、視圖、聯(lián)結(jié)、子查詢、游標(biāo)、存儲過程和觸發(fā)器等內(nèi)容。
1.檢索數(shù)據(jù)
SELECT prod_nameFROM Products;
#檢索單列
SELECT prod_id, prod_name, prod_priceFROMProducts;
#檢索多列
SELECT * FROM Products;
#檢索所有列
SELECT DISTINCTvend_id FROMProducts;
#檢索不同的值
SELECTprod_name FROM Products LIMIT 5;
#返回不超過5行數(shù)據(jù)
SELECTprod_name FROM Products LIMIT 5 OFFSET 5;
#返回從第5行起的5行數(shù)據(jù)。LIMIT指定返回的行數(shù),LIMIT帶的OFFSET指定從哪兒開始。
/* SELECT prod_name, vend_id
FROMProducts; */
SELECTprod_name
FROMProducts;
#多行注釋
2.排序檢索數(shù)據(jù)
SELECTprod_name
FROMProducts
ORDER BYprod_name;
#排序數(shù)據(jù)
SELECT prod_id, prod_price, prod_name
FROMProducts
ORDER BY prod_price, prod_name;
#按多個列排序
SELECT prod_id, prod_price, prod_name
FROMProducts
ORDER BY 2, 3;
#按列位置排序,第三行表示先按prod_price, 再按prod_name進(jìn)行排序
SELECT prod_id, prod_price, prod_name
FROMProducts
ORDER BY prod_priceDESC, prod_name;
#prod_price列以降序排序,而prod_name列(在每個價(jià)格內(nèi))仍然按標(biāo)準(zhǔn)的升序排序
3.過濾數(shù)據(jù)
SELECT prod_name, prod_price
FROMProducts
WHERE prod_price《 10;
#檢查單個值
SELECT prod_name, prod_price
FROMProducts
WHERE vend_id 《》 ‘DLL01’;
#不匹配檢查
SELECT prod_name, prod_price
FROMProducts
WHERE prod_priceBETWEEN 5 AND 10;
#范圍值檢查
SELECT cust_name
WHERE cust_emailIS NULL;
#空值檢查
4.高級數(shù)據(jù)過濾
SELECTprod_id, prod_price, prod_name
FROMProducts
WHERE vend_id = ‘DLL01’ANDprod_price 《= 4;
#AND操作符
SELECTprod_name, prod_price
FROMProducts
WHEREvend_id=’DLL01’ OR vend_id=’BRS01’;
#OR操作符
SELECTprod_name, prod_price
FROMProducts
WHERE (vend_id = ’DLL01’ORvend_id=’BRS01’)
ANDprod_price 》= 10;
#求值順序 AND的優(yōu)先級高于OR
SELECTprod_name, prod_price
FROMProducts
WHERE vend_idIN (‘DLL01’,’BRS01’)
ORDER BY prod_name;
#IN操作符
SELECT prod_name
FROMProducts
WHERE NOTvend_id = ‘DLL01’
ORDER BY prod_name;
#NOT 操作符
SELECT prod_name
FROMProducts
WHEREvend_id 《》 ‘DLL01’
ORDER BY prod_name;
#NOT 操作符
5.通配符進(jìn)行過濾
SELECT prod_id, prod_name
FROMProducts
WHERE prod_nameLIKE ‘Fish%’;
#%表示任何字符出現(xiàn)任意次數(shù),找出所有以詞Fish起頭的產(chǎn)品
SELECT prod_id, prod_name
FROMProducts
WHERE prod_nameLIKE ‘%bean bag%’;
#‘%bean bag%’表示匹配任何位置上包含文本bean bag的值,不論它在之前或之后出現(xiàn)什么字符
SELECT prod_name
FROMProducts
WHERE prod_nameLIKE ‘F%y’;
#找出以F起頭,以y結(jié)尾的所有產(chǎn)品
根據(jù)郵件地址的一部分來查找電子郵件,例如WHERE email LIKE ‘b%@forta.com’
WHERE prod_nameLIKE ‘%’; #不會匹配產(chǎn)品名稱為NULL的行,其它均可
%代表搜索模式中給定位置的0個、1個或多個字符
下劃線的用途與%一樣,但它只匹配單個字符,而不是多個字符
SELECT prod_id, prod_name
FROMProducts
WHERE prod_nameLIKE ‘__inchteddy bear’;
#搜索模式要求匹配兩個通配符而不是一個
方括號([])通配符用來指定一個字符集,它必須匹配指定位置(通配符的位置)的一個字符
SELECT cust_contact
FROMCustomers
WHERE cust_contactLIKE ‘[JM]%’
ORDER BY cust_contact;
#[JM]匹配方括號中任意一個字符,它也只能匹配單個字符,任何多于一個字符的名字都不匹配。[JM]之后的%通配符匹配第一個字符之后的任意數(shù)目的字符,返回所需結(jié)果。
SELECT cust_contact
FROMCustomers
WHERE cust_contactLIKE ‘[^JM]%’
ORDER BY cust_contact;
#以J和M之外的任意字符起頭的任意聯(lián)系人名
6.創(chuàng)建計(jì)算字段
SELECT Concat(vend_name, ‘ (‘, vend_country, ‘)’)
FROMVendors
ORDER BY vend_name;
輸出
Bear Emporium(USA)
Bears R Us (USA)
Doll House Inc.(USA)
Fun and Games(England)
SELECT Concat(vend_name, ‘ (‘, vend_country, ‘)’)
ASvend_title
FROMVendors
ORDER BY vend_name; #給拼接而成新字段起了一個名稱
SELECT prod_id,
quantity,
item_price,
quantity*item_price AS expanded_price
FROMOrderItems
WHERE order_num = 20008;
#匯總物品的價(jià)格
7.使用函數(shù)處理數(shù)據(jù)
SELECT vend_name, UPPER(vend_name)AS vend_name_upcase
FROMVendors
ORDER BY vend_name;
#文本處理函數(shù)
SELECT cust_name, cust_contact
FROMCustomers
WHERE SOUNDEX(cust_contact) =SOUNDEX(‘MichaelGreen’);
# SOUNDEX()函數(shù)搜索,匹配所有發(fā)音類似于Michael Green 的聯(lián)系名
SELECT order_num
FROMOrders
WHERE YEAR(order_date) = 2012;
#從日期中提取年份
8.數(shù)據(jù)匯總
SELECT AVG(prod_price)ASavg_price
FROMProducts;
WHERE vend_id = ‘DLL01’;
SELECT COUNT(*)ASnum_cust
FROMCustomers;
#COUNT(*)對表中行的數(shù)目進(jìn)行計(jì)數(shù),不管表列中包含的是空值(NULL)還是非空值
SELECT COUNT(cust_email)ASnum_cust
FROMCustomers;
#只對具有電子郵件地址的客戶計(jì)數(shù)
SELECT MAX(prod_price)ASmax_price
FROMProducts;
#返回Products表中最貴物品的價(jià)格
SELECT MIN(prod_price)ASmin_price
FROMProducts;
#返回Products表中最便宜物品的價(jià)格
SELECT SUM(quantity)ASitems_ordered
FROMOrderItems
WHERE order_num = 20005;
#SUM(quantity)返回訂單中所有物品數(shù)量之和,WHERE 子句保證只統(tǒng)計(jì)某個物品訂單中的物品
SELECT SUM(item_price*quantity)AS total_price
FROMOrderItems
WHERE order_num = 20005;
#SUM(item_price*quantity)返回訂單中所有物品價(jià)錢之和,WHERE子句保證只統(tǒng)計(jì)某個物品訂單中的物品
SELECT AVG(DISTINCTprod_price)AS avg_price
FROMProducts
WHERE vend_id = ‘DLL01’;
#使用DISTINCT參數(shù),平均值只考慮各個不同的價(jià)格
SELECT COUNT(*) AS num_items,
MIN(prod_price)AS price_min,
MAX(prod_price)AS price_max,
AVG(prod_price)AS price_avg
FROMProducts;
#組合聚集函數(shù)
9.分組數(shù)據(jù)
SELECT vend_id,COUNT(*) AS num_prods
FROMProducts
GROUP BY vend_id;
#創(chuàng)建分組
SELECT vend_id,COUNT(*) AS num_prods
FROMProducts
WHERE prod_price 》= 4
GROUP BY vend_id
HAVING COUNT(*) 》= 2;
#WHERE 子句過濾所有prod_price至少為4的行,然后按vend_id分組數(shù)據(jù),HAVING子句過濾計(jì)數(shù)為2或2以上的分組。
SELECT order_num,COUNT(*) AS items
FROMOrderItems
GROUP BY order_num
HAVING COUNT(*) 》= 3
ORDER BY items, order_num;
#按訂購物品的數(shù)目排序輸出
10.使用子查詢
SELECT cust_id
FROMOrders
WHERE order_numIN (SELECT order_num
FROM OrderItems
WHERE prod_id = ‘RGAN01’);
SELECT cust_name, cust_contact
FROMCustomers
WHERE cust_idIN (‘10000000004’, ‘10000000005’);
11.聯(lián)結(jié)表
SELECT vend_name, prod_name, prod_price
FROMVendors, Products
WHERE Vendors vend_id = Products.vend_id;
#創(chuàng)建聯(lián)結(jié)
SELECT vend_name, prod_name, prod_price
FROMVendorsINNER JOIN Products
ONVendors.vend_id = Products.vend_id;
#內(nèi)聯(lián)結(jié)
SELECT prod_name, vend_name, prod_price, quantity
FROMOrderItems, Products, Vendors
WHERE Products.vend_id = Vendors.vend_id
ANDOrderItems.prod_id = Products.prod_id
ANDorder_num = 20007;
#聯(lián)結(jié)多個表
12.創(chuàng)建高級聯(lián)結(jié)
SELECT c1.cust_id, c1.cust_name, c1.cust_contact
FROMCustomersAS c1, Customers AS c2
WHERE c1.cust_name = c2.cust_name
ANDc2.cust_contact = ‘Jim Jones’;
#自聯(lián)結(jié),此查詢中需要的兩個表實(shí)際上是相同的表
SELECT C. *, O.order_num, O.order_date,
OI.prod_id, OI.quantity, OI.item_price
FROMCustomersAS C, Orders AS O, OrderItems AS OI
WHERE C.cust_id = O.cust_id
ANDOI.order_num = O.order_num
ANDprod_id = ‘RGAN01’;
#自然聯(lián)結(jié)排除多次出現(xiàn),使每一列只返回一次
SELECT Customers.cust_id, Orders.order_num
FROMCustomersLEFT OUTER JOIN Orders
ONCustomers.cust_id = Orders.cust_id;
#從FROM子句左邊的表Customers表中選擇所有行
SELECT Customers.cust_id, Orders.order_num
FROMCustomersRIGHT OUTER JOIN Orders
ONOrders.cust_id =Customers.cust_id;
#從右邊的表中選擇所有行。
SELECT Customers.cust_id, Orders.order_num
FROMOrdersFULL OUTER JOIN Customers
ONOrders.cust_id = Customers.cust_id;
#檢索兩個表中的所有行并關(guān)聯(lián)那些可以關(guān)聯(lián)的行
13.組合查詢
SELECT cust_name, cust_contact, cust_email
FROMCustomers
WHERE cust_state IN (‘IL’, ‘IN’, ‘MI’)
UNION
SELECT cust_name, cust_contact, cust_email
FROMCustomers
WHERE cust_name = ‘Fun4ALL’
ORDER BY cust_name, cust_contact;
#SQL允許執(zhí)行多個查詢,并將結(jié)果作為一個查詢結(jié)果集返回
14.插入數(shù)據(jù)
INSERT INTO Customers(cust_id,
Cust_name,
Cust_address,
Cust_city,
Cust_state,
Cust_zip,
Cust_country,
Cust_contact,
Cust_email)
VALUES(‘100000000006’,
‘Toy Land’,
‘123 Any Street’,
‘New York’,
‘NY’,
‘111111’,
‘USA’,
NULL,
NULL);
#插入完整的行
INSERT INTO Customers(cust_id,
Cust_contact,
Cust_email,
Cust_name,
Cust_address,
Cust_city,
Cust_state,
Cust_zip,
Cust_country)
SELECT cust_id,
Cust_contact,
Cust_email,
Cust_name,
Cust_address,
Cust_city,
Cust_state,
Cust_zip,
Cust_country
FROMCustNew;
#將另一個表中的顧客列合并到Customers表中。
SELECT *
INTOCustCopy
FROMCustomers;
#從一個表復(fù)制到另一個表中
15.更新和刪除數(shù)據(jù)
UPDATE Customers
SETcust_contact = ‘Sam Roberts’,
Cust_email = ‘sam@toyland.com’
WHERE cust_id = ‘100000000000006’;
#更新多個列
UPDATE Customers
SETcust_email = NULL
WHERE cust_id = ‘1000000005’;
#刪除某個列
DELETE FROM Customers
WHERE cust_id = ‘1000000006’;
#刪除數(shù)據(jù)
16. 創(chuàng)建和操縱表
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,
Item_price DECIMAL(8, 2) NOT NULL
);
ALTER TABLE Vendors
ADDvend_phone CHAR(20);
#給表增加一個名為vend_phone的列,其數(shù)據(jù)類型為CHAR
ALTER TABLE Vendors
DROP COLUMN vend_phone;
#該表中的某列
DROP TABLE CustCopy;
#刪除表
17.高級SQL特性
主鍵:表中一列(或多個列)的值唯一標(biāo)識表中的每一行。主鍵是一種特殊的約束,用來保證一列或一組列的值唯一標(biāo)識表中的每一行。這方便直接或交互地處理表中的行。沒有主鍵,要安全地UPDATE 或DELETE特定行而不影響其他行會非常困難。
①任意兩行的主鍵值都不相同;
②每行都具有一個主鍵值(即列中不允許NULL值)
③包含主鍵值的列從不修改或更新。
④主鍵值不能重用
CREATE TABLE Vendors
(
Vend_id CHAR(10) NOT NULL PRIMARYKEY,
Vend_name CHAR(50) NOT NULL,
Vend_address CHAR(50) NULL,
Vend_city CHAR(5) NULL,
Vend_state CHAR(10) NULL,
Vend_zip CHAR(10) NULL,
Vend_country CHAR(50) NULL
);
ALTER TABLE Vendors
ADD CONSTRAINT PRIMARY KEY (vend_id);
#給表vend_id 列定義添加關(guān)鍵字PRIMARYKEY, 使其成為主鍵
其實(shí)一般用的就是查詢,插入,刪除等語句而已。。。。但學(xué)學(xué)存儲過程是好事。。。以后數(shù)據(jù)方面的東西就不用在程序里搞嘍。。而且程序與數(shù)據(jù)庫只要一個來回通訊就可以搞定所有數(shù)據(jù)的操作。。。.
評論
查看更多