Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

SQL

Last Reviewed By : Bhagavn/Sourajit Seth,Bangalore


Last updated on :23/08/2005

People. Processes. Technology. Results.

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0
OBJECTIVES

• Introduction
• Elements of SQL
• Operators, Functions
• Format Models
• Commands

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 2
Elements of SQL

• Datatypes
• Nulls
• Pseudocolumns
• Comments
• Database Objects

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 3
Datatypes...

• VARCHAR2 (size)
– Variable length character string with max length of “size” bytes.
– min size 1 byte, max size 4000 bytes.
• CHAR (n)
– Fixed length character string of size “n” bytes
– min size 1byte, max size 255 bytes
• NUMBER (p,s)

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 4
Datatypes...

• LONG
– To store variable length character strings containing up to 2
gigabytes.
– A table cannot contain more than one LONG column.
– Cannot appear in integrity constraints.
– Cannot be indexed.

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 5
Datatypes...
• DATE
– To store date & time.(Century, year, month, day, hour, min, second)
– On specifying a date value without time component, the default time
is 12:00:00 am.
– On specifying a date value without a date component, the default
date is first day of the month.

• RAW(size), LONG RAW


– To store byte oriented-data E.g.: Graphics, Sound
– RAW data can only be stored & retrieved.
– String manipulations cannot be performed.

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 6
Datatypes...
• LOB (Large Objects)
– Similar to LONG & LONG RAW types but differ in:
Can declare more than one LOB column in a table.
The LOB locator is stored in the table column, either with or
without the actual LOB value.
On accessing a LOB value, the locator is returned.
DML operations can be performed on LOB data.

• ROWID
– Each row in the database has an address.
– Values of ROWID column are hexadecimal strings representing the
address of each row.

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 7
Nulls & Pseudocolumns
• NULLS
– A NULL column is a column with no value.
– Can appear on columns of any datatype that are not restricted by
NOT NULL & PRIMARY KEY integrity constraints.

• PSEUDOCOLUMNS
– Behaves like a table column but is actually not stored in a table.
– Can Select but NOT Insert, Update or Delete from a pseudocolumn.
CURRVAL - Returns the current value of a sequence.
NEXTVAL - Increments the sequence & returns the next
value.

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 8
Nulls & Pseudocolumns

– ROWID: For each row in the table, RowId returns a row’s


address. ROWID values contain information (datablock, row in
the datablock & datafile) to locate a row.

– ROWNUM: For each row returned by a query, the RowNum


returns a number indicating the order in which ORACLE
selects the row from a table or set of joined rows. The First
ROW selected has a ROWNUM of 1, the second has 2 and so
on.

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 9
COMMENTS

• A comment can appear between any keywords, parameters, or


punctuation marks in a statement.

• A comment in a statement can be included using either of


these means:
– /* */
– --

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 10
Database Objects
• Schema Objects
– A schema is a collection of logical structures of data (schema
objects) owned by a dB user.
– Each user owns a single schema.
– Types of schema objects:
clusters, database links, database triggers
external procedure libraries, index-only tables
indexes, packages, sequences
stored functions, stored procedures
synonyms, tables, views

• Non Schema Objects


– directories, profiles, roles

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 11
Operators, Functions

• Operators
– Arithmetic (+/-/*)
– Character
|| - to concatenate strings
– Logical (AND/OR)
– Comparison
=, !=, ^=, !=, >, <, >=, =<,IN, NOT IN, ANY, ALL
[NOT] BETWEEN, EXISTS, [NOT] LIKE, IS [NOT] NULL
– Set Operators
UNION, UNION ALL, INTERSECT, MINUS.

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 12
Functions

Returns a single result row for every row of a


queried table or view.
– Number Functions
– Character Functions
– Date Functions
– Conversion Functions
– Other Functions

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 13
Number Functions

• Widely known functions:


– ABS, EXP, MOD, POWER, SQRT, TRUNC,SIGN
– COS, COSH, SIN, SINH, TAN, TANH, LN, LOG, ROUND

• Some of the uncommon functions:


– CEIL(n) - returns smallest integer >= n
– FLOOR(n) - returns largest integer <= n

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 14
Character Functions

• Widely known functions:


– CHR, LOWER, LPAD, LTRIM, RPAD, RTRIM, SUBSTR,
SUBSTRB, UPPER, ASCII, LENGTH

• Some of the common functions:


– Lower, Upper, InitCap
select city, Upper(city), Lower(city), Initcap(city) from A;
– SUBSTR
You can use SUBSTR to clip out a piece of a string.
Select Substr(Name,6,4) from magazine;

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 15
Character Functions

– Length

Length tells you how long a string is.


Select Name, Length(Name) from Magazine;

– Decode
Decode(value, if1, then1, if2, then2, if3, then3…..else)

– Rtrim,Ltrim
Select Rtrim( 'test', 'st') from dual ; ‘te’

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 16
Date Functions

• KNOWN FUNCTIONS
– ADD_MONTHS, LAST_DAY, NEXT_DAY, SYSDATE,
MONTHS_BETWEEN

– select sysdate from DUAL;


– select add_months(hiredate,6) from EMP;
– select Least(‘20-Dec-00’, ‘20-Nov-00’) from DUAL;
– select last_day(CycleDate) as End month from Payday;
– select To_date(‘17-Mar-00’) - Round(Sysdate) from DUAL;
– Select months_between(date1,date2) from dual ;

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 17
Conversion Functions

• Convert a value of one datatype to another.


TO_CHAR
TO_CHAR (HIRE_DATE, ‘MONTH, DD, YYYY’)
=> JUNE 25 1996
TO_DATE
TO_NUMBER

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 18
Group Functions

• Examples

– select AVG(age) from Emp;

– select SUM(age) from Emp;

– select count(Distinct city), count(city), count(*) from Address;

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 19
FORMAT MODELS

• Select to_char(sysdate,'dd/mm/yyyy hh24:mi:ss') from dual ;


08/07/2005 03:57:47

• Select to_char(56,'s99.00') from dual ;


+56.00

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 20
COMMANDS

• SQL commands are divided into the following categories:


Data Definition Language Commands
Create or Drop any object
Data Manipulation Language Commands
Insert, Update,Delete
Data Retrieval Language
Select
Transaction Control Commands
Commit, Rollback, Savepoint
Data Control Commands
Grant, Revoke

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 21
END

www.perotsystems.com Proprietary and confidential. © 2005 Perot Systems. All rights reserved. All registered trademarks are the property of their respective owners. V1.0 22

You might also like