mysqlcharacter-set

What datatype should i choose for mysql db to store a paragraph text which includes special characters as well?


In my MySql DB I want to store some text which will be a mimimun of 1000 words, it will contain some special characters like "",'()<>-.!; data will be passed using php.


Solution

  • There are several datatypes that hold text. When you declare any of them you can also specify their collation and character set. For example:

    CREATE OR REPLACE TABLE mytable {
        mytext VARCHAR(2000) COLLATE utf8mb4_general_ci
    )
    

    This gives you a place to store up to 2000 characters of Unicode text. Each Unicode character can take from 1 to 4 bytes of storage. Unicode, as you know, handles many different human scripts, not to mention emojis and math symbols.

    To store text you have

    These all have reasonable overhead in terms of disk space used.

    If you want to put indexes on VARCHAR() columns limit their size to VARCHAR(768).

    Pro tip: Avoid the LOB data types unless you absolutely need them. They make the server do extra work when you use them in SELECT or WHERE clauses. Use VARCHAR() instead.