Updating rows of a table with a UUID column
You can update a UUID column whether or not it is GENERATED BY DEFAULT. You can use the function random_uuid
to generate a random UUID value to update the column. The function random_uuid returns a randomly generated UUID, as a string of 36 characters.
Example 7-23 Updating a UUID Column defined without GENERATED BY DEFAULT clause
Create a table with
id
column as a UUID data type. CREATE TABLE myTable (tabId INTEGER, id STRING AS UUID, PRIMARY KEY (tabId))
Output:Statement completed successfully
Insert data into the table:INSERT INTO myTable values(1,"a81bc81b-dead-4e5d-abff-90865d1e13b1")
Output:Statement completed successfully
Update the id
column using the built-in random_uuid
function.UPDATE myTable set id=random_uuid() where tabId=1
Output:Statement completed successfully
The example above shows how you can update a UUID column which is NOT GENERATED BY DEFAULT. To do so, the UUID column should not be part of the primary key, as NoSQL Primary key values are immutable. In the above example, tabId
is the Primary key. So you can update the UUID column using the random_uuid
function.