Quickly Populate an SQL Table with Fake Data Print

  • 6

This brilliant little snippet came from Robert Eisele in Germany, whos site carries an amazing wealth of opinions on development and and code snippets.  The example has been updated to include a few other elements that we find common.  In the example we are generating a random timestamp between 2014-01-01 and 2014-01-01 + 90 days.

CREATE TABLE t1 (
  fixed_val TINYINT UNSIGNED NOT NULL,
  date_val datetime NOT NULL,
  int_val INT UNSIGNED NOT NULL
);
TRUNCATE t1;
INSERT INTO t1  
(
  fixed_val,
  date_val,
  int_val
)
SELECT
  1
  , FROM_UNIXTIME(UNIX_TIMESTAMP('2014-01-01') + (RAND() * (86400 * 90)))
  , FLOOR(RAND() * 1000)
FROM information_schema.tables
LIMIT 50;

Source: http://www.xarg.org/2012/08/running-standard-deviation-in-mysql/
Source: http://www.phpied.com/random-mysql-date/


Was this answer helpful?

« Back