SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    I was reading the article "Managing Users with PHP Sessions and MySQL" by Kevin Yank, and I saw this code:

    mysql> CREATE TABLE user (
    -> ID INT PRIMARY KEY AUTO_INCREMENT,
    -> userid VARCHAR(100) UNIQUE NOT NULL,
    -> password CHAR(16) NOT NULL,
    -> fullname VARCHAR(100) NOT NULL,
    -> email VARCHAR(100) NOT NULL,
    -> notes TEXT);

    Why does he use 'CHAR(16)' for the pasword and 'VARCHAR(100)' for the email and fullname? Why not use 'VARCHAR(16)' and 'VARCHAR(100)'? Or 'CHAR(16)' and 'CHAR(100)'? What's the difference?

    Please let me know.

  2. #2
    Anyone seen my cypher? OneChance's Avatar
    Join Date
    Apr 2001
    Location
    Seattle
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Some information about their differences can be found at MySQL.com. Basically CHAR is a fixed-length string, and VARCHAR is a variable-length string.

  3. #3
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the above code, mysql will automatically convert char to varchar since you have varchar columns in a table. That's the way mysql does (use command "describe user" and see for yourself).

  4. #4
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by johnn
    For the above code, mysql will automatically convert char to varchar since you have varchar columns in a table. That's the way mysql does (use command "describe user" and see for yourself).
    Why would it do that? That doesn't make sense... (not questioning your correctness but MySQL's logic with that).

  5. #5
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think mysql see that the row has a mixture of both char and varchar types and that means the row does has a variable length itself. Faster excecution or better performance does not apply here because the row is not a fixed-length row anymore, so mysql change all char to varchar types


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •