【转】SQL学习(一)
时间:2011-03-06 来源:丨灬風塵一丬
CREATE TABLE tablename(colname datatype [NOT NULL]
{,colname datatype [NOT NULL]...}
[,PRIMARY KEY (colname {,colname...})];
注: |,[] ,{}, ...在sql里不会出现,所以用其标记
二 简单的select语句:
select distinct ordno,x.cid,x.aid,x.pid,
.40*(x.qty*p.price)-.01*(c.dicnt+a.ercent)*(x.qty*p.price)as heji
from orders as x,customers as c,agents as a,products as p
where c.cid=x.cid and a.aid= x.aid and p.pid=x.aid;
x,c,a,p分别为orders,customers,agents,products的别名,关键字as可省略
ordno,cid, aid, pid ,heji为列名
heji 前面的as不可省略,他定义.40*(x.qty*p.price)-.01*(c.dicnt+a.ercent)*(x.qty*p.price)显示为heji
distinct确保检索后的每一行是唯一的
找出至少被两个顾客订购的产品的pid值
select distinct x1.pid
from orders x1,orders x2
where x1.pid=x2.pid and x1.pid<x2.pid;,
1. IN谓词IN谓词的通用表达式:expr in (Subquery)|expr in (val {,val...})
例: 检索由住在Duluth的顾客和住在New York的代理商组成的所有订货记录的ordno值
select ordno from orders
where (cid,aid) in
(select cid,aid from customers c,agents a where c.city='Duluth' and a.city='New York');
NOT IN谓词为当且仅当求解出的expr值不在子查询返回的集合中[expr NOT IN (Subquery)]
2. 量化比较谓词
通用形式 expr θ{SOME|ANY|ALL} (Subquery) where θ is some operator in the set {<,<=,=,<>,>,>=}
该形式中的SOME与ANY含义相同,θ包含:{<,<=,=,<>,>,>=} <>是不等于
例:找出佣金百分率最小的代理商的aid值
select aid from agents where percent <=all (select percent from agents);
注:谓词=SOME与谓词IN具有完全相同的效果
谓词<>all与NOT IN等价
谓词<>SOME不与 NOT IN等价
3. 谓词EXISTS
通用形式 [NOT] EXISTS (Subquery) 注:EXISTS (Subquery) 为真当且仅当子查询返回一个非空集合
例:求出订购了产品P01又订购了产品P07的顾客的cid值
select distinct cid from orders x
where pid='p01' and exists
(select * from orders where cid=x.cid and pid='p07');
NOT EXISTS 查询效果可用 NOT IN和等价谓词<>ALL替代
4. SQL查询会有很多等价形式
例:检索订购了产品p01的顾客所在的city名这一请求的表达式,有四种主要的Select语句表达式
select distinct city from customers where cid in
(select cid from orders where pid='p01');
select distinct city from customers where cid =any
(select cid from orders where pid='p01');
select distinct city from customers c where exists
(select * from oredrs o where c.cid=o.cid and o.pid='p01');
select distinct city from customers c, orders o
where c.cid=o.cid and o.pid='p01';
1. UNION 运算符
实质就是关系运算中的并
例:建立一个包含了顾客所在的或者代理商所在的或者两者接在的城市的名单
select city from customers
union select city from agents;
当设计到的子查询项目大于或等于3时可用()
(select city from customers
union select city from agents)
union all select city from products;
2. 除法: SQL "FOR ALL..."条件
如果我们所面临的查询要求被检索的对象集合必须符合某个带有“所有”这类关键词的条件,我们就按照下列步骤进行
1) 为要检索的对象命名并考虑如何用英文来表述要检索的候选对象的一个反例。在该反例中,前面提到的“所有”
对象中有至少一个对象不符合规定的条件。
2) 建立Select语句的搜索条件以选出步骤1所创建的所有反例。(步骤1和2必定会引用来自外部Select语句的对象,
所以我们要在如何用这些外部对象所在的表来引用他们这一问题上表现出一定的灵活性)
3) 建立包含步骤2所创建的语句的搜索条件,说明不存在上面定义的那种反例。这里将涉及到NOT EXISTS谓词
4) 用步骤3的搜索条件来建立最终的Select语句,检索所期望的对象。
上面的步骤序列通常会产生如下所示的嵌套子查询对:
select ... where not exists (select ... where not exists (select ... ));
例:找出具有下列性质的顾客的cid值:如果顾客c006订购了某种产品,那要检索的顾客也订购了该产品。
此题可以改写成:找出订购了所有被顾客c006订购的产品的cid值。
步骤1:我们称c.cid是符合题意的顾客并用英文构造反例
"There is a product ordered by customer c006 that is not ordered by c.cid."
步骤2:把英文表述成搜索条件,我们将被c006订购的产品命名为p.pid
cond1: p.pid in (select pid from orders x where x.cid='c006')
and not exists (select * from orders y
where y.pid=p.pid and y.cid=c.cid )
步骤3:j建立表示这种反例不存在的条件:
cond2: not exists (select p.pid from prducts p
where p.pid in (select pid from orders x
where x.cid='c006')and
not exists (select * from orders y
where y.pid=p.pid and y.cid=c.cid))
步骤4:建立最终的Select
select cid from customers c
where not exists (select p.pid from prducts p
where p.pid in (select pid from orders x
where x.cid='c006')and
not exists (select * from orders y
where y.pid=p.pid and y.cid=c.cid));
该select的一个变体是:
select cid from customers c
where not exists (select z.pid from oredrs z
where z.cid='c006' and
not exists (select * from orders y
where y.pid=z.pid and y.cid = c.cid));
3.表别名用法
例:检索对同一产品至少订购了两次的所有顾客的名字。
select cname from (select o.cid as spcid from orders o,oreders x where o.cid=x.cid
and o.pid=x.pid and o.ordno<>x.o.ordon)y, customers c
where y.spcid=c.cid;
该查询中为为子查询的结果指定了别名y,并为子查询所检索的列提供了别名spcid