版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Writing Basic SQL StatementsObjectivesAfter completing this lesson, you should be able to do the following:List the capabilities of SQL SELECT statementsExecute a basic SELECT statementDifferentiate between SQL statements and SQL*Plus commandsCapabilities of SQL SELECT StatementsSelectionProjectionT
2、able 1Table 2Table 1Table 1JoinBasic SELECT StatementSELECTDISTINCT *, column alias,.FROMtable;SELECT identifies what columnsFROM identifies which tableWriting SQL StatementsSQL statements are not case sensitive. SQL statements can be on one ormore lines.Keywords cannot be abbreviated or split acros
3、s lines.Clauses are usually placed onseparate lines.Tabs and indents are used to enhance readability. Selecting All Columns DEPTNO DNAME LOC- - - 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTONSQL SELECT * 2 FROM dept; Selecting Specific Columns DEPTNO LOC- - 10 NEW Y
4、ORK 20 DALLAS 30 CHICAGO 40 BOSTONSQL SELECT deptno, loc 2 FROM dept;Column Heading DefaultsDefault justificationLeft: Date and character dataRight: Numeric dataDefault display: UppercaseArithmetic ExpressionsCreate expressions on NUMBER and DATE data by using arithmetic operators.Operator+-* / Desc
5、riptionAddSubtract Multiply Divide Using Arithmetic Operators SQL SELECT ename, sal, sal+300 2 FROMemp;ENAME SAL SAL+300- - -KING 5000 5300BLAKE 2850 3150CLARK 2450 2750JONES 2975 3275MARTIN 1250 1550ALLEN 1600 1900.14 rows selected.Operator PrecedenceMultiplication and division take priority over a
6、ddition and subtraction.Operators of the same priority are evaluated from left to right.Parentheses are used to force prioritized evaluation and to clarify statements.*/+_ Operator PrecedenceSQL SELECT ename, sal, 12*sal+100 2 FROM emp;ENAME SAL 12*SAL+100- - -KING 5000 60100BLAKE 2850 34300CLARK 24
7、50 29500JONES 2975 35800MARTIN 1250 15100ALLEN 1600 19300.14 rows selected. Using ParenthesesSQL SELECT ename, sal, 12*(sal+100) 2 FROM emp;ENAME SAL 12*(SAL+100)- - -KING 5000 61200BLAKE 2850 35400CLARK 2450 30600JONES 2975 36900MARTIN 1250 16200.14 rows selected. Defining a Null ValueA null is a v
8、alue that is unavailable, unassigned, unknown, or inapplicable.A null is not the same as zero or a blank space.SQL SELECT ename, job, comm 2 FROMemp;ENAME JOB COMM- - -KING PRESIDENTBLAKE MANAGER.TURNER SALESMAN 0.14 rows selected. Null Values in Arithmetic ExpressionsArithmetic expressions containi
9、ng a null value evaluate to null.SQL select ename NAME, 12* m 2 from emp 3 WHERE ename=KING;NAME 12* M - -KINGDefining a Column AliasRenames a column headingIs useful with calculationsImmediately follows column name; optional AS keyword between column name and aliasRequires double quotation marks if
10、 it contains spaces or special characters or is case sensitive Using Column Aliases SQL SELECT ename AS name, sal salary 2 FROM emp;NAME SALARY- -.SQL SELECT ename Name, 2 sal*12 Annual Salary 3 FROM emp; Name Annual Salary- -.Concatenation OperatorConcatenates columns or character strings to other
11、columns Is represented by two vertical bars (|)Creates a resultant column that is a character expressionUsing the Concatenation OperatorSQL SELECTename|job AS Employees 2 FROM emp;Employees-KINGPRESIDENTBLAKEMANAGERCLARKMANAGERJONESMANAGERMARTINSALESMANALLENSALESMAN.14 rows selected.Literal Characte
12、r StringsA literal is a character, expression, or number included in the SELECT list.Date and character literal values must be enclosed within single quotation marks.Each character string is output once for each row returned.Using Literal Character StringsEmployee Details-KING is a PRESIDENTBLAKE is
13、 a MANAGERCLARK is a MANAGERJONES is a MANAGERMARTIN is a SALESMAN.14 rows selected.SQL SELECT ename| |is a| |job 2 AS Employee Details 3 FROM emp; Duplicate RowsThe default display of queries is all rows, including duplicate rows.SQL SELECT deptno 2 FROM emp; DEPTNO- 10 30 10 20.14 rows selected. E
14、liminating Duplicate RowsEliminate duplicate rows by using the DISTINCT keyword in the SELECT clause.SQL SELECT DISTINCT deptno 2 FROM emp; DEPTNO- 10 20 30 SQL and SQL*Plus InteractionSQL*PlusSQL StatementsBufferSQL StatementsServerQuery ResultsSQL*Plus CommandsFormatted ReportSQL Statements Versus
15、 SQL*Plus Commands SQLstatementsSQL A languageANSI standardKeyword cannot be abbreviatedStatements manipulate data and table definitions in the databaseSQL*PlusAn environmentOracle proprietaryKeywords can be abbreviatedCommands do not allow manipulation of values in the databaseSQLbufferSQL*Pluscomm
16、andsSQL*PlusbufferLog in to SQL*Plus.Describe the table structure.Edit your SQL statement.Execute SQL from SQL*Plus.Save SQL statements to files and append SQL statements to files.Execute saved files.Load commands from file to bufferto edit.Overview of SQL*PlusLogging In to SQL*PlusFrom Windows envi
17、ronment: From command line: sqlplus username/password databaseDisplaying Table StructureUse the SQL*Plus DESCRIBE command to display the structure of a table.DESCRIBE tablenameDisplaying Table StructureSQL DESCRIBE deptName Null? Type- - -DEPTNO NOT NULL NUMBER(2)DNAME VARCHAR2(14)LOC VARCHAR2(13)SQL*Plus Editing CommandsAPPEND textCHANGE / old / newCHANGE / text /CLEAR BUFFERDELDEL nDEL m nSQL*Plus Editing CommandsINPUTINPUT textLISTLIST nLIST m n RUN n n text0 textSQL*Plus File CommandsSAVE filenameGET filenameSTART filename filenameEDIT filenameSPOOL filenameSummaryUse SQ
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论