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ù)的操作。。。.
評論
查看更多