次のSQL文は通常の非テンポラル テーブルを作成し、行をいくつか挿入します。
CREATE MULTISET TABLE employee_systime ( eid INTEGER NOT NULL, ename VARCHAR(10) NOT NULL, deptno INTEGER NOT NULL, sys_start TIMESTAMP(6) WITH TIME ZONE NOT NULL, sys_end TIMESTAMP(6) WITH TIME ZONE NOT NULL ) PRIMARY INDEX(eid); INSERT INTO employee_systime VALUES (1001,'Sania',111,TIMESTAMP'2002-01-01 00:00:00.000000-08:00', TIMESTAMP'9999-12-31 23:59:59.999999+00:00'); INSERT INTO employee_systime VALUES (1002,'Ash',333,TIMESTAMP'2003-07-01 12:11:00.000000-08:00', TIMESTAMP'9999-12-31 23:59:59.999999+00:00'); INSERT INTO employee_systime VALUES (1003,'SRK',111,TIMESTAMP'2004-02-10 00:00:00.000000-08:00', TIMESTAMP'2006-03-01 00:00:00.000000-08:00'); INSERT INTO employee_systime VALUES (1004,'Fred',222, TIMESTAMP'2002-07-01 12:00:00.350000-08:00', TIMESTAMP'2005-05-01 12:00:00.350000-08:00'); INSERT INTO employee_systime VALUES (1005,'Alice',222,TIMESTAMP'2004-12-01 00:12:23.120000-08:00', TIMESTAMP'2005-05-01 12:00:00.450000-08:00'); INSERT INTO employee_systime VALUES (1004,'Fred',555, TIMESTAMP'2005-05-01 12:00:00.350000-08:00', TIMESTAMP'9999-12-31 23:59:59.999999+00:00'); INSERT INTO employee_systime VALUES (1005,'Alice',555,TIMESTAMP'2005-05-01 12:00:00.450000-08:00', TIMESTAMP'9999-12-31 23:59:59.999999+00:00');
このテーブルはまだテンポラル テーブルではないため、テーブルの無条件のSELECTは、行がシステム時間でオープンかクローズかに関わらず、すべての行を返します。
SELECT * FROM employee_systime; eid ename deptno sys_start sys_end ---- ------ ------ -------------------------------- -------------------------------- 1002 Ash 333 2003-07-01 12:11:00.000000-08:00 9999-12-31 23:59:59.999999+00:00 1005 Alice 222 2004-12-01 00:12:23.120000-08:00 2005-05-01 12:00:00.450000-08:00 1004 Fred 222 2002-07-01 12:00:00.350000-08:00 2005-05-01 12:00:00.350000-08:00 1005 Alice 555 2005-05-01 12:00:00.450000-08:00 9999-12-31 23:59:59.999999+00:00 1004 Fred 555 2005-05-01 12:00:00.350000-08:00 9999-12-31 23:59:59.999999+00:00 1001 Sania 111 2002-01-01 00:00:00.000000-08:00 9999-12-31 23:59:59.999999+00:00 1003 SRK 111 2004-02-10 00:00:00.000000-08:00 2006-03-01 00:00:00.000000-08:00
2つのALTER TABLE文でテーブルをsystem-timeテンポラル テーブルに変更できます。
ALTER TABLE employee_systime ADD PERIOD FOR SYSTEM_TIME(sys_start,sys_end) ADD sys_start TIMESTAMP(6) WITH TIME ZONE NOT NULL GENERATED ALWAYS AS ROW START ADD sys_end TIMESTAMP(6) WITH TIME ZONE NOT NULL GENERATED ALWAYS AS ROW END; ALTER TABLE employee_systime ADD SYSTEM VERSIONING;
ここで、無条件のSELECTにはシステム時間でオープンの行のみ表示されます。
SELECT * FROM employee_systime; eid ename deptno sys_start sys_end ---- ------ ------ -------------------------------- -------------------------------- 1002 Ash 333 2003-07-01 12:11:00.000000+00:00 9999-12-31 23:59:59.999999+00:00 1001 Sania 111 2002-01-01 00:00:00.000000+00:00 9999-12-31 23:59:59.999999+00:00 1001 Fred 222 2002-07-01 12:00:00.350000+00:00 9999-12-31 23:59:59.999999+00:00 1003 Alice 222 2004-12-01 00:12:23.120000+00:00 9999-12-31 23:59:59.999999+00:00
特別なテンポラル修飾子でsystem-timeテーブルのクローズ行を表示できます。詳細については、ANSI System-Timeテーブルの問合わせを参照してください。