Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > MySQL
|
MySQL General discussion about the MySQL database.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the MySQL section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 16th, 2006, 11:55 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default column with one character - use Char or Varchar

I have a column in my MySQL DB table that only has one character in it. Should the data type be set as Char(1) or Varchar(1). It will only be either 'N' or 'W'.

Thanks.

Picco

www.crmpicco.co.uk
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
 
Old January 17th, 2006, 03:36 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Picco,

Using char(1) would use 1 byte per character, while using a varchar would use 2 bytes per character. Therefore I recommend using char(1) for efficiency.

You can get more information on chars and varchars from MySQL's developer documentation: http://dev.mysql.com/doc/refman/5.0/en/char.html.

Jon Emerson
Computer Scientist
Adobe Systems, Inc.
 
Old January 17th, 2006, 05:27 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

Value CHAR(4) Storage Required VARCHAR(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

This table is quite conflicting from what you were saying above.
It seems VARCHAR is better for smaller records, i,e 1?

www.crmpicco.co.uk
 
Old January 17th, 2006, 05:52 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Picco,

A char column is fixed in length, whereas a varchar is not and uses one extra byte to store the length of the value. Therefore for a char(1), 1 byte will be used and for a varchar(1), 1 byte plus 1 byte to store the length = 2 bytes will be used.

HTH,

Chris

 
Old January 17th, 2006, 07:35 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

ah, ok. Cheers for that. I had it set up with Char(1), so i'll keep it at that!

www.crmpicco.co.uk





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to insert varchar data in numeric column dhirajDac Classic ASP Components 3 March 31st, 2008 06:23 AM
Big challenge here! How to convert char* to char^? samiswt Visual C++ 2005 1 November 30th, 2007 09:09 PM
find character % and format column paul20091968 Excel VBA 0 January 29th, 2007 05:45 AM
Invalid conversion from 'char*' to 'char' deuxmio C++ Programming 3 December 8th, 2006 07:56 AM
VarChar Problem acko SQL Server 2000 3 August 28th, 2003 10:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.