What is the difference between CHAR and VARCHAR datatype in SQL? - www.studyandupdates.com

Sunday

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

Q 5. What is the difference between CHAR and VARCHAR datatype in SQL?

Ans :

1. CHAR Datatype:

It is a datatype in SQL which is used to store character string of fixed length specified.

If the length of the string is less than set or fixed-length then it is padded with extra blank spaces so that its length became equal to the set length

when PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.

The storage size of the CHAR datatype is n bytes(set length). We should use this datatype when we expect the data values in a column are of the same length.


Example:

Consider the Query:


CREATE TABLE Student(Name VARCHAR(30), Gender CHAR(6)); INSERT into Student VALUES('Herry', 'Male'); INSERT into Student VALUES('Mahi', 'Female'); SELECT LENGTH(Gender) FROM Student;

OUTPUT:




2. VARCHAR Datatype:

It is a datatype in SQL which is used to store character string of variable length but a maximum of the set length specified.

If the length of the string is less than set or fixed-length then it will store as it is without padded with extra blank spaces.

The storage size of the VARCHAR datatype is equal to the actual length of the entered string in bytes.
We should use this datatype when we expect the data values in a column are of variable length.

Example:
Consider the Query:


CREATE TABLE Student(Name VARCHAR(20), Gender CHAR(6)); INSERT into Student VALUES('Herry', 'Male'); INSERT into Student VALUES('Mahi', 'Female'); SELECT LENGTH(Name) FROM Student;

OUTPUT:











No comments:

Post a Comment

Popular Posts