postgresql
7.1
insert into secret_tokens(token_id, token, timestamp)
values (sec_security_token_id_seq.nextval, :random_token, now())
select token_id, token
from secret_tokens,
(select trunc(random()*(select count(*)-15 from secret_tokens))::integer as first) r
where token_id >= r.first and r.first+15 > token_id;
update users
set second_to_last_visit = last_visit,
last_visit = now(),
n_sessions = n_sessions + 1
where user_id = :user_id