If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > var and varchar

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-16-09, 23:15
marcin_koss marcin_koss is offline
Registered User
 
Join Date: Nov 2009
Posts: 9
var and varchar

Hello.

I understand that VAR is a fixed length datatype and it makes sense using it for strings that have always the same amount of characters. I know that VARCHAR is variable length datatype and it stores only as many characters as it needs to store thus it is better to use it with strings of different length.
What I don't understand is giving a length to a VARCHAR datatype like for example VARCHAR(25) - what does it really do and when to use it?

Thanks
Reply With Quote
  #2 (permalink)  
Old 11-16-09, 23:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
instead of VAR and VARCHAR, you meant to say CHAR and VARCHAR

the length of a VARCHAR is the maximum allowed length

so you cannot enter the string 'abcdefghijklmnopqrstuvwxyz' into a VARCHAR(25) column, because it's too long
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-16-09, 23:45
marcin_koss marcin_koss is offline
Registered User
 
Join Date: Nov 2009
Posts: 9
hmmm that actually makes a lot of sense,

...and yes I meant CHAR not VAR (l guess I should quit learning MySQL during night time)

So it basically prevents inputting longer strings, right?

Ehh, enough for tonight

Thanks.
Reply With Quote
  #4 (permalink)  
Old 11-17-09, 02:47
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by r937 View Post
instead of VAR and VARCHAR, you meant to say CHAR and VARCHAR

the length of a VARCHAR is the maximum allowed length

so you cannot enter the string 'abcdefghijklmnopqrstuvwxyz' into a VARCHAR(25) column, because it's too long
Depends.
I think with the default installation, MySQL will simply truncate the value, but will not refuse to insert it.
Reply With Quote
  #5 (permalink)  
Old 11-17-09, 04:04
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by shammat View Post
I think with the default installation, MySQL will simply truncate the value, but will not refuse to insert it.
nicely spotted

ya learn sumpin new ever day
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Tags
mysql, string lenght, var, varchar

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On