Hello!

If you're starting to program and getting into databases, you'll run into this problem someday. Text fields can't be unique in MySQL, but there's a very simple solution, which is a unique identifier equivalent to that hash.

Let's say we have the following long text:

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

If we use the https://www.php.net/manual/es/function.md5.php function from php or the same one from https://dev.mysql.com/doc/refman/8.0/en/encryption-functions.html#function_md5, we can get the following unique result:

01aad0e51fcd5582b307613842e4ffe5

This way, we'd have a hash to check whether it exists or not without it taking up a lot of space (encryption uses more CPU, but not database space).

The function in php would look like this:

<?php
$string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
echo $string;
echo "<hr />";
echo md5($string);
?>

So in the DB we'd have to create the following fields:

(unique, varchar255) hash: 01aad0e51fcd5582b307613842e4ffe5 (text, without identifying it as unique) text: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.