English

Differentiate between CHAR and VARCHAR data types in SQL with appropriate examples.

Advertisements
Advertisements

Questions

Differentiate between CHAR and VARCHAR data types in SQL with appropriate examples.

Differentiate between char and varchar.

Distinguish Between
Advertisements

Solution

S. No. CHAR VARCHAR
1. Used to store strings of fixed size. Used to store strings of variable length.
2. Uses a fixed amount of storage, based on the size of the column. Use varying amounts of storage space based on the size of the string stored.
3. Takes up 1 to 4 bytes for each character, based on the collation setting. Takes up 1 to 4 bytes for each character based on the collation and requires one or more bytes to store the length of the data.
4. Better performance. Slightly poorer performance because length has to be accounted for.

Example: Consider the table student given below:

Table: Student
Roll Name Phone Aadhar Marks

Every value in the Name field will take up 30 characters if the user selects char(30), regardless of whether "Ria" or "Ramakrishnan Ayyar" is the saved name.

If varchar(30) is used, "Ria" will occupy 3 units, whereas "Ramakrishnan Ayyar" will take 18 units.

shaalaa.com

Notes

Students should refer to the answer according to their question.

Data Types and Constraints in MySQL
  Is there an error in this question or solution?
Chapter 9: Databases Concepts and SQL - EXERCISE [Page 173]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 9 Databases Concepts and SQL
EXERCISE | Q 12. | Page 173
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×