37

i want to make a table in MySQL server with mediumtext column as UNIQUE KEY

CREATE TABLE `parts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` mediumtext NOT NULL, `display_status` int(11) NOT NULL, UNIQUE KEY `name` (`name`), PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

but this made an error

 BLOB/TEXT column 'name' used in key specification without a key length 

when I change the type of `name` to varchar .. it works!

can you tell if i can to make text column as UNIQUE KEY

3
  • 1
    possible duplicate : stackoverflow.com/questions/1827063/… Commented Dec 25, 2012 at 19:22
  • 1
    thanks @Tarek but that solution force me to use varchar .. and i want to use a type allow me to insert strings longer than 255 characters Commented Dec 25, 2012 at 19:29
  • You might add a length, like UNIQUE KEY name` (name(100)),` if the first 100 were unique. Commented Dec 25, 2012 at 19:43

3 Answers 3

39

Basically you can not use Text column as UNIQUE key. Because practically such a big column will not be unique and there might be a chance of more duplicates. So go for hashing method and use that output as a UNIQUE constraint.

Sign up to request clarification or add additional context in comments.

6 Comments

Note that technically this is not a solution, because two different text values may generate the same hash - ie a [hash collision](en.wikipedia.org/wiki/Collision_(computer_science). To be sure of keeping the values unique you would have to store the whole string too and not have a unique index on the hash column.
Here is a more deep answer for who want to really understand why mysql behave like this : stackoverflow.com/a/1827099/5677103
@Bohemian But practically speaking, the chances that this collision would occur given that we use a decent hashing algorithm would be very very low, so it would be a good pragmatic solution wouldn't it?
@Javascriptonian it depends on the population size and the hash algorithm. For example, when using a 32-bit hash result, at around 70K individuals, there's a 50/50 chance of a collision between any two individuals.
Does not work with mysql, but with mariadb: gist.github.com/BacLuc/0ffb97c8d32b1190b649a955134d0625
|
26

The limit of 255 for varchar length no longer applies. From the documentation:

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions.

Unique indexes must have a known maximum length (a requirement of mysql due to its internal implementation), so use varchar with a large enough value to fit your longest expected value, eg

... `name` varchar(65535) NOT NULL, -- for example ... 

2 Comments

Ok but you cannot create a unique key on a varchar(65535) can you?
-4

I know this is very old but in case anyone else wants to, you can make a varchar or text column unique, you just have to do it after the table creation.

 DROP TABLE IF EXISTS meeting; CREATE TABLE meeting ( meeting_id int(11) UNSIGNED UNIQUE NOT NULL PRIMARY KEY AUTO_INCREMENT, meeting_name varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL ); CREATE UNIQUE INDEX meeting ON meeting(meeting_name); 

1 Comment

No, you cannot make a text column unique.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.