Bonjour !
Si vous commencez à programmer et à toucher aux bases de données, vous rencontrerez un jour ce problème. Les champs text ne peuvent pas être uniques dans MySQL, mais il existe une solution très simple : un identifiant unique équivalent à ce hash.
Disons que nous avons le texte long suivant :
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.
Si nous utilisons la fonction https://www.php.net/manual/es/function.md5.php de php ou celle de https://dev.mysql.com/doc/refman/8.0/en/encryption-functions.html#function_md5, nous pourrons obtenir le résultat unique suivant :
01aad0e51fcd5582b307613842e4ffe5
De cette façon, nous aurions un hash pour vérifier s'il existe ou non sans qu'il occupe un grand espace (le chiffrement consomme plus de CPU, mais pas d'espace dans la base de données).
La fonction en php serait ainsi :
<?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);
?>
Par conséquent, dans la DB, nous devrions créer les champs suivants :
(unique, varchar255) hash : 01aad0e51fcd5582b307613842e4ffe5 (texte, sans l'identifier comme unique) 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.