select count(*) as num_prod from Products where vend_id = "DLL01";
select vend_id,count(*) as num_prod from Products group by vend_id;
select cust_id, count(*) as orders from Orders group by cust_id having count(*)>=2;
select vend_id, count(*) as num_prods
from Products
where prod_price>=4
group by vend_id
having count(*)>=2;
select order_num, count(*) as items
from OrderItems
group by order_num
having count(*)>=3
order by items, order_num;
select -> from -> where -> group by -> having ->order by
select order_num from OrderItems where prod_id = "RGAN01";
select cust_id from Orders where order_num in (20007, 20008);
合并为两层子查询
select cust_id
from Orders
where order_num in (select order_num
from OrderItems
where prod_id = "RGAN01");
select cust_name, cust_contact from Customers where cust_id in ("1000000004", "1000000005");
select cust_name, cust_contact
from Customers
where cust_id in (select cust_id
from Orders
where order_num in (select order_num
from OrderItems
where prod_id = "RGAN01"));
select count(*) as orders from Orders where cust_id = "1000000001";
select Orders.cust_id, count(*) from Orders, Customers where Orders.cust_id = Customers.cust_id group by Orders.cust_id;
select cust_name,
cust_state,
(select count(*)
from Orders
where Orders.cust_id = Customers.cust_id) as orders
from Customers
order by cust_name;
联结join
关系表
各表中只主键重复,其他数据只记录一次
更有效地存储、更方便地处理、可伸缩性好
创建联结
select vend_name, prod_name, prod_price
from Vendors, Products
where Vendors.vend_id = Products.vend_id;
select vend_name, prod_name, prod_price from Vendors, Products;
select vend_name, prod_name, prod_price
from Vendors inner join Products
on Vendors.vend_id = Products.vend_id;
select prod_name, vend_name, prod_price, quantity
from OrderItems, Products, Vendors
where Products.vend_id = Vendors.vend_id
and OrderItems.prod_id = Products.prod_id
and order_num = 20007;
select cust_name, cust_contact
from Customers, Orders, OrderItems
where Customers.cust_id = Orders.cust_id
and OrderItems.order_num = Orders.order_num
and prod_id = "RGAN01";
select rtrim(vend_name) + " (" + rtrim(vend_country) +")"
as vend_title
from Vendors
order by vend_name;
对表使用别名
select cust_name, cust_contact
from Customers as C, Orders as O, OrderItems as OI
where C.cust_id = O.cust_id
and OI.order_num = O.order_num
and prod_id = "RGAN01";
-自联结(多次使用相同的表)
使用子查询实现
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";
select C.*, O.order_num, OI.prod_id, OI.quantity, OI.item_price
from Customers as C, Orders as O, OrderItems as OI
where C.cust_id = O.cust_id
and OI.order_num = O.order_num
and prod_id = "RGAN01";
select Customers.cust_id, Orders.order_num
from Customers inner join Orders
on Customers.cust_id = Orders.cust_id;
外联结实现
select Customers.cust_id, Orders.order_num
from Customers left outer join Orders
on Customers.cust_id = Orders.cust_id;
select Customers.cust_id,
count(Orders.order_num) as num_ord
from Customers inner join Orders
on Customers.cust_id = Orders.cust_id
group by Customers.cust_id;
聚集+外联结实现
select Customers.cust_id,
count(Orders.order_num) as num_ord
from Customers left outer join Orders
on Customers.cust_id = Orders.cust_id
group by Customers.cust_id;
select cust_name, cust_contact, cust_email from Customers where cust_state in ("IL", "IN", "MI") or cust_name = "Fun4All";
union实现
select cust_name, cust_contact, cust_email
from Customers
where cust_state in ("IL", "IN", "MI")
union
select cust_name, cust_contact, cust_email
from Customers
where cust_name = "Fun4All";
select cust_name, cust_contact, cust_email from Customers where cust_state in ("IL", "IN", "MI") union select cust_name, cust_contact, cust_email from Customers where cust_name = "Fun4All" order by cust_name, cust_contact;
insert into Customers(cust_id,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email)
values("1000000006",
"Toy Land",
"123 Any Street",
"New York",
"NY",
"11111",
"USA",
NULL,
NULL);
insert into Customers(cust_id,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country)
values("1000000006",
"Toy Land",
"123 Any Street",
"New York",
"NY",
"11111",
"USA");
insert into Customers(cust_id,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email)
select cust_id,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email
from CustNew;
create table CustCopy as
select * from Customers;
update Customers
set cust_contact = "Sam Robert",
cust_email = "[email protected]"
where cust_id = "1000000006";
update Customers
set cust_email = NULL
where cust_id = "1000000005";
delete from Customers where cust_id = "100000006";
CREATE TABLE Products
(
prod_id char(10) NOT NULL ,
vend_id char(10) NOT NULL ,
prod_name char(255) NOT NULL ,
prod_price decimal(8,2) NOT NULL ,
prod_desc text NULL
);
CREATE TABLE OrderItems
(
order_num int NOT NULL ,
order_item int NOT NULL ,
prod_id char(10) NOT NULL ,
quantity int NOT NULL DEFAULT 1,
item_price decimal(8,2) NOT NULL
);
默认值经常用于日期或时间戳列
default current_date()
alter table Vendors add vend_phone char(20);
alter table Vendors drop vend_phone;
drop table CustCopy;
rename
select cust_name, cust_contact
from Customers,Orders, OrderItems
where Customers.cust_id = Orders.cust_id
and OrderItems.order_num = Orders.order_num
and prod_id = "RGAN01";
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 cust_name, cust_contact
from ProductCustomers
where prod_id = "RGAN01";
create view VendLocation as
select concat(vend_name, " (", vend_country, ")") as vend_title
from Vendors;
select * from VendLocation;
create view CustomerEmailList as
select cust_id, cust_name, cust_email
from Customers
where cust_email is not null;
select * from CustomerEmailList;
create view OrderItemExpanded as
select order_num,
prod_id,
quantity,
item_price,
quantity*item_price as expanded_price
from OrderItems;
select * from OrderItemExpanded where order_num = 20008;
create procedure productpricing()
select avg(prod_price) as priceaverage
from Products;
call productpricing();
drop procedure productpricing;
事务处理
事务 一组SQL语句
回退 撤销指定SQL语句的过程
提交 将未存储的SQL语句结果写入数据库表
保留点 事务处理中设置的临时占位符,可以对它发布回退
控制事务处理
标识事务的开始语句
start transaction;
回退语句
delete from Orders;
rollback T0 delete1;
使用保留点
savepoint detele1;
DECLARE CustCursor CURSOR
FOR
SELECT * FROM Customers
WHERE cust_email IS NULL
OPEN CURSOR CustCursor
CREATE TABLE Vendors
(
vend_id char(10) NOT NULL PRIMARY KEY,
vend_name char(50) NOT NULL ,
vend_address char(50) NULL ,
vend_city char(50) NULL ,
vend_state char(5) NULL ,
vend_zip char(10) NULL ,
vend_country char(50) NULL
);
ALTER TABLE Vendors
ADD CONSTRAINT PRIMARY KEY (vend_id);
CREATE TABLE Orders
(
order_num int NOT NULL PRIMARY KEY,
order_date datetime NOT NULL ,
cust_id char(10) NOT NULL REFERENCES
Customers(cust_id)
);
ALTER TABLE Orders
ADD CONSTRAINT
FOREIGN KEY (cust_id) REFERENCES Customers (cust_id)
CREATE TABLE OrderItems
(
order_num int NOT NULL ,
order_item int NOT NULL ,
prod_id char(10) NOT NULL ,
quantity int NOT NULL CHECK (quantuty > 0),
item_price decimal(8,2) NOT NULL
);
ADD CONSTRAINT CHECK (gender LIKE "[MF]")
CREATE INDEX prod_name_ind
ON Products (prod_name);
SELECT 查询列表
FROM 表1
【联结类型】 JOIN 表2
ON 联结条件
WHERE 筛选条件
GROUP BY 分组列表
HAVING 分组后的筛选条件
ORDER BY 排序字段
LIMIT 起始的条目索引,条目数;
SELECT 查询列表 ⑦
FROM 表1 ①
【联结类型】 JOIN 表2 ②
ON 联结条件 ③
WHERE 筛选条件 ④
GROUP BY 分组列表 ⑤
HAVING 分组后的筛选条件 ⑥
ORDER BY 排序字段 ⑧
LIMIT 起始的条目索引,条目数; ⑨