DB2 – 修改表格添加具有唯一默认值(UUID)的列

huangapple go评论69阅读模式
英文:

DB2 - ALTER table ADD column with unique default value (UUID)

问题

我需要更新一个现有的表,添加一个新列,其默认值为UUID,数据类型为VARCHAR(255)。

我尝试通过编写以下函数来实现:

CREATE FUNCTION UUID_FUNC()
    RETURNS VARCHAR(255)
    LANGUAGE SQL 
    BEGIN ATOMIC
    DECLARE UUID VARCHAR(4000);
    SET UUID = (SELECT TRIM(CHAR(HEX(GENERATE_UNIQUE()))) from sysibm.sysdummy1);
    RETURN UUID;
END

并在查询中使用它:

ALTER TABLE <表名>
    ADD ID_VALUE VARCHAR(255) NOT NULL DEFAULT (UUID_FUNC())

在执行上述查询时,出现以下错误:

SQL Error [42601]: An unexpected token "DEFAULT" was found following "ARCHAR(255) NOT NULL".  
Expected tokens may include:  "CHECK".. SQLCODE=-104, SQLSTATE=42601, DRIVER=3.59.81

请问在ALTER查询中调用自定义定义的函数的正确格式是什么,或者是否有任何建议可以实现上述需求?

提前感谢。

英文:

I need to update an existing table by adding a new column with default value as UUID and datatype as VARCHAR(255).

I tried to achieved it by writing a function as:

CREATE FUNCTION UUID_FUNC()
     RETURNS VARCHAR(255)
     LANGUAGE SQL 
     BEGIN ATOMIC
     DECLARE UUID VARCHAR(4000);
     SET UUID = (SELECT TRIM(CHAR(HEX(GENERATE_UNIQUE()))) from sysibm.sysdummy1);
     RETURN UUID;
END

And used it in the query as:

ALTER TABLE &lt;Table-name&gt; 
	ADD ID_VALUE VARCHAR(255) NOT NULL DEFAULT (UUID_FUNC())

Got following error when executing the above query:

SQL Error [42601]: An unexpected token &quot;DEFAULT&quot; was found following &quot;ARCHAR(255) NOT NULL&quot;.  
Expected tokens may include:  &quot;CHECK&quot;.. SQLCODE=-104, SQLSTATE=42601, DRIVER=3.59.81

What is the correct format for calling custom defined functions in ALTER query or any suggestions to achieve the above requirement is appreciated.

Thanks in advance.

答案1

得分: 2

以下是翻译好的部分:

我能够以以下方式实现它:

ALTER TABLE <表名> ADD ID_VALUE VARCHAR(255) NOT NULL DEFAULT '0';

UPDATE <表名> SET ID_VALUE=(hex(GENERATE_UNIQUE())) WHERE ID_VALUE='0';

英文:

I was able to achieve it in following way:

ALTER TABLE &lt;Table-name&gt; ADD ID_VALUE VARCHAR(255) NOT NULL DEFAULT &#39;0&#39;;

UPDATE &lt;Table-name&gt; SET ID_VALUE=(hex(GENERATE_UNIQUE())) WHERE ID_VALUE=&#39;0&#39;;

答案2

得分: 1

需要通过触发器来实现这一点,而不是作为生成表达式。给定以下DDL:

create or replace function your.uuid_func()
   returns char(26)
   language sql
   not deterministic
   return values(hex(generate_unique()));

create table your.table (
   c1 int not null,
   c2 char(26) not null
);

您可以创建触发器:

create trigger set_uuid
   before insert on your.table
   referencing new as n
   for each row
   when (n.id_value is null)
      set n.id_value = your.uuid_func();

然后进行插入操作:

-- 您可以插入普通字符串:
insert into your.table (c1, c2)
   values (1, 'anything');

-- 或者,如果不提供c2的值,它将被设置为UUID_FUNC()的值:
insert into your.table (c1) 
    values (2);

结果:

select * from your.table;

C1          C2                        
----------- --------------------------
          1 anything                     
          2 20200111154913255440000000
英文:

You would need to do this via a trigger, rather than as a generated expression. Given the DDL:

create or replace function your.uuid_func()
   returns char(26)
   language sql
   not deterministic
   return values(hex(generate_unique()));

create table your.table (
   c1 int not null,
   c2 char(26) not null
);

You can create the trigger:

create trigger set_uuid
   before insert on your.table
   referencing new as n
   for each row
   when (n.id_value is null)
      set n.id_value = your.uuid_func();

Then the insert:

—- You can insert a normal string:
insert into your.table (c1, c2)
   values (1, ‘anything’);


—- Or, if you don’t provide a value for c2, it will be set
—- to the value of UUID_FUNC():

insert into your.table (c1) 
    values (2);

Results:

select * from your.table;

C1          C2                        
----------- --------------------------
          1 anything                     
          2 20200111154913255440000000

答案3

得分: -1

ALTER TABLE ADD ID_VALUE AS (UUID_FUNC());

For accessing existing field values,
ALTER TABLE ADD ID_VALUE AS (UUID_FUNC([field_name]));

英文:

ALTER TABLE <Table-name> ADD ID_VALUE AS (UUID_FUNC());

For accessing existing field values,
ALTER TABLE <Table-name> ADD ID_VALUE AS (UUID_FUNC([field_name]));

huangapple
  • 本文由 发表于 2020年1月6日 20:14:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定