Friday, May 15, 2009

ORACLE DATA TYPES

Lets discuss three data types CHAR, VARCHAR and VARCHAR2 and understand the differences between them. This is also one of the frequently asked questions in interviews.

CHAR
It is used for storing fixed length character strings. For example FLAGS in tables which contain Y or N for YES or NO respectively.
NAME CHAR(50);
When we declare such a type for name which contains variable no of characters, it results in wastage of lot of space.
NAME := 'MANIK' fills 5 places rest of the places will be padded with spaces.
So, its not a good idea to use this type for variable length strings.
NOTE: When we don't specify a size with CHAR, it defaults to one.


VARCHAR
Its not used generally, I learnt from internet that it is kept for furture use, otherwise it behaves same as VARCHAR2 as discussed next

VARCHAR2
It is used for storing variable length strings. In Oracle 10g, a VARCHAR2 column may be up to 4000 bytes in a table and it may be up to 32767 bytes in a PL/SQL program.

NAME VARCHAR2(50) := 'MANIK';
This does not leads to wastage of space. Only 5 places are occupied.

No comments:

Post a Comment