Skip to content

Instantly share code, notes, and snippets.

@graffiti-withwind
Last active June 1, 2017 09:46
Show Gist options
  • Save graffiti-withwind/654c3789d1290816e388ce2b299e8a06 to your computer and use it in GitHub Desktop.
Save graffiti-withwind/654c3789d1290816e388ce2b299e8a06 to your computer and use it in GitHub Desktop.
[sql] SQLだけでテストデータを生成する方法
/*
* 参考:
* SQL だけで100万行 のテストデータを用意する方法
* http://qiita.com/chisei/items/c4439adf3d0faedb65ed
* MySQLで簡単にランダムなテストデータを作成する方法
* http://qiita.com/tayasu/items/c5ddfc481d6b7cd8866d
*/
-- 0~9の数字が入ったテーブルを生成
CREATE TABLE digit(num integer);
INSERT INTO digit VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
-- テスト用のテーブルを生成
CREATE TABLE users(id integer, email varchar(255));
-- 連番テーブル用にテンポラリーテーブルを生成
-- 例だと10000件分
CREATE TEMPORARY TABLE num_table(
SELECT d1.num + d2.num*10 + d3.num*100 + d4.num*1000 as num
FROM digit d1, digit d2, digit d3, digit d4
ORDER BY num
);
-- テスト用テーブルにテストデータをインサート
INSERT INTO users(id, email)
SELECT num, CONCAT("user+", num, "@example.com")
FROM num_table
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment