Nick
Nick

Reputation: 331

Unique VARCHAR(65535) field per <ID> in MySQL

Are there solutions to maintain a VARCHAR(65535) field unique per ID in MySQL?

Upvotes: 0

Views: 291

Answers (1)

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181350

I would use a hashing function on field's value and store it in a separate unique field on the same table.

So your table would be something like this:

create table my_table (
   id integer not null primary key,
   text_content varchar(65535) not null,
   text_hash varchar(128) not null unique
);

When inserting on that table, you will compute sha256 hash for text_content field and store it in text_hash field. That way you can be pretty sure text_content values are unique on your table.

If you like DB SIDE programming, you can put this logic on a trigger provided MySQL supports them.

Upvotes: 1

Related Questions