Saturday, 4 August 2012

SQL Server Data Types

SQL Server Data Types

Data type and range for SQL Server data type

char(n)               Occupy Fixed length charater maximum 8000.

varchar(n)          Occupy variable length character maximum 8000
varchar(max)      Occupy variable length character maximum 1073741824
text                     Occupy varilble length character maximum 2 GB.
nchar(n)             Occupy fixed length unicode-data maximum 4000 character.
nvarchar(n)         Occuupy variable length uni-code data maximum 4000 character.
nvarchar(max)     Occupy variable length uni-code data. maximum 536870912 character
ntext                    Occupy varialbe length uni-code data..maximum 2 GB data.
bit                        allow 0,1 or null
binary(n)              Occupy fixed length binary data.maximum  8000 bytes
image                   Occupy variable length binary data. Maximum 2 GB.
tinyint                   Allow 0 to 255 and it's storage space is 1byte.
smallint                 Allow -32768 to 32767. It's storage space is 2 byte
int                        Allow -2147483648 to 2147483647.It's storage space is 4 Byte.
float(n)                floating number data from -1.79e+308 to 1.79e+308. n parameter shows field should hold     
                           4 or 8 byte.By default it hold 8 byte.float(24) hold 4 byte and float(53) hold 8 byte.
datetime             It holds from January 1,1753 to December 31 9999.It hold space of 8 Byte.
datetime2           It holds from January 1,0001 to December 31, 9999. It holds space of 6 or 8 Byte



There are many other data type in SQL Server.such as ,xml, cursor, table, decimal, timestamp ... etc...we will discuss in next chapter where we need it.

Some Important Questions:

Ques- Difference between varchar and nvarchar.
* The data type Varchar and NVarchar are the sql server data types, both will used to store the string values.

Differences :
1 
Character Data Type

Varchar - Non-Unicode Data 
NVarchar - Unicode Data

2 Character Size 

Varchar - 1 byte
NVarchar - 2 bytes

3 Maximum Length 

Varchar
 - 8,000 bytes
NVarchar - 4,000 bytes

4 Storage Size 

Varchar - Actual Length (in bytes)
NVarchar - 2 times Actual Length (in bytes)

* The abbreviation for Varchar is Variable Length character String.
* The abbreviation of NVarchar is uNicode Variable Length character String. 
Ques- Difference between char and varchar.
  • The data type char and varchar are the sql server data types,both will used to store the string values.
  • CHAR should be used for storing fix length character strings. String values will be space/blank padded before stored on disk. If this type is used to store varibale length strings, it will waste a lot of disk space.                                                                                                                                                            
    VARCHAR is used to store variable length character strings. The string value's length will be stored on disk with the value itself.
    Char is faster than Varchar.
    Use Char when the data entries in a column are expected to be the same size.
    Ex:
    Declare test Char(100);
    test="Test" -
    Then "test" occupies 100 bytes first four bytes with values and rest with blank data. 
    Use Varchar when the data entries in a column are expected to vary considerably in size.In Vrachar if we use less space than define space the rest of space is not wasted.
    of space is not wasted.

    Ex: 
    Declare test VarChar(100); 
    test="Test" - 
    Then "test" occupies only 4+2=6 bytes. first four bytes for value and other two bytes for variable length information.  
    Conclusion:
    1.When Using the fixed length data's in column like phone number, use Char
    2.When using the variable length data's in column like address use VarChar.
    3. Searching is faster in char comparision to varchar.



    Thanks
    Amitesh Anand
    Please give your suggestion and comment that I improve this blog.  Your comment is very useful for me and all reader of this blog.



No comments:

Post a Comment