Indexing a MySql TEXT column?

MysqlIndexing

Mysql Problem Overview


I ran this using MySql and it appears to not like TEXT. With SQL server I use nvarchar(max) What should I use in MySql? In other tables some fields will be descriptions and may be long so at the moment I am thinking that fixed length is bad.

create table if not exists 
    misc_info (
        id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
        key TEXT UNIQUE NOT NULL,
        value TEXT NOT NULL
    )ENGINE=INNODB;

Mysql Solutions


Solution 1 - Mysql

You can't have a UNIQUE index on a text column in MySQL.

If you want to index on a TEXT or a BLOB field, you must specify a fixed length to do that.

From MySQL documentation:

> BLOB and TEXT columns also can be > indexed, but a prefix length must be > given.

Example:

CREATE UNIQUE INDEX index_name ON misc_info (key(10));

Solution 2 - Mysql

Two things:

  1. Key is a reserved word.
  2. You have to specify a length for a UNIQUE(key) TEXT value.

Solution 3 - Mysql

I think it chokes on the key field name rather than the TEXT type (which should be perfectly fine).

Reserved Words in mySQL

(And as @Pablo already said, memo fields can't be unique.)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser34537View Question on Stackoverflow
Solution 1 - MysqlPablo Santa CruzView Answer on Stackoverflow
Solution 2 - Mysqlkhai_khaiView Answer on Stackoverflow
Solution 3 - MysqlPekkaView Answer on Stackoverflow