SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    May 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    String vs Text column types

    In a migration, I can create columns of various types, including :string or :text. What is the difference? Does type :string limit the number of characters used? It seems like I would be able to find a cheatsheet online explaining the difference, but I haven't yet. Thanks.

  2. #2
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    string is varchar(255), text is text in MySQL

    Create a table using both methods, then ...
    mysql> describe tablename;
    ... if you want to double-check.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a listing of MySQL field types. Other database will have variations to these (e.g. Postgres).

    String is limited in size. By default it creates a varchar field of 255 maximum size as samsm indicates. You can also use the :limit option to reduce the size of the field further. So:
    Code:
    t.column "title", :string, :limit => 40
    Will create a varchar field called "title" with a maximum string size of 40 characters.

    A text field is far larger. For MySQL 65535 characters.

    In general, the smaller the field size the more efficient the database. Therefore, if you used text fields for all your string fields, your database would be slower than if you used varchar fields where possible. So restrict using text to fields where you want to store large pieces of character data.

    So for a blog the name of the author is unlikely to be greater that 100 characters, so a varchar field is best. Whereas the content of the blog is very likely to be of variable length and often greater than 255 characters. Use a text field for that.
    Last edited by ReggieB; Jun 2, 2007 at 15:24.


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
  •