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:
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; |
No comments:
Post a Comment