what is the difference between char and varchar data type ?

Hi arupa

Let's look at an example: With create table t(c char(10), v varchar(10)) there will be 10 places, e.g. 10 bytes statically allocated for column c. At first, there will be nothing allocated for column v, internally indicated by setting current length of v to 0. If you do insert into t (c,v) values ('Hello', 'World') value of c will be internally stored as 'Hello#####' and v as 'World' plus current lenght 5, (# stands for space char). So values of columns with datatype char(char_length) will be padded by spaces if current length < char_length. varchar datatype only allocates as much memory units (usually bytes) as current length requires. Today, most databases usually convert datatype char() into varchar() automatically if char_length exceeds given lower bound (e.g. 10 chars).

krs,
tesu

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.