Du kan gøre det efter kolonne eller summen af kolonner. Nedenfor er en test på mit bord.
SELECT sum(crc32(email)) as crc from users;
+-------------+
| crc |
+-------------+
| 10679459550 |
+-------------+
select sum(crc32(concat(user_id,first_name,last_name,email,reportingManager))) as crc from users;
+------------+
| crc |
+------------+
| 7196315383 |
+------------+
Rediger
I en lagret procedure, der sendes et databasenavn og et tabelnavn, vil nedenstående returnere crc. Den går til den særlige INFORMATION_SCHEMA db for at hente kolonnenavnene for dig og bruger alle disse kolonner og værdier til at generere en kontrolsum.
Lagret procedure:
drop procedure if exists getTableCRC32;
DELIMITER $$
create procedure getTableCRC32
( dbname varchar(80),
tableName varchar(80)
)
BEGIN
set @sql1="select GROUP_CONCAT(`column_name` SEPARATOR ',') into @colNames";
set @sql1=concat(@sql1," FROM `INFORMATION_SCHEMA`.`COLUMNS`");
set @sql1=concat(@sql1," WHERE `TABLE_SCHEMA`='",dbName,"'");
set @sql1=concat(@sql1," AND `TABLE_NAME`='",tableName,"'");
-- select @sql1;
PREPARE stmt1 FROM @sql1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
-- select @colNames;
set @sql2=concat( 'select sum(crc32(concat(', @colNames, '))) as crc from ',tableName);
-- select @sql2;
PREPARE stmt2 FROM @sql2;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
END
$$
DELIMITER ;
Test det:
call getTableCRC32('so_gibberish','users');
+------------+
| crc |
+------------+
| 7196315383 |
+------------+
call getTableCRC32('so_gibberish','fish');
+------------+
| crc |
+------------+
| 3273020843 |
+------------+