Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 27th Dec 2013, 04:39 AM   #1
Warrior Member
 
Join Date: 2013
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Default
MySQL data type char or varchar is best?
Share on: 
fb share twitter share gplus share more share

MySQL data type char or varchar is best?

what is the difference between char and varchar?

Thanks,
Roger,
connected commerce
rogerlegend is offline  
Unread 27th Dec 2013, 05:19 AM   #2
sheik
 
Join Date: 2013
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default
Re: MySQL data type char or varchar is best?
Share on: 
fb share twitter share gplus share more share

Try This Link
MySQL :: MySQL 5.1 Reference Manual :: 11.4.1 The CHAR and VARCHAR Types

sheikmd is offline  
Unread 27th Dec 2013, 09:22 AM   #3
digital carpenter
War Room Member
 
brutecky's Avatar
 
Join Date: 2013
Location: Pennsylvania
Posts: 1,306
Thanks: 480
Thanked 747 Times in 444 Posts
Default
Re: MySQL data type char or varchar is best?
Share on: 
fb share twitter share gplus share more share

No one can say which is best considering you didnt indicate what kind of data you want to store.

Char has a max of 256 characters
Var Char has a max of 65,535 characters

So one consideration is the length of the data you wish to ad to the field.

brutecky is offline  
Unread 28th Dec 2013, 03:58 AM   #4
Warrior Member
 
carltonman's Avatar
 
Join Date: 2013
Posts: 20
Thanks: 0
Thanked 7 Times in 7 Posts
Default
Re: MySQL data type char or varchar is best?
Share on: 
fb share twitter share gplus share more share

If you specifiy CHAR as 8 characters long, than no matter if you write "one" or "eightcha", both will take up 8 characters. "one" will be written as "one_____" with the trailing space added.

If you specify VARCHAR 8 characters long and you only want to store the word "one" in it, it will only store those three characters. No trailing space added.

As it is explained on the link already posted:
MySQL :: MySQL 5.1 Reference Manual :: 11.4.1 The CHAR and VARCHAR Types
carltonman is offline  
The Following User Says Thank You to carltonman For This Useful Post:
Unread 28th Dec 2013, 10:07 PM   #5
IM Rocks!
War Room Member
 
shahriyar's Avatar
 
Join Date: 2010
Location: USA
Posts: 501
Thanks: 52
Thanked 65 Times in 60 Posts
Arrow
Re: MySQL data type char or varchar is best?
Share on: 
fb share twitter share gplus share more share

Originally Posted by rogerlegend View Post

MySQL data type char or varchar is best?

what is the difference between char and varchar?
The data range is given above, the point is what type of data you want to store in it. If it is not more than 256 char, use char.

shahriyar is offline  
Unread 2nd Jan 2014, 04:44 PM   #6
Active Warrior
War Room Member
 
HJdev's Avatar
 
Join Date: 2013
Location: Netherlands
Posts: 52
Thanks: 10
Thanked 18 Times in 11 Posts
Default
Re: MySQL data type char or varchar is best?
Share on: 
fb share twitter share gplus share more share

sql server - Why would I ever pick CHAR over VARCHAR in SQL? - Stack Overflow
HJdev is offline  
Unread 2nd Jan 2014, 11:35 PM   #7
Veteran Marketer
War Room Member
 
Jeremy Morgan's Avatar
 
Join Date: 2009
Location: Portland, OR
Posts: 548
Thanks: 51
Thanked 86 Times in 61 Posts
Blog Entries: 10
Default
Re: MySQL data type char or varchar is best?
Share on: 
fb share twitter share gplus share more share

I generally tend to lean on varchar for just about everything. Unless it's something like state abbreviations (Oregon - OR) that you know will never change, it's always a good idea.

As others have said it depends on what you're storing, and how much of it you're storing.

Jeremy Morgan, Software Developer / SEO
Learn AI Powered Content Creation
at Prompt and Prosper
Jeremy Morgan is offline  
Unread 7th Jan 2014, 03:52 AM   #8
Warrior Member
 
Join Date: 2013
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default
Re: MySQL data type char or varchar is best?
Share on: 
fb share twitter share gplus share more share

CHAR should be used for storing fix length character strings.
VARCHAR provides for variable-length storage of strings.
raypal is offline  
Unread 10th Jan 2014, 01:49 PM   #9
HyperActive Warrior
Registered Member
 
Join Date: 2012
Location: In the land of Imagination
Posts: 228
Thanks: 13
Thanked 7 Times in 6 Posts
Blog Entries: 1
Default
Re: MySQL data type char or varchar is best?
Share on: 
fb share twitter share gplus share more share

For fixed and short length i myself use char i think if its not more than 256 than use char.

designitbuddy is offline  
Unread 9th Feb 2014, 03:54 AM   #10
Active Warrior
War Room Member
 
Jamie Lin's Avatar
 
Join Date: 2012
Location: Worldwide
Posts: 75
Thanks: 34
Thanked 18 Times in 16 Posts
Default
Re: MySQL data type char or varchar is best?
Share on: 
fb share twitter share gplus share more share

Generally, varchar for everything but when you're dealing with database optimization char is a great way to go. So it really depends on what you are trying to accomplish.
Jamie Lin is offline  
Unread 9th Feb 2014, 07:33 PM   #11
Development, SEO, & ORM
 
Iconiplex's Avatar
 
Join Date: 2014
Location: Houston, TX
Posts: 62
Thanks: 3
Thanked 14 Times in 10 Posts
Default
Re: MySQL data type char or varchar is best?
Share on: 
fb share twitter share gplus share more share

Use CHAR when you know your data storage size requirement for any given data string will be less to its equivalent in VARCHAR and when you know your data won't exceed the maximum CHAR limit.

Else, use VARCHAR.

US-based design, development, marketing, branding, printing, and all other business services.
High-risk payment processing for pharma, e-cigs, seeds, and more!
Accepting investment management clients. Minimum deposit is $3000.
Skype/AIM: artizhay
Iconiplex is offline  
Unread 10th Feb 2014, 12:42 AM   #12
Coder&Marketer
War Room Member
 
r0dvan's Avatar
 
Join Date: 2011
Location: US and Mexico
Posts: 176
Thanks: 4
Thanked 15 Times in 12 Posts
Default
Re: MySQL data type char or varchar is best?
Share on: 
fb share twitter share gplus share more share

Learn to identify your data limits. As same as you choose DATE, choose CHAR and VARCHAR, everything can result in a unknown bug in future development if you do not choose carefully.

LeadGen.tools - Lead Generation Search Engine from any Network and many useful tools. FREE trial.
r0dvan is offline  
Unread 26th Feb 2014, 12:51 PM   #13
HyperActive Warrior
War Room Member
 
Join Date: 2014
Posts: 120
Thanks: 18
Thanked 16 Times in 14 Posts
Default
Re: MySQL data type char or varchar is best?
Share on: 
fb share twitter share gplus share more share

The following will best display the differences:

ValueCHAR(4)Storage RequiredVARCHAR(4)Storage Required''' '4 bytes''1 byte'ab''ab '4 bytes'ab'3 bytes'abcd''abcd'4 bytes'abcd'5 bytes'abcdefgh''abcd'4 bytes'abcd'5 bytes

Just stay with varchar and set the length to the max size you will need. In 19 years I have rarely used char. My cousin does massive multi gigabyte government dbs and he uses char.

Again set the length as example:

email varchar(50)
first_name varchar(10)
last_name varchar(15)
state_abbreviated varchar(2)
ssn varchar(10)
postal_code varchar(10)
username varchar(10)

By setting the length you prevent the database from becoming bloated with unused space, plus you will help speed up searches.

Also index each column you will search on.

For example, email, first and last names, state, zip_codes, etc.

Indexing will dramatically help speed up search results.

Hope this helps.
MonopolyOnline is offline  
Unread 27th Feb 2014, 01:48 AM   #14
Web Developer
 
Join Date: 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default
Re: MySQL data type char or varchar is best?
Share on: 
fb share twitter share gplus share more share

mysql - Any benefit of uses CHAR over VARCHAR? - Stack Overflow

VARCHAR
varchar stores variable-length character string. it can require less storage than fixed-length types because it uses only as much space as it needs.

varchar also uses 1 or 2 extra bytes to record the value's length. for example varchar(10) will use up to 11 bytes of storage space. varchar helps performance because it saves space. however because the rows are variable length, they can grow when you update them, which can cause extra work. if a row grows and no longer fits in its original location, the behavior is storage engine-dependent...

CHAR
char is fixed-length , mysql always allocates enough space for the specified number of characters. When storing a CHAR value, MySQL removes any trailing spaces. Values are padded with spaces as needed for comparisons.

char is useful if you want to store very short strings, or if all the values are nearly the same length. For example, CHAR is a good choice for MD5 values for user passwords, which are always the same length.

char is also better than VARCHAR for data that’s changed frequently, because a fixed-length row is not prone to fragmentation.

Ubiq - Easily create reports from MySQL data
Ramy31 is offline  
Closed Thread


Bookmarks

Tags
char, data, mysql, type, varchar


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 04:44 AM.