Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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

How are Char, Varchar and Varchar2 different from each other.?

Please explain Their working, storage, speed and everything that differs

user-image
Question ajoutée par Abdul Khader Shaik , IT & Ops Manager , TheSmartlabs
Date de publication: 2018/01/03
MOHAMED LABIDI
par MOHAMED LABIDI , Responsable Technique , Next software groupe Cegedim

CHAR(n) est un type de données qui respecte de la longueur dont n caractères stockée même si la longueur du texte est inférieure à n, il y aura alors un remplissage à blanc.

VARCHAR(n) est un type de données de longueur variable avec au maximum n caractères stockée + stockage de la longueur du texte (en général sur 2 octets) et pas de remplissage à blanc.

Atul Singh
par Atul Singh , Senior Software Engineer , HCL Technologies India Pvt. Ltd.

The char is a fixed-length character data type, the varchar is a variable-length character data type.

The only difference between them is that nchar/nvarchar store Unicode characters (essential if you require the use of extended character sets) whilst varchar does not.

VARCHAR2 is used to store variable length character strings. Both CHAR and VARCHAR2 types are used to store character string values, however, they behave very differently. The VARCHAR type should not be used: CHAR. CHAR should be used for storing fixed length character strings.

Brijesh Sharma
par Brijesh Sharma , Senior Architect , Mindtree Ltd

CHAR store the data in fixed length where as VARCHAR storage depends on the data it has. They differ in the way they are stored and retrieved.

For Example: if we define char(10) and we store "data" in it then the rest 6 spaces with have NULL. The memory space occupied will be 10.

if we define varchar(10) and we store "data" in it then the rest 6 spaces with be freed means now act as varchar(4). On futher updates it will grow and shink based on the data it has maximum to 10. Memory occupied is the data it has.

Badr Alharbi
par Badr Alharbi , Help Desk Supervisor , Madinah Cardiac Center

CHAR it is datatype of which can hold fixed length of characters.

VARCHAR is dynamically allocated its length in the memory.

In term of size, CHAR allocated until 255 bytes.

VARCHAR, however, can hold characters reach to 65,535 bytes.

CHAR is more attractive for developer those seeking the speed in their queries, a risky situation if they do not manage the user entry using validation techniques.

VARCHAR therefor could make your program more reliable but lower performance.

Thank you

Ghaith Mansour
par Ghaith Mansour , network engineer , Hiast

to quote MySql official website: "they differ in the way they are stored and retrieved. They also differ in maximum length and in whether trailing spaces are retained.

The CHAR and VARCHAR types are declared with a length that indicates the maximum number of characters you want to store. For example, CHAR(30) can hold up to 30 characters."

mohammed bedrouni
par mohammed bedrouni , Ingénieur en Informatique , Université des Sciences et de la Technologie Houari-Boumediene USTHB

VARCHAR is variable-length.

CHAR is fixed length.

Rasha Salim
par Rasha Salim , تطوير مواقع و تطبيقات , Freelancer

Hello, 

The difference is the CHAR has a fixed length while VARCHAR has a variable length.in other word if you need to define a variable that has a fixed length like a yes or no variable that takes only  "Y" or "N" no less no more you would define it this way: CHAR(1)

but in case of you don't know the exact size of your charaters (e.g. names has different lengths) in that case it is more efficient to define a VARCHAR this way even if you define it as VARCHAR(MAX) it will only take the size of the entered name from memory and that certainly the best approach.Edit: sorry I didn't see the VARCHAR2 but basically what I understood, it is exactly like VARCHAR the only difference is that in VARCHAR you will be able to check if the variable has NULL value or just an empty string, while VARCHAR2 treats them both the same.Hope this will help you

More Questions Like This