|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
缺乏可以共同遵循的行业标准,ASP还处在发展初期,大家对它的理解不同,如产品和服务标准,收费标准等,不利于行业的健康发展。serverbegin:
SQLServerViews
Postedbyscotton2004年11月28日
AnRDBMSusesaviewtocreateavirtualtable.Thecarefuluseofviewscanimprovetheinteractionbetweena.NETapplicationandtheunderlyingdata.InthisarticlewewilldiscussviewsinMicrosoftSQLServer,includingbestpracticesforcreatingandusingviews.
InSQLServeraviewrepresentsavirtualtable.Justlikearealtable,aviewconsistsofrowswithcolumns,andyoucanretrievedatafromaview(sometimesevenupdatedatainaview).Thefieldsintheview’svirtualtablearethefieldsofoneormorerealtablesinthedatabase.Youcanuseviewstojointwotablesinyourdatabaseandpresenttheunderlyingdataasifthedatawerecomingfromasingletable,thussimplifyingtheschemaofyourdatabaseforusersperformingad-hocreporting.Youcanalsouseviewsasasecuritymechanismtorestrictthedataavailabletoendusers.Viewscanalsoaggregatedata(particularlyusefulifyoucantakeadvantageofindexedviews),andhelppartitiondata.Inthisarticlewewilllookatthesedifferenttypesofviewtoseewhenwecantakeadvantageofaviewforourapplication.
SampleView
ThesampledatabaseNorthwindinSQLServerhasanumberofviewsinstalledbydefault.Oneexampleisthe“CurrentProductList”view,shownhere.
SELECT
Product_List.ProductID,Product_List.ProductName
FROM
ProductsASProduct_List
WHERE(Product_List.Discontinued=0)
FrominsideanapplicationwecanissuethefollowingSQLquerytoretrieveasetofrecordsrepresentingactiveproducts.
SELECTProductID,ProductNamefrom[CurrentProductList]
TheviewhascreatedanewvirtualtablebyusingrecordsfromtheProductstableandapplyingasmallpieceoflogic(afilterontheDiscontinuedfield).Youcouldusetheviewinsideofaqueryfromyourapplication,orastoredprocedure,orevenfrominsideanotherview.Viewsareasimplebutpowerfulabstraction.Youcanpushquerycomplexity,likefilterandjoinstatements,intoaviewtopresentasimplermodelofthedatawithoutsacrificingthedatabasedesignorintegrity.
Weoftendescribeaviewasavirtualtablebecausethedatabasedoesnotstoretheviewdata.Instead,whenweretrievedatafromaviewthedatabaseenginerecreatesthedatausingtheSELECTstatementsintheview’sdefinition.Sincethedatabaseonlystoresadefinitionoftheview,andnotthedata,thereisnosignificantcostinspaceforusingaview,althoughthereisanexceptiontothisrulewewilldiscusslaterinthearticle.NotealsothatthedatabaseenginesqueryoptimizercanoftencombinethedefinitionoftheviewwiththeSQLqueriesinteractingwiththeviewtoprovideanefficientqueryplan(inotherwords,thedatabaseenginemightnotneedtoperformtheentireSELECToperationintheviewifitknowstheouterquerywillfilteroutadditionalrecords).
WhenToUseAView
Youneedtohaveagoalinmindwhencreatingaview.Thereareanumberofscenarioswhereyouwillwanttolookforaviewasasolution.
Tohidethecomplexityoftheunderlyingdatabaseschema,orcustomizethedataandschemaforasetofusers.
Tocontrolaccesstorowsandcolumnsofdata.
Toaggregatedataforperformance.
Let’stakealookateachofthesescenarios.
ComplexityandCustomization
Takingcareofcomplexjoinsandfilteringrulesinsideofaviewcanbenefitotherusers.Asanexample,considerthefollowingviewfromtheNorthwinddatabase.
CREATEVIEW"OrderDetailsExtended"AS
SELECT
"OrderDetails".OrderID,
"OrderDetails".ProductID,
Products.ProductName,
"OrderDetails".UnitPrice,
"OrderDetails".Quantity,
"OrderDetails".Discount,
(CONVERT(money,("OrderDetails".UnitPrice*Quantity*(1-Discount)/100))*100)ASExtendedPrice
FROM
Products
INNERJOIN
"OrderDetails"ON
Products.ProductID="OrderDetails".ProductID
Abusinessuserwithanad-hocreportingtoolcantakeadvantageoftheaboveviewinbuildingcustomizedreportstosupporthergoals.Shecanusetheviewtoseeallofthedetailsaboutanorderwithoutfindingthetablestojoinforproductandorderinformation,andwithoutperformingthecalculationforthepricediscount.Notonlydoesthismakethedatabaseeasierfortheenduser,butitalsoallowsaDBAtomakechangestot</p>强大的可扩展性。ASP具有强大的扩展性,可以实现与多种网络、硬件设备的连接:通过专用的通讯线路远程接入企业;通过远程拨号服务器为远程拨号客户提供服务;通过WAP为移动电话互联网客户服务。 |
|