Naming each row in a table with a random 2-row row

I want to select simple naming conventions in a table in my database.

This means that I have to call each line a random 2-line line.

for example

GO NAME

1 ROMEL SUMPI

2 BORMI SUIEMOD

etc.,,,,,

This means that each NAME column must have a unique name .....

How can I do this in PHP, which uses the postgreSQL database ....

Thank you in advance,.,,

0
php postgresql
Dec 02
source share
2 answers

I suspect that you really mean β€œarbitrary” unique names. A simple way could be:

INSERT INTO tbl (id, name) SELECT g, 'name'::text || g || ' surname' || g FROM generate_series(1, 1000) g; 

.. to create 1000 different names - not random, but unique.

To create 100 two-word names with 3 to 10 random letters from AZ:

 INSERT INTO tbl (id, name) SELECT g%100 ,left(string_agg(chr(65 + (random() * 25)::int), ''), 3 + (random() * 7)::int) || ' ' || left(string_agg(chr(65 + (random() * 25)::int), ''), 3 + (random() * 7)::int) FROM generate_series(1, 1000) g GROUP BY 1 ORDER BY 1; 

The ASCII code "A" is 65, one of the "Z" is 90. Fortunately, the range between spaces makes up the main alphabet of upper case. You can find out using the ascii() function, which is in the opposite direction of chr() :

 SELECT ascii('A') 

The second method does not guarantee uniqueness, but duplicates are extremely unlikely with just a few hundred names. Eliminating possible duplicates is trivial. Add another SELECT layer, where you are GROUP BY name and select min(id) .

+1
Dec 03
source share

It looks like you might need a couple of randomly selected words from a dictionary. This is hard to say, given the lack of clarity.

ACCIDENTAL DICTIONARIES

The best way to pick random dictionary words is probably to end PHP using a password generator that does this for you.

You can do this in PostgreSQL using a dictionary table with one word for each row, though:

 SELECT word FROM dictionary ORDER BY random() LIMIT 2; 

Performance will be really terrible with a large dictionary. This can be done much faster if the dictionary does not change and there is a unique word_id with no spaces in the numbering, allowing you to write:

 CREATE OR REPLACE FUNCTION get_random_word() RETURNS text AS $$ SELECT word FROM dictionary WHERE word_id = ( SELECT width_bucket(random(), 0, 1, (SELECT max(word_id) FROM dictionary)) ); $$ LANGUAGE sql; SELECT get_random_word() || ' ' || get_random_word(); 

for a table, for example:

 CREATE TABLE dictionary(word_id serial primary key, word text UNIQUE NOT NULL); 

This will result in consistent results if there are no spaces in the word numbering and if word_id is unique or PRIMARY KEY . He can produce the same word twice. If you want to avoid this, you will need a recursive CTE or some kind of PL / PgSQL.

RANDOM GIBBERISH

If you really want really random strings that are already well described here in Stack Overflow. See How to create a random string suitable for a session id in PostgreSQL? among the others; look at this search .

To ensure uniqueness, just add a UNIQUE . Ask your app test to check if unique_violation was raised when you INSERT edited a row and inserted it with a new random identifier if a violation occurred. If you like, you can automate this using the PL / PgSQL helper routine, although it will still be subject to race between concurrent inserts in different transactions.

+1
Dec 03
source share



All Articles