-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemporal_Examples.sql
92 lines (58 loc) · 2.31 KB
/
Temporal_Examples.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
FormaServe IBM i Training
For full disclaimer see https://www.formaserve.co.uk/examples.php
© - FormaServe Systems Ltd. 1990 - 2020
www.FormaServe.co.uk
*/
Set Schema Temporal ;
/* Create Temporal Table */
Create Or Replace Table Contact_Info (
Name Varchar(50),
Email Varchar(50),
Tel_No Varchar(50),
Sys_Start Timestamp(12) Not Null Generated Always As Row Begin,
Sys_End Timestamp(12) Not Null Generated Always As Row End,
Ts_Id Timestamp(12) Not Null Generated Always As Transaction Start Id,
Period System_Time (Sys_Start, Sys_End)
) ;
/* Create History Table */
Create Table Hist_Contact_Info Like Contact_Info ;
/* Link the 2 tables together & start temporal versioning */
Alter Table Contact_Info
Add Versioning Use History Table Hist_Contact_Info ;
/* Change an existing table to a temporal table */
Alter Table Employee
Add Column Sys_Start Timestamp(12) Not Null Generated Always As Row Begin
Add Column Sys_End Timestamp(12) Not Null Generated Always As Row End
Add Column Ts_Id Timestamp(12) Not Null Generated Always As Transaction Start Id
Add Period System_Time (Sys_Start, Sys_End) ;
/* Create History Table */
Create Table Hist_Employee Like Employee ;
/* Start versioning on existing table */
Alter Table Employee
Add Versioning Use History Table Hist_Employee ;
/* Create History Table */
Create Table Hist_Clients Like Clients ;
/* Change an existing table to a temporal table */
Alter Table Clients
Add Versioning Use History Table Hist_Clients ;
/* Create History Table */
Create table hist_suppliers like suppliers ;
/* Start Temporal */
Alter table suppliers
add versioning use history table hist_suppliers ;
/* Select employees at a certain time */
Select * from employee
for system_time as of '2020-01-10-10.00.00'
where emid = 86 ;
/* Select employees between a date range, using from & to */
Select * from employee
for system_time from '2020-01-10-00.00.00' to '2020-01-10-17.00.00'
where emid = 86 ;
/* Select employees between a date range */
Select * from employee
for system_time between '2020-01-10-09.00.00' and '2020-01-10-17.00.00'
where emid = 86 ;
/* Stop versioning - Dont need it any more - taking up to much disk! */
alter table employee
drop versioning ;