Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword or section
Like this
15Activity

Table Of Contents

0 of .
Results for:
No results containing your search query
P. 1
DBA Lesson

DBA Lesson

Ratings:

2.0

(1)
|Views: 355 |Likes:
Published by blackwolf23

More info:

Published by: blackwolf23 on Sep 08, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

10/20/2011

pdf

text

original

 
Lesson One - Selecting Data from Oracle
In our first lesson we will cover the following topics:
 
Writing select statements.Handling NULL values.Renaming columns with column aliases.Concatenating columns.Editing SQL queries.Performing arithmetic equations.
Writing SELECT statements
The most common SQL statement executed on an Oracle database is a query or SELECT statement.SELECT statements are used to pull or extract data (information) from the Oracle database. This datais stored in TABLES. The concept of an Oracle table is the same as an excel spreadsheet table, it alsocontains rows and columns.. When SQL*Plus is invoked (run) it first displays the version of SQL*Plusused, the date, the version of the Oracle database used, the version of PL/SQL used and any serveroptions available on the database.eg.SQL*Plus: Release 3.3.3.0.0 - Production on Mon Sep 27 23:38:41 1999Copyright (c) Oracle Corporation 1979, 1996. All rights reserved.Connected to:Oracle7 Server Release 7.3.3.0.0 - Production ReleaseWith the distributed, replication and parallel query optionsPL/SQL Release 2.3.3.0.0 - ProductionSQL>
SQL> select * from students;NAME DATE_OF_B G STUDENT_ID EXAMS GPA------------- --------- - ---------- --------- ---------Smith 04-JUL-65 F 2 2 5.8Green 04-JUL-65 F 3 2 3.3Taylor 01-JAN-77 F 4 1 .8Bane 01-JAN-55 M 5 4 4
Our SELECT statement has selected all the columns and all the rows from the TABLE students. Noticeall we have asked is for the data, it is not necessary to specify how to return the data. Lesson 34 willexplain the tasks that Oracle performs in the background to return the data.The first component of a SELECT statement is the SELECT clause, this tells Oracle that it must nowperform a SELECT statement. The second component of a SELECT statement is the list of columnsthat the user wishes to view. In the above example the "*" wildcard character was used to return allthe columns in the table. The last component of the SELECT statement is the FROM clause, thisspecifies the table to extract the data from. It is also possible to specify the schema or the owner towhich the table belongs to.
Handling NULL values.
1
 
Sometimes a database query on a column will produce a nothing result. With Oracle nothing is referredto as NULL. In set theory NULL represents the value of an empty dataset. There are times when wemay wish to set a default value instead of the NULL value, in this instance we use the function nvl().eg.
SQL> select * from enrolments;SURNAME FNAME AGE G-------------------- -------------------- --------- -Morris Frank 18 NYale Tony 72Brown Mary 71 YWirth Peter 17 NKlawitter Eric 75 YWong Ronnie 726 rows selected.SQL> select surname, fname,age, nvl(graduate,'N') from enrolments;SURNAME FNAME AGE N-------------------- -------------------- --------- -Morris Frank 18 NYale Tony 72 NBrown Mary 71 YWirth Peter 17 NKlawitter Eric 75 YWong Ronnie 72 N6 rows selected.
If the column specified in the nvl is not null then the value in the column is returned, when the column isnull the value of alternative value is returned. The nvl() function can be used on columns of alldatatypes. The syntax for the nvl() function isnvl(column_name,value_if_null)
Renaming columns with column aliases.
As you may have noticed when Oracle returns data to the user, Oracle creates a heading for eachcolumn. The column names correspond to the column names indicated in the SELECT statement. Theproblem is that Oracle will truncate the column name to the width of the column display. The solution tothis problem is to use column aliases in the SELECT statement. Any column can be given another nameby the user when the SELECT statement is issued. This allows the user to use more descriptive name inthe space allowed by the datatype definition.
select surname, fname firstname, age, nvl(graduate,'N') from enrolments;SQL> select surname, fname firstname, age, nvl(graduate,'N') from enrolments;SURNAME FIRSTNAME AGE N-------------------- -------------------- --------- -Morris Frank 18 NYale Tony 72 NBrown Mary 71 YWirth Peter 17 NKlawitter Eric 75 YWong Ronnie 72 N6 rows selected.
2
 
Alternatively you may issue the
as
keyword to denote the alias. This just makes the statement easier toread.select surname, fname as firstname, age, nvl(graduate,'N')from enrolments;
Concatenating columns.
We can do more than just renaming columns and using the nvl() function to modify the output of ourSELECT statements. Columns can be joined together or concatenated to produce more readable output.This concatenation is accomplished using a technique called concatenation. The concatenation operatorlooks like to pipe character put together '||'. Here is an example.
select fname||' '||surname Name,age, nvl(graduate,'N') from enrolments;NAME AGE N----------------------------------------- --------- -Frank Morris 18 NTony Yale 72 NMary Brown 71 YPeter Wirth 17 NEric Klawitter 75 YRonnie Wong 72 N6 rows selected.
Editing SQL Queries.
Sooner or later, in my case sooner you will make a typing mistake with your SQL statements. Not agreat drama is you notice it before you complete the line or if the statement is only two lines, but whatdo you do if you have typed twenty lines before you realise that you have misspelt SELECT on the firstline.SQL*Plus has the ability to perform limited editing of errors. Here is an example.
slect fname||' '||surname Name,age,nvl(graduate,'N')from enrolments;
This is the error that you will see.
SQL> slect fname||' '||surname Name,unknown command beginning "slect fnam..." - rest of line ignored.SQL> age,unknown command "age," - rest of line ignored.SQL> nvl(graduate,'N')unknown command beginning "nvl(gradua..." - rest of line ignored.SQL> from enrolments;
Enter the letter 'l', this will list the contents of the buffer.
SQL> l1 slect fname||' '||surname Name,2 age,3 nvl(graduate,'N')4* from enrolmentsSQL> 11* slect fname||' '||surname Name,
3

Activity (15)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads
madhu_oradba liked this
abusidik liked this
abusidik liked this
jalil1972 liked this
rajsuni39 liked this
ksknrindian liked this
ashok_chankar liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->