合并为两层子查询 联结join 关系表 创建联结 对表使用别名 -自联结(多次使用相同的表) 使用联结实现 外联结实现 聚集+外联结实现 union实现 默认值经常用于日期或时间戳列 事务处理 控制事务处理 回退语句 使用保留点
23.高级SQL特性
SQL必知必会+MySQL实践学习记录(下)
11.分组数据
select count(*) as num_prod from Products where vend_id = "DLL01";
位置:在where子句后,order by子句后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;
一般在使用group by子句时,应该也给出order by子句,保证数据正确排序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
12.使用子查询
作为子查询的select语句只能查询单个列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;
13.联结表
各表中只主键重复,其他数据只记录一次
更有效地存储、更方便地处理、可伸缩性好select vend_name, prod_name, prod_price from Vendors, Products where Vendors.vend_id = Products.vend_id;
没有where给出的联结条件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";
14.创建高级联结
对列名和计算字段使用别名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;
15.组合查询
where子句实现select cust_name, cust_contact, cust_email from Customers where cust_state in ("IL", "IN", "MI") or 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子句,对所有结果排序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;
16.插入数据
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);
省略行允许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;
17.更新和删除数据
update Customers set cust_contact = "Sam Robert", cust_email = "sam@toyland.com" where cust_id = "1000000006";
update Customers set cust_email = NULL where cust_id = "1000000005";
delete from Customers where cust_id = "100000006";
18.创建和操作表
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 );
只有不允许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
19.使用视图
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;
20.使用存储过程
create procedure productpricing() select avg(prod_price) as priceaverage from Products;
call productpricing();
drop procedure productpricing;
21.管理事务处理
事务 一组SQL语句
回退 撤销指定SQL语句的过程
提交 将未存储的SQL语句结果写入数据库表
保留点 事务处理中设置的临时占位符,可以对它发布回退
标识事务的开始语句start transaction;
delete from Orders; rollback T0 delete1;
savepoint detele1;
22.使用游标
DECLARE CustCursor CURSOR FOR SELECT * FROM Customers WHERE cust_email IS NULL
OPEN CURSOR CustCursor
23.高级SQL特性
管理如何插入或处理数据库数据的规则
使用PRIMARY KEY定义主键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);
24.补充
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 起始的条目索引,条目数; ⑨
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算