CodesJava

Easy learning with example program codes

What is the difference between CHAR and VARCHAR2 datatype in SQL?


CHAR
Char type refers to fixed size datatype. So if we declare it with 10 bytes, then it always stores 10 bytes in the database and it doesn’t matter whether we store any text or just empty 10 bytes

VARCHAR2
Varchar2 represents a datatype of variable size, it’s size depends on how many bytes you are actually going to store in the database. The number we specify is just the maximum number of bytes that can be stored.  Minimum is 1 byte.

We should use CHAR when we know the exact length of the value in advance, database can then manipulate with it better and faster since it knows the exact length. Otherwise use VARCHAR2.

Example

username VARCHAR2(255),
zip_code CHAR(6) //Fixed size zip code

Related topics

Posted in SQL   


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap