IDENTITY値のプログラムによる挿入

値をIDENTITY列にプログラムで挿入する場合は、特別な考慮事項が発生します。この項では、存在する問題とput()およびその他のメソッドを使用してその問題を回避する方法を示します。

次のいずれかの選択肢を使用して、表に各IDENTITY列を作成します。
  • GENERATED ALWAYS AS IDENTITY
  • GENERATED BY DEFAULT AS IDENTITY
  • GENERATED BY DEFAULT ON NULL AS IDENTITY
また、アイデンティティ列は主キーになることがあり、その場合はIDENTITY値を変更できません。
アイデンティティ列を作成する各方法は、put関数を使用した行の追加時に、そのバリアントの1つとともにアクティビティに影響します。
  • put (無条件)
  • put if absent (行に値がない場合のみ)
  • put if present (行に値がある場合のみ)
この項では、IDENTITY列の挿入と更新の様々な影響について説明します。
たとえば、GENERATED ALWAYS AS IDENTITYを使用して定義された列を持つ次の表を作成します。IDENTITYフィールドは主キーです。

CREATE Table foo(
  idValue INTEGER GENERATED ALWAYS AS IDENTITY 
  (START WITH 1 INCREMENT BY 1 MAXVALUE 2 NO CYCLE),
  name STRING,
  PRIMARY KEY(idValue)); 

IDENTITY列への行の挿入

foo表に行を挿入するには、アプリケーションで次を実行します。  

L1: TableAPI api = store.getTableAPI(); // Gets the TableAPI for the store 
L2: Table table = api.getTable("foo"); // Gets the Table foo instance 
L3: Row row = table.createRow(); // constructs an empty Row row for Table foo. 
L4: row.put("name", "joe"); // populates the values for the Row row 
L5: api.put(row, null /* previous-row */, null /* write-options */); 

// The client driver recognizes that the system must generate the id values and \
generates value 1 for the id field in the row before putting it in the DB.
L6: System.out.println("Value of idValue: " + row.get("idValue")); // 1 
L7: row.put("name", "smith"); 
L8: api.put(row, null /* previous-row */, null /* write-options */); 
// driver sets id field to 2 
L9: System.out.println("Value of id: " + row.get("idValue")); // 2

注意:

生成されたIDENTITY列の値を取得するには、L6およびL9に示すとおり、IDENTITY列に対してget()コールを使用します。
また、idValueを返すには、次のようにRETURNING idValue句を使用します。

StatementResult sr = store.executeSync("INSERT INTO foo " + "(name) VALUES ('foe') 
RETURNING idValue"); 
int id = sr.iterator().next().get("idValue").asInteger().get();
IDENTITY列の更新

GENERATED ALWAYS AS IDENTITYとして列を定義する場合、SGが常に次の値を指定する必要があるため、IDENTITY列に値を指定することはできません。

次の例は、IDENTITY列に値を指定しようとした場合を示しています。最初の追加であるjoeおよびjohnは問題なく、SGは両方にidValueを指定します。


CREATE TABLE foo(
 idValue INTEGER GENERATED ALWAYS AS IDENTITY, 
 name STRING, PRIMARY KEY (idValue)) 
 api.put(‘joe’) 
 api.put(‘john’) 
 get(idValue, name) or 
select * from foo; 
1, joe 
2, john 
put()メソッドで更新を試行すると、列がGENERATED ALWAYS AS IDENTITYとして定義されている場合に次のエラーが発生します。

api.put(2,’dave’) // error –- cannot specify a value for \
 a column defined as GENERATED ALWAYS AS IDENTITY 

api.putIfPresent (2, ‘dave’) -- The following error occurs first in the code path, 
even though idValue = 2 is present 
// error – user cannot specify a value for \
 IDENTITY column defined as GENERATED ALWAYS

api.putIfPresent (3,’cezar’) -- The following error occurs, first in the code path, 
even though idValue = 3 is NOT present 
// error - user cannot specify a value for \
 IDENTITY column defined as GENERATED ALWAYS

api.putIfPresent (‘hema’) 
//error – a primary key is not provided to look up the record.  
 
putIfAbsent (10, joe) -– is an insert 
// error - user cannot specify a value for \
IDENTITY column defined as GENERATED ALWAYS 
GENERATED ALWAYS AS IDENTITYとして定義された列に対してUPDATEを使用するには、次のようにします。

Create table foo( idValue INTEGER GENERATED ALWAYS AS IDENTITY, 
 name STRING, 
 PRIMARY KEY (idValue))

UPDATE foo SET idValue = 10 WHERE name=joe
// error -  user cannot set a value for an IDENTITY column defined as GENERATED ALWAYS
 UPDATE foo SET name=hema WHERE idValue=2  
// Success! By using the Primary Key value (idValue=2)to locate its name record, 
// you can update the value and hema replaces john
select * from foo
1, joe
2, hema  
PRIMARY KEYではないIDENTITY列に対してputputIfPresentおよびputIfAbsentを使用するには、次のようにします。

Create table Foo(idValue INTEGER GENERATED ALWAYS AS IDENTITY, 
 acctNumber INTEGER, 
 name STRING, 
 PRIMARY KEY (acctNumber) ) 

//Put two acctNumber and name values. 
api.put(100, ‘joe’) 
api.put (200, ‘john’) 

//SG increments the IDENTITY values, 1 and 2:
api.get(idValue, acctNumber, name) 
1, 100, joe 
2, 200, john 

//Attempt to put an idValue
api.put (2, 200, dave) 
// error – Cannot specify a value for IDENTITY column defined as GENERATED ALWAYS   

api.putIfPresent(3, 200, cezar) 
//error – Cannot specify a value for IDENTITY column defined as GENERATED ALWAYS   

api.putIfPresent (400, cezar)  // not IDENTITY column value error 
// error - Cannot specify a primary key (400) that is not present 

api.putIFPresent (200, cezar) 
1, 100, joe
2, 200, cezar
// Success! The IDENTITY value is updated.  
The system generates a value on putIfPresent as the API semantics are to update 
the entire record, and not update fields within the record selectively.
 
api.putIfAbsent (300, hema) 
// Success! IDENTITY idValue was generated (3), and 300, hema were absent
get(idValue, acctNumber, name)
1, 100, joe
2, 200, cezar
3, 300, hema
 
api.putIfAbsent (20, 300, hema)
// error – user cannot specify a value for IDENTITY column defined as GENERATED ALWAYS
 
api.putIfAbsent (300, hema)
//error - no row with primary key = 300 is present 
 
api.putIfAbsent (3,400, hema)
// error – user cannot specify a value for IDENTITY column defined as GENERATED ALWAYS
PRIMARY KEYではないIDENTITY列に対してUPDATEを使用するには、次のようにします。

Create table Foo(idValue INTEGER GENERATED ALWAYS AS IDENTITY, 
 acctNumber INTEGER, 
 name STRING, 
 PRIMARY KEY (acctNumber))
 
select * from foo
1, 100, joe
2, 200, cezar
3, 300, hema
 
UPDATE foo set name= dave, where PRIMARY KEY = 200
// replaces (2, 200, cezar) with (2, 200, dave)
 
select * from foo;
1, 100, joe
2, 200, dave
3, 300, hema
 
UPDATE foo set name=george, where acctNumber=100 
// acctNumber is the PRIMARY KEY
// replaces (1, 100, joe) with (1, 100, george)
select * from foo;
1, 100, george
2, 200, dave
3, 300, hema
 
UPDATE foo set idValue=10, where acctNumber=100 
// acctNumber is the PRIMARY KEY
// error - Cannot specify a value for IDENTITY column defined as GENERATED ALWAYS

PRIMARY KEYである、GENERATED BY DEFAULT AS IDENTITYとして定義されている列に対してput()を使用するには、次の例を確認してください。この場合、IDENTITY列に値を指定しないと、SGによって値が生成されます。値を指定すると、指定した値が使用されます。


Create table foo( idValue INTEGER GENERATED BY DEFAULT AS IDENTITY, 
 name STRING, 
 PRIMARY KEY (idValue))

api.put(‘joe’)
api.put(‘john’)
 
//Since you supplied no idValue, SG supplies them:
get(idValue, name) 
1, joe
2, john

//You supply 4 as the idValue, so system uses it
api.put (4, george) 

get(idValue, name) 
1, joe
2, john
4, george
 
api.put (2, sam) // replaces (2, john)  with (2, sam)
get(idValue, name) 
1, joe
2, sam
4, george

列に対してUPDATE()を使用するには、次のようにします。


select * from foo;
1, joe
2, sam
4, george
 
UPDATE foo SET idValue=3 where name=sam 
// Updates idValue 2 (2, sam) with 3, so becomes 3, sam
select * from foo 
1, joe
3, sam
4, george
IDENTITY列の削除

IDENTITY列を含む行の削除は、その製品の既存の削除ロジックに従います。変更はありません。