Вы находитесь на странице: 1из 11

sql data retrieval

objectives :
•list oracle9i data-types
•state what is sql
•list sql components
•state the syntax for data retrieval using select statement
•state & demonstrate the use of where clause
•state & demonstrate the use of order by clause
•explain and demonstrate the use of various operators in where
clause
•demonstrate sql*plus environment

oracle data types


•the information in a database in maintained in the form of tables
•each table consists of rows and columns to store the data
•a particular column in a table must contain similar data, which is
of a particular type
•data type decides the characteristics of the information handled by
the column

oracle data types


•character
•numeric
•binary
•date
•row id
•lob
character data types
•char
•varchar2
•varchar
•long
•nchar
•nvarchar2

character data types


•char (n)
–fixed length character data of length n
–default length is 1
–maximum length is 2000 bytes
–when data is entered, any space left over will be filled with blanks
–all alpha-numeric characters are allowed

•varchar2(n)
–variable length character data
–maximum length `n’ must be specified
–maximum length is 4000 bytes
–all alpha-numeric characters are allowed

•varchar (n)
–synonymous with varchar2
–oracle has reserved this data type for future use

•long
–large amounts of variable length text
–any length up to 2 gb can be specified
restrictions :
•only one column per table can be defined as long
•a long column can’t be indexed
•a long column can’t be passed as argument to a procedure or function
•a function can’t be used to return a long column
•a long column can’t be used in where, order by, group by and other
clauses
numeric data types
•number
–the number data type is used to store zero, negative, positive, fixed and
floating point numbers with up to 38 digits of precision having
magnitude between 1.0 * 10 -130 to
9.9….9 * 10 125
number can be defined in three ways :
–number(p, s)
–number(p)
–number

•number(p,s) :
–where p is the precision up to 38 digits
–s is the scale ranging

•number (p) :
–a fixed point number with a scale of 0 and a precision of p

•number :
–a floating point number with a precision of 38
–storage space 21 bytes / row

binary data types


•raw (n)
–variable length row binary data
–maximum `n’ must be specified
–maximum length is 2000 bytes
–data retrieved as hexadecimal character values
–used for data such as sound and images

•long raw
–used to store binary data up to 2 gb
date data type
•date and time data
•storage space 7 bytes / row
•can store data ranging from january 1, 4712 bc to december 31,
4712 ad
•default format is : dd-mon-yy

lob data types


•the lob (large object) data types are used to store large data
•the maximum size is 4 gb
•various lob data types
–blob
–clob
–nclob
–bfile

lob data types


•blob
–used to store large binary data

•clob
–used to store large single byte character data

•nclob
–used to store large nls character data

•bfile
–used with large binary data stored out side the database in an external
file
introduction to sql
•what is sql, sql*plus and pl/sql?
–sql :
•structured query language
•the language used to access the data from rdbms
•non-procedural language

–pl/sql
•oracle’s procedural language extension of sql
•used by oracle tools

–sql* plus :
•powerful oracle support product
•programmer’s tool
•command line interpreter
•used in conjunction with sql and pl/sql

sql * plus
•using sql*plus you can
–enter, edit, store, retrieve and run sql command and pl/sql blocks
–format, perform calculations on, store and print query results
–list column definitions for a table

sql - structured query language


•the language used to access data
•developed in a prototype rdbms, `system r’, by ibm in mid 1970s
•oracle incorporation introduced the first commercially available
implementation of sql in 1979
•ansi adopted sql as a standard language for rdbms, in oct. 86

components of sql
•sql is made of three sub-languages
–data definition language (ddl)
–data manipulation language (dml)
–data control language (dcl)
components of sql
•data definition language (ddl) :
–consists of commands to create the objects such as tables, views,
indexes etc.

•data manipulation language (dml) :


–used for query, insertion, deletion and updating information stored in the
database

•data control language (dcl) :


–used for controlling access to the data

sample data tables


•emp
–employee information

•dept
–department information

data retrieval using sql


•the select statement is used to retrieve data from database tables

•syntax :
select <column_list>
from <table_name>
[where <condition>]
[group by <column_name(s)>]
[having <condition>]
[order by <expression>];

the select statement


•select statement can be used to display
–some or all the columns from a specified table
–some or all of the rows from a specified table
–calculated values from the table
–statistical information, like averages or sums of column values
the select statement
•guidelines for using select statement :
–the select clause followed by from clause are required for any sql query
–not all columns need to be selected
–columns are displayed left to right in the order specified in the select list
–select list items must be separated by commas
–rows are returned in arbitrary manner
–users may query only tables they have created or tables to which they
have access
–use select * from tab;
to see all tables in your account
–use distinct clause to suppress repeating values

examples :
1. list the information of all employees.

2. list the employee names along with their salaries.

3. list the different jobs offered in the company.

sql data retrieval


conditional retrieval of rows:
•the where clause :
–the where clause is used along with the select statement to specify the
condition, based on which the rows will be extracted from a table
–operators used to specify conditions:
•relational operators
•logical operators
•special operators

•relational operators :
= equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
<>, !=,^= not equal to

•logical operators
and logical and
or logical or
not logical not

examples : where clause


1. list the employees belonging to the department number 20.
2. list the name and salary of the employees, whose salaries are more than
1000.
3. list the employee number and name of the employee whose job is
manager.
4. list the names of clerks working in the department number 20.
5. list the employee name and salary for employees whose salary is
between 1000 and 2000.
6. list the names of analysts and salesmen.
7. list the names of employees who are not managers.

special operators:
•special operators :
in checking value in a set
between checking value within a range
is nullchecking for null value
like matching pattern from a column
|| concatenation of strings

special operators: in
•in operator :
–the in operator can be used to select rows that match one of the values
included in the list
•examples :
1. list the name of the employees whose employee numbers are 7369,
7521, 7839, 7934, 7788.
2. list the employees not belonging to department numbers 30 and 10.

special operators: between


–between operator
–between operator is used to select values that are within or outside a
range of values
–between is an inclusive operator
–not between is an exclusive operator
–used with numbers or dates

•examples :
1. list the employee name and salary, whose salary is in the range 1000
to 2000.

2. list employee names, who have joined before 30 june 81 and after
december 81.

special operators: is null


•working with null values
–null values are different from the value 0 or blank
–it represents an unknown or inapplicable value
–it cannot be compared using relational and / or logical operators
–the special operator is null is used to locate null values

•examples :
1. list the employee names, who are not eligible for commission.

2. list the name and job of the employees, who do not report to
anybody.

special operators: like


–like operator is used for matching a pattern with a column from a table
–like operator is used only with char and varchar2 to match a pattern
–wildcards used are :
% percentage any sequence of zero or more characters
_ underscore any single character
\ escape to include the underscore or percentage itself
in a pattern

•examples :
1. list the employees whose name start with an `s’.

2. list the employee names having third character as `r’.

3. list the names of employees whose names have exactly five


characters.

ordering the results:


order by clause
•the results of a query can be ordered specifically using the order by clause
in a select statement
•order by col|expr [asc|desc], …
•one or more columns and / or expressions can be specified
•you can also use integers that represent the position of a column in the
select column list
•orderby clause must be the last clause in a select query
•columns that are not in select column list may still appear in order
by clause
•rows with null appears at the beginning or end

examples: order by clause


1. list the employee details in the ascending order of their salary.

2. list the details of employees in the department wise ascending order and
in the descending order of salary.

3. write the same query using relative position of columns.

Вам также может понравиться