Sunday, 27 October 2013

Data types in Oracle SQL



You all know that we are aiming at one common thing in every programming language: that is data. yes, we target data in every programming methodology with the use of types of data. in the oracle too, we have data types to work with different types of data. they are:
  • Number(n)
  • Number(p,q)
  • char(n)
  • varchar2(n)
  • varchar
  • Date
  • Nchar(n)
  • Nvarchar2(n)
First of all, we'll go for the very first one in the line: Number. Have you noticed that there are two Number types i have shown? The first one has only a single parameter in the braces, while the second one has two entries. We'll go for number(n).

number(n) is used to store or define numbers or integers of n digits. This means that if we declare an attribute by the type Number(n), then the attribute can hold a value of at most n digits.

Example:
                          create table Student_Number(snum number(5));

However, when we add another value m to the Number(n) syntax, it would become Number(n,m) which is used to define float type or decimal type variables. the variable will be at most n digits with n-m-1 as left to decimal and m to the right of decimal.

For example, if we define Number(5,2) for an attribute, then the attribute can hold:
  1. 5 total digits, including decimal
  2. 2 digits as number
  3. 2 digits as decimal
  4. one is reserved for the decimal (.)
Example:
                         create table Item_Price(price Number(10,2));

The third type is character, it is obviously used to store alpha numeric characters, or strings. If we define an attribute as char(n), then the attribute can hold at most of n characters. the dis advantage of using char(n) for defining strings is that, if the data stored is either less or equal: the fixed block of memory is given. If our data is lesser than allocated, its sure a waste. Hence a new type has been developed, its varchar2(n). it has the answer for the above discussed problem. the data is given memory up to the needed, but not excess. this sure saves memory. Hence we generally declare strings with varchar2().

Example:

create table Student_Name(initial char(1)name varchar2(10)last_name varchar2(10));

If you question me that why we're talking all the way about varchar2() and not varchar() ? well the answer is like what we would see in Java, Reserved Keywords. In the same sound, varchar() is also reserved for future use. Date type is used to store the date, Oracle supports Date in the format "DD-MON-YY". Hence the date in that format will be stored.

Example:

create table Student_Register(initial char(1), name varchar2(10)last_name varchar2(10)doj date);

Nchar() and Nvarchar2() are similar to the char() and varchar2() in their properties and behavior  but the thing that changes is the data stored. Nchar and Nvarchar2 store data in uni code format.

No comments:

Post a Comment