您好,欢迎访问三七文档
当前位置:首页 > 财经/贸易 > 资产评估/会计 > 0Ch04advSQL
Source:DatabaseSystemConcepts,Silberschatzetc.20064-1IntroductiontoDatabaseCHAPTER4AdvancedSQLSQLDataTypesandSchemasIntegrityConstraintsAuthorizationEmbeddedSQLDynamicSQLFunctionsandProceduralConstructs**RecursiveQueries**AdvancedSQLFeatures**1-50Source:DatabaseSystemConcepts,Silberschatzetc.20064-2BasicDataTypes:IntegerChar…Build-inDataTypesinSQLUser-DefinedTypesLarge-ObjectTypes4.1SQLDataTypesandSchemasSource:DatabaseSystemConcepts,Silberschatzetc.20064-34.1.1Build-inDataTypesinSQLBuild-inDataTypesinSQL:date:Dates,containinga(4digit)year,monthanddate,‘2005-7-27’time:Timeofday,inhours,minutesandseconds,‘09:00:30’timestamp:dateplustimeofday,‘2005-7-2709:00:30.75’OperationsforBuild-inDataTypesinSQL:Convertstringtypestodate/time/timestamp,caststringasdateFindinterval:periodoftime,dtae1–date2•Intervalvaluescanbeaddedtodate/time/timestampvaluesExtractvaluesofindividualfieldsfromdate/time/timestamp•Example:extract(yearfromr.starttime)Comparison:ifdate1date2then…Source:DatabaseSystemConcepts,Silberschatzetc.20064-44.1.3Large-ObjectTypesLargeobjects(photos,videos,CADfiles,etc.)arestoredasalargeobject:blob:binarylargeobject–•objectisalargecollectionofuninterpretedbinarydata•whoseinterpretationislefttoanapplicationoutsideofthedatabasesystemclob:characterlargeobject–•objectisalargecollectionofcharacterdataWhenaqueryreturnsalargeobject,apointerisreturnedratherthanthelargeobjectitself.Source:DatabaseSystemConcepts,Silberschatzetc.20064-54.2IntegrityConstraintsIntegrityConstraintsguardagainstaccidentaldamagetothedatabase,byensuringthatauthorizedchangestothedatabasedonotresultinalossofdataconsistency.Example:•E.g.1:Acheckingaccountmusthaveabalancegreaterthan$10,000.00•E.g.2:Asalaryofabankemployeemustbeatleast$4.00anhour•E.g.3:Acustomermusthavea(non-null)phonenumberSource:DatabaseSystemConcepts,Silberschatzetc.20064-64.2.1ConstraintsonaSingleRelationnotnullprimarykeyuniquecheck(P),wherePisapredicatecreatetablebranch(branch-namechar(15)notnull,branch-citychar(30),assetsinteger,primarykey(branch-name),check(assets=0))Source:DatabaseSystemConcepts,Silberschatzetc.20064-74.2.2NotNullConstraintE.g.Declarebranch_nameforbranchisnotnullbranch_namechar(15)notnullE.g.DeclarethedomainDollarstobenotnullcreatedomainDollarsnumeric(12,2)notnullcreatetablebranch(branch-namechar(15)notnull,branch-citychar(30),assetsinteger,primarykey(branch-name),check(assets=0))Source:DatabaseSystemConcepts,Silberschatzetc.20064-84.2.3UniqueConstraintAstatement:unique(A1,A2,…,Am)TheuniquespecificationstatesthattheattributesA1,A2,…Amformacandidatekey.Source:DatabaseSystemConcepts,Silberschatzetc.20064-94.2.4Thecheckclausecheck(P),wherePisapredicateExample1:Declarebranch_nameastheprimarykeyforbranchandensurethatthevaluesofassetsarenon-negative.createtablebranch(branch-namechar(15)notnull,branch-citychar(30),assetsinteger,primarykey(branch-name),check(assets=0))Example2:Usecheckclausetoensurethatanhourly_wagedomainallowsonlyvaluesgreaterthanaspecifiedvalue.createdomainhourly_wagenumeric(5,2)constraintvalue_testcheck(value=4.00)•Theclauseconstraintvalue_testisoptionalattributeDomainSource:DatabaseSystemConcepts,Silberschatzetc.20064-104.2.5ReferentialIntegrityReferentialIntegrity:ensuresthatavaluethatappearsinonerelationforagivensetofattributesalsoappearsforacertainsetofattributesinanotherrelation.Example:If“Perryridge”isabranch-nameappearinginoneofthetuplesintheaccountrelation,thenthereexistsatupleinthebranchrelationforbranch“Perryridge”.InSQLcreatetablestatement:Theprimarykeyclause:listsattributesthatcomprisetheprimarykey.Theuniquekeyclause:listsattributesthatcompriseacandidatekey.Theforeignkeyclause:liststheattributesthatcomprisetheforeignkeyandthenameoftherelationreferencedbytheforeignkey.Source:DatabaseSystemConcepts,Silberschatzetc.20064-11ReferentialIntegrity:Examplecreatetableaccount(account-numberchar(10),branch-namechar(15),balanceinteger,primarykey(account-number),foreignkey(branch-name)referencesbranch)3.account1.branchSource:DatabaseSystemConcepts,Silberschatzetc.20064-12ReferentialIntegrityinSQLForeignkeyscanbespecifiedaspartoftheSQLcreatetablestatement:ByusingtheforeignkeyclauseBydefault,aforeignkeyreferencestheprimarykeyattributesofthereferencedtablecreatetableaccount(account-numberchar(10),branch-namechar(15),balanceinteger,primarykey(account-number),foreignkey(branch-name)referencesbranch)Source:DatabaseSystemConcepts,Silberschatzetc.20064-13Example:BankingDatabaseBankingDatabase:consists6relations:1.branch(branch-name,branch-city,assets)2.customer(customer-name,customer-street,customer-only)3.account(account-number,branch-name,balance)4.loan(loan-number,branch-name,amount)5.depositor(customer-name,account-number)6.borrower(customer-name,loan-number)Source:DatabaseSystemConcepts,Silberschatzetc.20064-14Example:BankingDatabase1.branch2.customer客戶(存款戶,貸款戶)5.account存款帳3.depositor存款戶6.loan貸款帳4.borrower貸款戶分公司Source:DatabaseSystemConcepts,Silberschatzetc.20064-15ReferentialIntegrityinSQL:
本文标题:0Ch04advSQL
链接地址:https://www.777doc.com/doc-3812985 .html