How to generate a reference key between rows
As I had some time to think about it, I came up with this solution. I would appriciate a comment on this.

SET @ref = LAST_INSERT_ID() + 1;

INSERT INTO txn (reference) VALUES (@ref), (@ref);


I'm a bit worried about concurrent connections, which could result in identical reference numbers for unrelated transactions.

EDIT:

The upper solution will NOT work, since LAST_INSERT_ID() returns the last inserted ID of the current connection, which in this case will always be 0.

So another idea would be this:

-- This is the parent transaction
INSERT INTO txn (reference) VALUES (0);
-- Get ID of parent
SET @ref = LAST_INSERT_ID();
-- Insert related/child transactions
INSERT INTO txn (reference) VALUES (@ref), (@ref);
-- Update parent with its own ID
UPDATE txn SET reference = @ref WHERE id = @ref;