Mysql
 sql >> Database >  >> RDS >> Mysql

MySQL Alter table, aggiungi una colonna con un valore casuale univoco

-- tbl_name: Table
-- column_name: Column
-- chars_str: String containing acceptable characters
-- n: Length of the random string
-- dummy_tbl: Not a parameter, leave as is!
UPDATE tbl_name SET column_name = (
  SELECT GROUP_CONCAT(SUBSTRING(chars_str , 1+ FLOOR(RAND()*LENGTH(chars_str)) ,1) SEPARATOR '')
  FROM (SELECT 1 /* UNION SELECT 2 ... UNION SELECT n */) AS dummy_tbl
);

-- Example
UPDATE tickets SET code = (
  SELECT GROUP_CONCAT(SUBSTRING('[email protected]' , 1+ FLOOR(RAND()*LENGTH('[email protected]'))     ,1) SEPARATOR '')
  FROM (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5) AS dummy_tbl
);