用随机的2个单词字符串命名表中的每一行 [英] Naming each row in the table with a random 2 word string
问题描述
我想为数据库中的表选择一个简单的命名约定。
I want to choose a simple naming conventions to the table in my database.
这意味着我必须用随机的2个单词字符串来命名每一行。
It means I have to name each row with a random 2 word string.
例如
ID名称
1个ROMEL SUMPI
1 ROMEL SUMPI
2 BORMI SUIEMOD
2 BORMI SUIEMOD
依此类推, 、、、、、、、
and so on,,,,,,,,,
这意味着每个NAME列应具有唯一的名称....
It means each NAME column should have a Unique name.....
如何在使用postgreSQL的PHP中做到这一点DB ....
How can I do this in PHP which uses postgreSQL DB....
在此先感谢您,,。,,,
Thank you in advance,,.,,,,
推荐答案
我怀疑您实际上是在说任意唯一名称。简单的方法可能是:
I suspect you actually mean "arbitrary" unique names. Simple way could be:
INSERT INTO tbl (id, name)
SELECT g, 'name'::text || g || ' surname' || g
FROM generate_series(1, 1000) g;
..生成1000个不同的名称-完全不是随机的,而是唯一的。
.. to generate 1000 distinct names - not random at all, but unique.
要生成100个名称,该名称由两个单词组成,两个单词带有AZ的3-10个随机字母:
To generate 100 names consisting of two words with 3 - 10 random letters from A-Z:
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;
ASCII码 A为65, Z为90。幸运的是,范围介于基本大写字母之间。您可以通过 ascii()
函数,它与 chr()
:
ASCII code of 'A' is 65, the one of 'Z' is 90. Fortunately, the range between spans the basic upper case alphabet. You can find out with the ascii()
function, which is the reverse of chr()
:
SELECT ascii('A')
第二种方法不能保证唯一性,但只有几百个名称几乎不可能重复。消除可能的重复是微不足道的。添加另一个 SELECT
层,您在其中按名称分组
并选择 min(id)
。
The second method doesn't guarantee uniqueness, but duplicates are extremely unlikely with just a few hundred names. Eliminating possible duplicates is trivial. Add another SELECT
layer where you GROUP BY name
and pick min(id)
.
这篇关于用随机的2个单词字符串命名表中的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!