BEGIN;
DROP TABLE IF EXISTS data, referenced;
CREATE TABLE referenced(
    referenced_id serial primary key,
    data int NOT NULL
);

CREATE TABLE data(
    data_id serial primary key,
    referenced_id int NOT NULL REFERENCES referenced,
    data int NOT NULL
);

INSERT INTO referenced SELECT nextval('referenced_referenced_id_seq'), 1 FROM generate_series(1, 20);

INSERT INTO data(referenced_id, data)
   SELECT trunc(random()*20+1)::int, g.i
   FROM generate_series(1, 100000) g(i);
COMMIT;
