您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 数据库原理课件第三章
Chapter3:SQL4.2DatabaseSystemChapter3:SQLDataDefinitionBasicQueryStructureSetOperationsAggregateFunctionsNullValuesNestedSubqueriesComplexQueriesViewsModificationoftheDatabaseJoinedRelations**4.3DatabaseSystem3.1HistoryIBMSequellanguagedevelopedaspartofSystemRprojectattheIBMSanJoseResearchLaboratoryRenamedStructuredQueryLanguage(SQL)ANSIandISOstandardSQL:SQL-86SQL-89SQL-92SQL:1999(languagenamebecameY2Kcompliant!)SQL:2003Commercialsystemsoffermost,ifnotall,SQL-92features,plusvaryingfeaturesetsfromlaterstandardsandspecialproprietaryfeatures.Notallexamplesheremayworkonyourparticularsystem.4.4DatabaseSystem3.2DataDefinitionLanguageTheschemaforeachrelation.Thedomainofvaluesassociatedwitheachattribute.IntegrityconstraintsThesetofindicestobemaintainedforeachrelations.Securityandauthorizationinformationforeachrelation.Thephysicalstoragestructureofeachrelationondisk.Allowsthespecificationofnotonlyasetofrelationsbutalsoinformationabouteachrelation,including:4.5DatabaseSystem1.DomainTypesinSQLchar(n).Fixedlengthcharacterstring,withuser-specifiedlengthn.varchar(n).Variablelengthcharacterstrings,withuser-specifiedmaximumlengthn.Nchar(n),nVarchar(n)(forunicode,2bytecode,1-4000chars)Text.Variablelengthcharacterstrings,1to2GB.=varchar(max)int.Integer(4B,afinitesubsetoftheintegersthatismachine-dependent).smallint.Smallinteger(2B,asubsetoftheintegerdomaintype).Bigint.Biginteger(8B),tinyint(1B),bit(多个位占1B)numeric(p,d).Fixedpointnumber,withuser-specifiedprecisionofpdigits,withddigitstotherightofdecimalpoint.Decimal(p,d).4.6DatabaseSystem1.DomainTypesinSQL(cnt)real,doubleprecision.Floatingpointanddouble-precisionfloatingpointnumbers,withmachine-dependentprecision.float(n).Floatingpointnumber,withuser-specifiedprecisionofatleastndigits.Datatime8B:yyyy.mm.ddh:m:s,smalldatatime4BMoney8BXml(2GB)(不同表单可以存在xml类型的属性字段中)Binary(n)1-8000BImage2GBTimestamp8BSql_variant8016bit,可存储除text、image、timestamp、sql-variantCursor:查询结果的数据集Table:表格式数据4.7DatabaseSystem2.Createdatabase/tabaleCREATEDATABASEdatabase_nameONPRIMARY(NAME=student_dataFILENAME=‘C:\ProgramFiles\Microsofterver\MSSQL.1\MSSQL\Data\student.mdf',SIZE=10MB,MAXSIZE=50MB,FILEGROWTH=10%),--FILEGROWTH=10%LOGON(NAME=student_log,FILENAME=C:\ProgramFiles\MicrosoftSQLServer\MSSQL.1\MSSQL\Data\student.ldf',SIZE=1MB,MAXSIZE=20MB,FILEGROWTH=10%)4.8DatabaseSystemCreateTableConstructAnSQLrelationisdefinedusingthecreatetablecommand:createtabler(A1D1,A2D2,...,AnDn,(integrity-constraint1),...,(integrity-constraintk))risthenameoftherelationeachAiisanattributenameintheschemaofrelationrDiisthedatatypeofvaluesinthedomainofattributeAiExample:createtablebranch(branch_namechar(15)notnull,branch_citychar(30),assetsinteger)Databaseschama4.9DatabaseSystemIntegrityConstraintsinCreateTablenotnullprimarykey(A1,...,An)Example:Declarebranch_nameastheprimarykeyforbranch.createtablebranch(branch_namechar(15),branch_citychar(30),assetsinteger,primarykey(branch_name))primarykeydeclarationonanattributeautomaticallyensuresnotnullinSQL-92onwards,needstobeexplicitlystatedinSQL-894.10DatabaseSystemcreatetablec_s(student_idchar(8),course_nochar(4),gradeinteger,primarykey(student_id,course_no),foreignkey(student_id)referencesstudent(id),foreignkey(course_no)referencescourse(course_no));名字可以不同域必须相同4.11DatabaseSystem3.DropandAlterTableConstructsThedroptablecommanddeletesallinformationaboutthedroppedrelationfromthedatabase.Thealtertablecommandisusedtoadd/dropattributestoanexistingrelation:altertableraddADwhereAisthenameoftheattributetobeaddedtorelationrandDisthedomainofA.Alltuplesintherelationareassignednullasthevalueforthenewattribute.altertablerdropAaltertablescaltercolumnsnochar(4);whereAisthenameofanattributeofrelationrdroptablerdropdatabasestudentDroppingofattributesnotsupportedbymanydatabases4.12DatabaseSystem3.3BasicQueryStructureSQLisbasedonsetandrelationaloperationswithcertainmodificationsandenhancementsAtypicalSQLqueryhastheform:selectA1,A2,...,Anfromr1,r2,...,rmwherePAirepresentsanattributeRirepresentsarelationPisapredicate.Thisqueryisequivalenttotherelationalalgebraexpression.TheresultofanSQLqueryisarelation.))((21,,,21mPAAArrrn4.13DatabaseSystem3.3.1.TheselectClauseTheselectclauselisttheattributesdesiredintheresultofaquerycorrespondstotheprojectionoperationoftherelationalalgebraExample:findthenamesofallbranchesintheloanrelation:selectbranch_namefromloanIntherelationalalgebra,thequerywouldbe:branch_name(loan)NOTE:SQLnamesarecaseinsensitive(i.e.,youmayuseupper-orlower-caseletters.)E.g.Branch_Name≡BRANCH_NAME≡branch_nameSomepeopleuseuppercasewhereverweuseboldfont.4.14DatabaseSystemTheselectClause(Cont.)SQLallowsduplicatesinrelationsaswellasinqueryresults.Toforcetheeliminationofduplicates,insertthekeyworddistinctafterselect.Findthenamesofallbranchesintheloanrelations,andremoveduplicatesselectdistinctbranch_namefromloanThekeywordallspecifiesthatduplicatesnotberemoved.selectallbranch_namefromloan4.15DatabaseSystemTheselectClaus
本文标题:数据库原理课件第三章
链接地址:https://www.777doc.com/doc-4858751 .html