列的重命名
视图继承了已有列的名字此外视图还可以有自己的名字SQL 的CREATE VIEW 允
许你对所选择的列进行重命名它与前边的例子非常相似如果你想把COMPANY 中的
ADDRESS CITY 和STATE 字段组合起来并打印到信封上时该如何做呢请看下边的例
子它使用了SQL 的+操作符将地址字段与逗号和空格组合起来
INPUT
SQL> CREATE VIEW ENVELOPE (COMPANY, MAILING_ADDRESS) AS
2 SELECT NAME, ADDRESS + " " + CITY + ", " + STATE
3 FROM COMPANY;
SQL> SELECT * FROM ENVELOPE
OUTPUT
COMPANY MAILING_ADDRESS
Phone Company 111 1st Street Atlanta, GA
Power Company 222 2nd Street Jacksonville, FL
Record Club 333 3rd Avenue Los Angeles, CA
Software Company 444 4th Drive San Francisco, CA
Cable TV Company 555 5th Drive Austin, TX
Joe's Car Palace 1000 Govt. Blvd Miami, FL
S.C. Student Loan 25 College Blvd. Columbia, SC
Florida Water Company 1883 Hwy. 87 Navarre, FL
U-O-Us Insurance Company 295 Beltline Hwy. Macon, GA
Debtor's Credit Card 115 2nd Avenue Newark, NJ
SQL 21 日自学通(V1.0) 翻译人笨猪
199
分析
当在视图中使用SQL 的计算功能时SQL 会要求你给出一个虚字段的名字这是可以
理解的因为像COUNT(*)或AVG(PAYMENT).是不能作为名字的
注检查你的解释器看它是否支持+操作符
SQL 对视图的处理过程
视图可以以比数据在数据库表中的存储情况更为便捷的方式来返回数据当需要连续
进行几个复合的查询时例如在存储过程和应用程序中时视图也是非常方便的为了进
一步地说明视图和SELECT 语句下边的例子分别使用了SQL 的查询方法和视图方法以
作对比假设你需要经常去运行一个查询例如你需要例行公事地将BILLS 表与
BANK_ACCOUNT 表进行归并以得到支付信息
INPUT
SQL> SELECT BILLS.NAME, BILLS.AMOUNT, BANK_ACCOUNTS.BALANCE
2 BLANCE BANK_ACCOUNTS.BANK BANK FROM BILLS, BANK_ACCOUNTS
3 WHERE BILLS.ACCOUNT_ID = BANK_ACCOUNTS.ACCOUNT_ID;
OUTPUT
BILLS.NAME BILLS.AMOUNT BALANCE BANK
Phone Company 125 500 First Federal
Power Company 75 500 First Federal
Record Club 25 1200 First Investor's
Software Company 250 500 First Federal
Cable TV Company 35 90 Credit Union
Joe's Car Palace 350 2500 Second Mutual
S.C. Student Loan 200 4500 Fidelity
Florida Water Company 20 500 First Federal
U-O-Us Insurance Company 125 2500 Second Mutual
这一过程用视图来表达则语句如下
INPUT/OUTPUT
SQL> CREATE VIEW BILLS_DUE (NAME, AMOUNT, ACCT_BALANCE, BANK) AS
2 SELECT BILLS.NAME, BILLS.AMOUNT, BANK_ACCOUNTS.BALANCE
3 BANK_ACCOUNTS.BANK FROM BILLS, BANK_ACCOUNTS
SQL 21 日自学通(V1.0) 翻译人笨猪
200
4 WHERE BILLS.ACCOUNT_ID = BANK_ACCOUNTS.ACCOUNT_ID
View created.
如果你对BILLS_DUE 视图执行查询是使用了一些条件如下语句所示
INPUT/OUTPUT
SQL> SELECT * FROM BILLS_DUE
2 WHERE ACCT_BALANCE > 500
NAME AMOUNT ACCT_BALANCE BANK
Record Club 25 1200 FirstInvestor's
Joe's Car Palace 350 2500 SecondMutual
S.C. Student Loan 200 4500 Fidelity
U-O-Us Insurance Company 125 2500 SecondMutual
分析
在上述语句中SQL 执行了好几步操作因为BILLS_DUE 是一个视图不是一个真实
的表SQL 首先查找一个名字叫BILLS_DUE 的表但是没有找到SQL 的过程可能会从
系统表中发现BILLS_DUE 原来是一个视图这依据你所使用的数据库而定于是它对视
图进行了诠释并形成了如下的查询语句
SQL> SELECT BILLS.NAME, BILLS.AMOUNT, BANK_ACCOUNTS.BALANCE,
2 BANK_ACCOUNTS.BANK FROM BILLS, BANK_ACCOUNTS
3 WHERE BILLS.ACCOUNT_ID = BANK_ACCOUNTS.ACCOUNT_ID
4 AND BANK_ACCOUNTS.BALANCE > 500;
例10.1
构造一个视图以显示所有需要发送账单的州同时要求显示每个州的账单金额总数和
账单的总数
首先你知道CREATE VIEW 语句看起来应该是下边的样子
CREATE VIEW EXAMPLE (STATE, TOTAL_BILLS, TOTAL_AMOUNT) AS...
现在你必须决定SELECT 语句的内容你要清楚根据需要你要选择STATE 字段并应
该使用SELECT DISTINCT 语法以显示账单需要发送的州例如
INPUT
SQL> SELECT DISTINCT STATE FROM COMPANY;
OUTPUT
STATE GA FL CA TX SC NJ 为节约宽度这里用制表符取代了段落标记
SQL 21 日自学通(V1.0) 翻译人笨猪
201
除了选择州字段以外你还需要知道发往这个州的账单总数因此你需要归并BILLS
表和COMPANY表
INPUT/OUTPUT
SQL> SELECT DISTINCT COMPANY.STATE, COUNT(BILLS.*) FROM BILLS, COMPANY
2 GROUP BY COMPANY.STATE
3 HAVING BILLS.NAME = COMPANY.NAME;
STATE COUNT(BILLS.*)
GA 2
FL 3
CA 2
TX 1
SC 1
NJ 1
现在你已经成功地返回了需要的三分之二的结果你可以用下边的语句来最终结束这
条语句加入SUM 语句以返回每个州的金额总数
INPUT/OUTPUT
SQL> SELECT DISTINCT COMPANY.STATE, COUNT(BILLS.NAME), SUM(BILLS.AMOUNT)
2 FROM BILLS, COMPANY
3 GROUP BY COMPANY.STATE
4 HAVING BILLS.NAME = COMPANY.NAME
STATE COUNT(BILLS.*) SUM(BILLS.AMOUNT)
GA 2 250
FL 3 445
CA 2 275
TX 1 35
SC 1 200
NJ 1 35
最后一步你可以将它与最初的CREATE VIEW 语句组合在一起
INPUT/OUTPUT
SQL> CREATE VIEW EXAMPLE (STATE, TOTAL_BILLS, TOTAL_AMOUNT) AS
2 SELECT DISTINCT COMPANY.STATE, COUNT(BILLS.NAME),SUM(BILLS.AMOUNT)
3 FROM BILLS, COMPANY
SQL 21 日自学通(V1.0) 翻译人笨猪
202
4 GROUP BY COMPANY.STATE
5 HAVING BILLS.NAME = COMPANY.NAME;
INPUT/OUTPUT
SQL> SELECT * FROM EXAMPLE;
STATE TOTAL_BILLS TOTAL_AMOUNT
GA 2 250
FL 3 445
CA 2 275
TX 1 35
SC 1 200
NJ 1 35
上边的例子向你展示了如何设计CREATE VIEW 语句和SELECT 语句SELECT 语句
测试代码的作用在于检查数据的返回结果是不是符合你的要求然后才将其最终组合来创
建一个视图
例10.2
假设你的债权人因为你推迟付款加收10%的服务费而且不幸的是你在这个月的每件
事都需要推迟因此你想看一下需要推迟付款的债主的账号
归并语句在这里是非常简单的因为你不需要使用像SUM 或COUNT 之类的语句
可是你会第一次发现使用视图的好处在视图中你可以将增加的10%的服务费在视图中作
为一个字段由于这一点你可以在视图中使用SELECT 语句来为你计算总计的结果语
句如下
INPUT
SQL> CREATE VIEW LATE_PAYMENT (NAME, NEW_TOTAL, ACCOUNT_TYPE) AS
2 SELECT BILLS.NAME, BILLS.AMOUNT * 1.10, BANK_ACCOUNTS.TYPE
3 FROM BILLS, BANK_ACCOUNTS
4 WHERE BILLS.ACCOUNT_ID = BANK_ACCOUNTS.ACCOUNT_ID;
OUTPUT
View created.
INPUT/OUTPUT
SQL> SELECT * FROM LATE_PAYMENT
SQL 21 日自学通(V1.0) 翻译人笨猪
203
NAME NEW_TOTAL ACCOUNT_TYPE
Phone Company 137.50 Checking
Power Company 82.50 Checking
Record Club 27.50 MoneyMarket
Software Company 275 Checking
Cable TV Company 38.50 Checking
Joe's Car Palace 385 Checking
S.C. Student Loan 220 Business
Florida Water Company 22 Checking
U-O-Us Insurance Company 137.50 Business
Debtor's Credit Card 38.50 Savings