Skip to content

Instantly share code, notes, and snippets.

@simonw
Last active March 31, 2023 18:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonw/c16ce01244760e186a3a0aa3fee0405d to your computer and use it in GitHub Desktop.
Save simonw/c16ce01244760e186a3a0aa3fee0405d to your computer and use it in GitHub Desktop.
-- Create the 'nodes' table in SQLite
CREATE TABLE nodes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
data TEXT
);
-- Create the 'edges' table in SQLite
CREATE TABLE edges (
previous_node INTEGER REFERENCES nodes(id),
next_node INTEGER REFERENCES nodes(id),
PRIMARY KEY (previous_node, next_node)
);
-- Insert random data into the 'nodes' table in SQLite
WITH RECURSIVE
random_data(cnt, rnd) AS (
SELECT 1, (abs(random()) % 1000000) || '_name'
UNION ALL
SELECT cnt+1, (abs(random()) % 1000000) || '_name'
FROM random_data
WHERE cnt < 1000000
)
INSERT INTO nodes(data)
SELECT rnd FROM random_data;
-- Insert random data into the 'edges' table in SQLite
WITH RECURSIVE
random_edges(cnt, previous_node, next_node) AS (
SELECT 1, abs(random()) % 1000000 + 1, abs(random()) % 1000000 + 1
UNION ALL
SELECT cnt+1, abs(random()) % 1000000 + 1, abs(random()) % 1000000 + 1
FROM random_edges
WHERE cnt < 1000000
)
INSERT OR IGNORE INTO edges(previous_node, next_node)
SELECT previous_node, next_node FROM random_edges;
@simonw
Copy link
Author

simonw commented Mar 31, 2023

I created this by sending GPT-4 the following prompt:

-- Create the 'nodes' table in SQLite
CREATE TABLE nodes (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  data TEXT
);

-- Create the 'edges' table in SQLite
CREATE TABLE edges (
  previous_node INTEGER REFERENCES nodes(id),
  next_node INTEGER REFERENCES nodes(id),
  PRIMARY KEY (previous_node, next_node)
);

-- Insert data into the 'nodes' table in SQLite
INSERT INTO nodes (data) VALUES ('Bob');
INSERT INTO nodes (data) VALUES ('Hank');
INSERT INTO nodes (data) VALUES ('Jeff');
INSERT INTO nodes (data) VALUES ('Sally');
INSERT INTO nodes (data) VALUES ('Sue');
INSERT INTO nodes (data) VALUES ('Sam');

-- Insert data into the 'edges' table in SQLite
INSERT INTO edges (previous_node, next_node) VALUES (1, 2);
INSERT INTO edges (previous_node, next_node) VALUES (1, 3);
INSERT INTO edges (previous_node, next_node) VALUES (2, 4);
INSERT INTO edges (previous_node, next_node) VALUES (3, 4);
INSERT INTO edges (previous_node, next_node) VALUES (4, 5);

Rewrite that SQL to use a succinct SQLite recursive query to populate the nodes and edges tables
with random data - one million row in each table

Then following up with:

I got an error about a unique constraint violated on the edges table, rewrite that SQL so that any
existing rows are ignored rather than causing that error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment