In an app I am writing I am automatically generating a "callsign" for each user when they first register with the server. The callsign bears the format adjective adjective creature. The underlying Postgres tables from which I pick these values are as follows
CREATE TABLE adjectives ("adj" character varying(16) NOT NULL,"quality" smallint NOT NULL,
CONSTRAINT "adjectives_adj_key" UNIQUE ("adj")) WITH (oids = false);
INSERT INTO adjectives (adj,quality) VALUES ('bold',1),('witty',1),('red',5),('old',3);
CREATE TABLE creatures ("creature" character varying(16) NOT NULL,"quality" smallint NOT NULL,
CONSTRAINT "creatures_creature_key" UNIQUE ("creature")) WITH (oids = false);
INSERT INTO creatures (creature,quality) VALUES ('tiger',1),('dog',1),('sparrow',5),
('dolphin',3);
When generating a new callsign I need to do the following
- Pick two adjectives and a creature at random from their respective tables
- Order the adjectives in ascending order of quality. In order to respect the rules of English grammar I need the right adjective order.
Big Red Bull, for instance is right.Red Big Bullis not. - I need to check that the generated call sign does not already exist in the users table shown below
CREATE TABLE users ("user" character varying(16) NOT NULL,"callsign CHARACTER VARYING(32) NOT NULL);
The users table has much else - I have trimmed the details.
While I can work with basic SQL selects and do the rest in server-side script I suspect that there is a smart SQL only way of doing much, if not all, of the above in order to generate a unique, grammatically valid callsign. My rather limited knowledge of SQL is not quite up to that task. How can this be done?