Whenever you are populating table columns with dummy data you’ll probably need to generate some random strings.
This is very easy in YugabyteDB with the combination of several YSQL built-in functions, and you can create a stored function to encapsulate the logic!
Example:
CREATE OR REPLACE FUNCTION randomstring(INT)
RETURNS VARCHAR AS
$$
DECLARE
passedValue INT:=$1;
rstring VARCHAR:='';
BEGIN
SELECT string_agg(substring('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', ((RANDOM() * 51) + 1)::INT, 1), '') INTO rstring
FROM generate_series(1, passedValue);
RETURN rstring;
END;
$$ language 'plpgsql';
Example:
yugabyte=> CREATE OR REPLACE FUNCTION randomstring(INT)
yugabyte-> RETURNS VARCHAR AS
yugabyte-> $$
yugabyte$> DECLARE
yugabyte$> passedValue INT:=$1;
yugabyte$> rstring VARCHAR:='';
yugabyte$> BEGIN
yugabyte$> SELECT string_agg(substring('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', ((RANDOM() * 51) + 1)::INT, 1), '') INTO rstring
yugabyte$> FROM generate_series(1, passedValue);
yugabyte$> RETURN rstring;
yugabyte$> END;
yugabyte$> $$ language 'plpgsql';
CREATE FUNCTION
yugabyte=> SELECT randomstring(5);
randomstring
--------------
UWaAw
(1 row)
yugabyte=> SELECT randomstring(15);
randomstring
----------------
cHkKaeDqtjcDUK
(1 row)
yugabyte=> SELECT randomstring(10) || ' ' || randomstring(10) full_name;
full_name
-----------------------
tWFKoJicXy UUPlnbYeVB
(1 row)
Have Fun!