9.分组数据
SELECT vend_id,COUNT(*) AS num_prods
FROMProducts
GROUP BY vend_id;
#创建分组
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分组数据,HAVING子句过滤计数为2或2以上的分组。
SELECT order_num,COUNT(*) AS items
FROMOrderItems
GROUP BY order_num
HAVING COUNT(*) 》= 3
ORDER BY items, order_num;
#按订购物品的数目排序输出
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.联结表
SELECT vend_name, prod_name, prod_price
FROMVendors, Products
WHERE Vendors vend_id = Products.vend_id;
#创建联结
SELECT vend_name, prod_name, prod_price
FROMVendorsINNER JOIN Products
ONVendors.vend_id = Products.vend_id;
#内联结
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;
#联结多个表
12.创建高级联结
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’;
#自联结,此查询中需要的两个表实际上是相同的表
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’;
#自然联结排除多次出现,使每一列只返回一次
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;
#检索两个表中的所有行并关联那些可以关联的行
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允许执行多个查询,并将结果作为一个查询结果集返回
14.插入数据
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;
#从一个表复制到另一个表中
15.更新和删除数据
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’;
#删除数据
16. 创建和操纵表
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的列,其数据类型为CHAR
ALTER TABLE Vendors
DROP COLUMN vend_phone;
#该表中的某列
DROP TABLE CustCopy;
#删除表
17.高级SQL特性
主键:表中一列(或多个列)的值唯一标识表中的每一行。主键是一种特殊的约束,用来保证一列或一组列的值唯一标识表中的每一行。这方便直接或交互地处理表中的行。没有主键,要安全地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 列定义添加关键字PRIMARYKEY, 使其成为主键
其实一般用的就是查询,插入,删除等语句而已。。。。但学学存储过程是好事。。。以后数据方面的东西就不用在程序里搞喽。。而且程序与数据库只要一个来回通讯就可以搞定所有数据的操作。。。.
评论
查看更多