jaggi
jaggi

Reputation: 59

Defining length of CHAR type in Mysql

Consider the two String types in MySQL

1.Char[LENGTH]

2.Varchar[LENGTH]

Question:Defining the LENGTH field is necessary For which type Varchar or Char ?

According to My opinion It is Char but In one tutorial it is written that you must define Length for Varchar while there is no such requirement for Char type.

CHAR(M) - A fixed-length string between 1 and 255 characters in length (for example

CHAR(5)), right-padded with spaces to the specified length when stored. Defining a length

is not required, but the default is 1.

VARCHAR(M) - A variable-length string between 1 and 255 characters in length; for

example VARCHAR(25). You must define a length when creating a VARCHAR field.

Link: www.tutorialspoint.com/mysql/mysql-data-types.htm

Is the thing Written in Tutorial Right?

Upvotes: 1

Views: 3364

Answers (1)

Vishwanath Dalvi
Vishwanath Dalvi

Reputation: 36591

If I'm clear with what you are looking for .

Take a look at this.

CREATE TABLE mytable (column1 VARCHAR(20), column2 CHAR);

insert into mytable values('stackoverflow','v');
select * from mytable;

If you don't specify the length for char datatype it will take 1 as default.

Now if you try to insert string in column2 more than length 1 it will raise an error.

Summary : Varchar datatype need length otherwise it will raise an error where in char datatype if you don't specify the length it will take 1 as default.

Upvotes: 2

Related Questions