Ciao!
Se stai iniziando a programmare e a lavorare con i database, un giorno incontrerai questo problema. I campi text non possono essere unici in MySQL, ma c'è una soluzione molto semplice: un identificatore unico equivalente a quell'hash.
Diciamo che abbiamo il seguente testo lungo:
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.
Se usiamo la funzione https://www.php.net/manual/es/function.md5.php di php o quella stessa di https://dev.mysql.com/doc/refman/8.0/en/encryption-functions.html#function_md5, potremo ottenere il seguente risultato unico:
01aad0e51fcd5582b307613842e4ffe5
In questo modo, avremmo un hash per verificare se esiste o no senza che occupi molto spazio (la crittografia consuma più CPU, ma non spazio nel database).
La funzione in php sarebbe così:
<?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);
?>
Perciò nel DB dovremmo creare i seguenti campi:
(unico, varchar255) hash: 01aad0e51fcd5582b307613842e4ffe5 (testo, senza identificarlo come unico) texto: 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.