You are on page 1of 57

Data Base Management System & Oracle

Paper V
PGDCS&A

By Rupa Patel
Lecturer, MCA dept.
GHRIIT, Nagpur.
UNIT III -SQL
CODD’s Rules, oracle database objects, Sub Languages of SQL,
data Types, Operators.
DDL Statements : Creating tables, Deriving Table from Existing
table, Altering, Dropping Tables, Integrity
Constraints,Specifying Names for the constraints,Viewing
Integrity constraints,adding & Dropping Constraints.
DML Statements : SELECT statement,Insert,update,Delete,&
Synonyms.
Functions : Arithmetic,Date,Character,Cobnversions.Single
row,Aggregate,Decode,Joins,Set operators & Sub queries.
DCL & TCL Statements : Grant,Revoke,Commit,Rollback &
Savepoints.

Feb 12, 2024 PGDCS&A Unit III 2


Functions

 What is a Function?
Stored Software that Manipulates
Submitted Elements and Returns Some
Value
􀂄 May by a SQL Standard Function (SUBSTR)
􀂄 Could be Application Specific

Feb 12, 2024 PGDCS&A Unit III 3


SQL Standard Functions :A function takes
one/more arguments & return a value

Feb 12, 2024 PGDCS&A Unit III 4


Types of Functions

Feb 12, 2024 PGDCS&A Unit III 5


Feb 12, 2024 PGDCS&A Unit III 6
Feb 12, 2024 PGDCS&A Unit III 7
Character Functions: accept character input &
return either character or number values.

Feb 12, 2024 PGDCS&A Unit III 8


Dual table

 Real Oracle Table With a Single Row


 Used When All Elements in the SELECT
and WHERE Clauses do not Reference Any
Tables

Feb 12, 2024 PGDCS&A Unit III 9


1. Syntax : initcap(char)
returns characters with a first letter of each
word in uppercase, all other letters in
lowercase.
Eg. select initcap (‘hello’) from dual;
o/p : Hello

Note :

Feb 12, 2024 PGDCS&A Unit III 10


2. Syntax : lower(char)
used to convert upercase letters to
lowercase.
Eg. select initcap (‘INDIA’) from dual;
o/p : india

Feb 12, 2024 PGDCS&A Unit III 11


3. Syntax : upper(char)
used to convert lowercase letters to
uppercase.
Eg. select initcap (‘india’) from dual;
o/p : INDIA
Another eg. : select * from raisoni where
Lower(dept)=‘mca’;

Feb 12, 2024 PGDCS&A Unit III 12


1. Concat(char1,char2)
 returns char1 concatenated with char2.
 These function is equivalent to concatenation operator
(||).
 user cannot concatenate more than 2 strings.
Ex. : select concat(‘abc’,’xyz’) from dual;
o/p : abcxyz

Feb 12, 2024 PGDCS&A Unit III 13


2. Substr (char, m [,n])
 returns a portion of a character beginning at character
m,n character long.
 If n is omitted,then to the end of character
 The first position of a character is 1.
Ex. : select substr(‘abcdefg’,2,3) from dual;
o/p : bcd
select substr(‘abcdefg’,0,0) from dual;
o/p : no output

Feb 12, 2024 PGDCS&A Unit III 14


3. Length(char)
 Used to find the length of the char.
Ex. : select length (‘abcdefg’) from dual;
o/p : 7
select substr(‘’) from dual;
o/p : no output

Feb 12, 2024 PGDCS&A Unit III 15


4. INSTR(m,n)
 Used to find the position of the char n in string m.
Ex. : select instr (‘abcdefg’,d) from dual;
o/p : 4

Feb 12, 2024 PGDCS&A Unit III 16


5. LPAD(char1,n[,char2])
 Returns char1,left padded to the length n with the
sequence of character in char2,char2 as default to
blank.
Ex. : select lpad (‘india’,10,’*’) from dual;
o/p : *****india
select lpad (‘india’,3,’*’) from dual;
o/p : ind

Feb 12, 2024 PGDCS&A Unit III 17


6. RPAD(char1,n[,char2])
 Returns char1,right padded to the length n with the
sequence of character in char2,char2 as default to
blank.
Ex. : select rpad (‘india’,10,’*’) from dual;
o/p : india*****
Select rpad(‘india’,10) from dual;
o/p : india

Feb 12, 2024 PGDCS&A Unit III 18


7. LTRIM (char [,set])
 Removes character from the left of the char with initial
characters removed up to the first character not in the
set.
Ex. : select ltrim(‘xxxDxxLast’,’x’) from dual;
o/p : DxxLast

Feb 12, 2024 PGDCS&A Unit III 19


8. RTRIM (char [,set])
 Removes character from the right of the char with initial
characters removed up to the first character not in the
set.
Ex. : select rtrim(‘AxDxx’,’x’) from dual;
o/p : AxDxx

Feb 12, 2024 PGDCS&A Unit III 20


9. TRIM
 Combines the functionality of the ltrim & rtrim
 When Specified leading ,the function is similar to the
ltrim
Ex. : select trim(leading 9 from 99987699) from
dual;
o/p : 87699
 When specified trailing, the function is similar to rtrim
Ex. : select trim(trailing 9 from 99987699) from
dual;
o/p : 999876
Feb 12, 2024 PGDCS&A Unit III 21
10. Replace (char, search-string [,replacement-
string])
 Returns char with every occurrence of search-string
replaced with replacement-string. If replacement-string
is omitted all occurrence of search-string are removed.
Ex. : select replace (‘abcd’,’b’,’Z’) from dual;
o/p : aZcd
Select replace (‘abcd’,’b’) from dual;
o/p acd

Feb 12, 2024 PGDCS&A Unit III 22


11. Translate( char, from, to)
 Returns char with all occurrence of each character in
from replace by its corresponding character in to.
 Characters in char that are not in from are not replaced.
 You cannot use empty string for to in order to remove
characters in from the returned value.
 Translate performs character by character replacement.
Ex. : select translate (‘ABCDEFED’,’CDE’,’123’) from dual;
o/p : AB123F32

Feb 12, 2024 PGDCS&A Unit III 23


12. Chr(n)
 Returns the character equivalent of the number n.
Ex. : select chr(67) from dual;
o/p : C

Feb 12, 2024 PGDCS&A Unit III 24


12. Ascii(char)
 Converts character into number .
Ex. : select ASCII(‘A’) from dual;
o/p : 65

Feb 12, 2024 PGDCS&A Unit III 25


Decode function
 In Oracle/PLSQL, the decode function has the functionality of an IF-
THEN-ELSE statement.
 Decode statement was developed to allow user to transform data
values at retrieval time.
 syntax
decode( expression , search , result [, search , result]... [, default] )
 expression is the value to compare.

 search is the value that is compared against expression.

 result is the value returned, if expression is equal to search.

 default is optional. If no matches are found, the decode will return


default. If default is omitted, then the decode statement will return null
(if no matches are found).
 The maximum number of components that you can have in a decode
function is 255. This includes the expression, search, and result
argument

Feb 12, 2024 PGDCS&A Unit III 26


Decode function
 desc studr; Name Null? Type
ROLL NUMBER(2)
MARK NUMBER(5)

select * from studr;


ROLL MARK
1 100
2 25

Feb 12, 2024 PGDCS&A Unit III 27


Decode function
IF mark = 100 THEN
result := ‘A’
ELSe
result := ‘B';
 Following decode statement is equivalent to the If-then statement
select roll, mark,decode(mark,100,’A’,
25, ‘B’) R from studr;
ROLL MARK R
1 100 A
2 25 B

select roll, mark,decode(mark,100,'X','B')R from studr;


ROLL MARK R
1 100 X
2 25 B

Feb 12, 2024 PGDCS&A Unit III 28


Number/Aritmetic

1. Abs 10. Sin, cos, tan


2. Ceil 11. Acos, Asin, Atan
12. Round
3. Floor
13. Mod
4. Power 14. Power
5. Sqrt 15. Trunc
6. Sign 16. Nvl
7. Exp
8. Ln (logarithm)
9. Log

Feb 12, 2024 PGDCS&A Unit III 29


Feb 12, 2024 PGDCS&A Unit III 30
Feb 12, 2024 PGDCS&A Unit III 31
Feb 12, 2024 PGDCS&A Unit III 32
Feb 12, 2024 PGDCS&A Unit III 33
Feb 12, 2024 PGDCS&A Unit III 34
Feb 12, 2024 PGDCS&A Unit III 35
Feb 12, 2024 PGDCS&A Unit III 36
Feb 12, 2024 PGDCS&A Unit III 37
Feb 12, 2024 PGDCS&A Unit III 38
Feb 12, 2024 PGDCS&A Unit III 39
Feb 12, 2024 PGDCS&A Unit III 40
Feb 12, 2024 PGDCS&A Unit III 41
Feb 12, 2024 PGDCS&A Unit III 42
Feb 12, 2024 PGDCS&A Unit III 43
Feb 12, 2024 PGDCS&A Unit III 44
Feb 12, 2024 PGDCS&A Unit III 45
Feb 12, 2024 PGDCS&A Unit III 46
exp

 the exp function returns e raised to the nth


power, where e = 2.71828183.
 The syntax for the exp function is:

exp( number )
number is the power to raise e to.
exp(3)would return 20.0855369231877
exp(3.1)would return 22.1979512814416
exp(-3)would return 0.0497870683678639

Feb 12, 2024 PGDCS&A Unit III 47


Cos

 the cos function returns the cosine of a


number.
 syntax for the cos function is:
cos( number )
cos(0.2)would return 0.980066577841242
cos(0.35)would return 0.939372712847379
cos(-3.15)would return -0.999964658471342
cos(200)would return 0.487187675007006

Feb 12, 2024 PGDCS&A Unit III 48


Acos

 the acos function returns the arc cosine of a


number.
 Syntax: acos( number )

number is the number used to calculate the arc


cosine.
 For example:

acos(0.2)would return 1.36943840600457


acos(-0.15)would return 1.72136459957158

Feb 12, 2024 PGDCS&A Unit III 49


Aggregate /Group functions
 SQL aggregate functions return a single
value, calculated from values in a column.
 Useful aggregate functions:
 AVG() - Returns the average value
 COUNT() - Returns the number of rows
 FIRST() - Returns the first value
 LAST() - Returns the last value
 MAX() - Returns the largest value
 MIN() - Returns the smallest value
 SUM() - Returns the sum
Feb 12, 2024 PGDCS&A Unit III 50
Aggregate functions

1. AVG :
Returns the average of the column
values.
Syntax : avg([distinct | all ] columnname)
Eg. Select avg(mark) from studr;

Feb 12, 2024 PGDCS&A Unit III 51


SQL Scalar functions

 SQL Scalar functions


 SQL scalar functions return a single value, based on
the input value.
 Useful scalar functions:
 UCASE() - Converts a field to upper case
 LCASE() - Converts a field to lower case
 MID() - Extract characters from a text field
 LEN() - Returns the length of a text field
 ROUND() - Rounds a numeric field to the number of
decimals specified
 NOW() - Returns the current system date and time

Feb 12, 2024 PGDCS&A Unit III 52


Feb 12, 2024 PGDCS&A Unit III 53
Number/Arithmetic
 ACOS,
 ASIN,
 ATAN,
 ATAN2,
 COS,
 COT,
 SIN,
 TAN,
 DEGREES,
 RADIANS,
 CEILING,
 FLOOR,
 EXP,
 LOG, LOG10, PI(), POWER, ABS, RAND, ROUND, SIGN, SQRT.

Feb 12, 2024 PGDCS&A Unit III 54


12. DECODE( valu1,if1,then1,if2,then2,..)
 Decode function does a value by value replacement
Ex. :

Feb 12, 2024 PGDCS&A Unit III 55


Single value functions

1. ABS : returns the absolute value.

Feb 12, 2024 PGDCS&A Unit III 56


Feb 12, 2024 PGDCS&A Unit III 57

You might also like