{"id":236,"date":"2023-02-22T17:11:28","date_gmt":"2023-02-22T16:11:28","guid":{"rendered":"http:\/\/www.sepchat.com\/SC\/?p=236"},"modified":"2023-02-23T15:09:15","modified_gmt":"2023-02-23T14:09:15","slug":"ibm-i-creer-une-table-temporelle","status":"publish","type":"post","link":"http:\/\/www.sepchat.com\/SC\/index.php\/2023\/02\/22\/ibm-i-creer-une-table-temporelle\/","title":{"rendered":"IBM i &#8211; Cr\u00e9er une table temporelle"},"content":{"rendered":"\n<p>Depuis la V7R3 IBM a introduit la notion de table temporelle.Cette fonctionnalit\u00e9 permet de traiter de mani\u00e8re simple vos historiques des donn\u00e9es (alimentation, consultation).<\/p>\n\n\n\n<p>&nbsp;La mise en oeuvre est relativement simple.<\/p>\n\n\n\n<p>Les tables temporelles fonctionnent par paire. Une table pour vos donn\u00e9es, une table pour l&rsquo;historique.<\/p>\n\n\n\n<p>La premi\u00e8re table contient toutes les donn\u00e9es de base m\u00e9tier, (vous pouvez utiliser une table d\u00e9j\u00e0 existante). Il faudra lui ajouter 3 champs d&rsquo;horodatage pour pouvoir dater les enregistrements.<\/p>\n\n\n\n<p>La seconde table, contient l&rsquo;historique des donn\u00e9es et a la m\u00eame structiure que la premi\u00e8re table (champs d&rsquo;horodatage compris).<\/p>\n\n\n\n<p>Par exemple, la table des d\u00e9partements peut \u00eatre d\u00e9finie comme suit :&nbsp;<\/p>\n\n\n\n<p><strong>CREATE OR REPLACE TABLE<\/strong>&nbsp;DEPARTMENT<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(DEPTNO&nbsp;&nbsp;&nbsp;&nbsp;<strong>CHAR<\/strong>(3)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>NOT NULL<\/strong>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DEPTNAME&nbsp;&nbsp;<strong>VARCHAR<\/strong>(36)&nbsp;&nbsp;&nbsp;<strong>NOT NULL<\/strong>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MGRNO&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>CHAR<\/strong>(6),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADMRDEPT&nbsp;&nbsp;<strong>CHAR<\/strong>(3)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>NOT NULL<\/strong>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LOCATION&nbsp;&nbsp;<strong>CHAR<\/strong>(16),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;START_TS&nbsp;&nbsp;<strong>TIMESTAMP<\/strong>(12)&nbsp;<strong>NOT NULL GENERATED ALWAYS AS ROW BEGIN<\/strong>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END_TS&nbsp;&nbsp;&nbsp;&nbsp;<strong>TIMESTAMP<\/strong>(12)&nbsp;<strong>NOT NULL GENERATED ALWAYS AS ROW END<\/strong>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TS_ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>TIMESTAMP<\/strong>(12)&nbsp;<strong>GENERATED ALWAYS AS TRANSACTION START ID<\/strong>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>PERIOD SYSTEM_TIME<\/strong>&nbsp;(START_TS, END_TS),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>PRIMARY KEY<\/strong>&nbsp;(DEPTNO))<\/p>\n\n\n\n<p>Ici les champs d\u00e9di\u00e9s \u00e0 la table temporelle sont START_TS, END_TS etTS_ID.<\/p>\n\n\n\n<p>&nbsp;Puis, on cr\u00e9e la table historique :<\/p>\n\n\n\n<p><strong>CREATE TABLE<\/strong>&nbsp;DEPARTMENT_HIST&nbsp;<strong>LIKE<\/strong>&nbsp;DEPARTMENT<\/p>\n\n\n\n<p>Enfin, on lie les 2 tables :<\/p>\n\n\n\n<p><strong>ALTER TABLE<\/strong>&nbsp;DEPARTMENT&nbsp;<strong>ADD VERSIONING USE HISTORY TABLE<\/strong>&nbsp;DEPARTMENT_HIST<\/p>\n\n\n\n<p>Il est ensuite facile de consulter la table telle qu&rsquo;elle \u00e9tait il y a 6 mois par exemple :<\/p>\n\n\n\n<p><strong>SELECT * FROM<\/strong>\u00a0DEPARTMENT\u00a0<strong>FOR SYSTEM_TIME AS OF CURRENT TIMESTAMP<\/strong>\u00a0&#8211; 6\u00a0<strong>MONTHS<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized size-full has-custom-border\"><a href=\"windows\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.sepchat.com\/SC\/wp-content\/uploads\/2023\/02\/fleche-r-edited.png\" alt=\"\" width=\"57\" height=\"57\"\/><\/a><\/figure>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex\">\n<div class=\"wp-block-button is-style-outline\"><a class=\"wp-block-button__link wp-element-button\" style=\"border-radius:100px\">Cet article vous a \u00e9t\u00e9 utile ? N\u2019h\u00e9sitez pas \u00e0 le partager.<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Depuis la V7R3 IBM a introduit la notion de table temporelle.Cette fonctionnalit\u00e9 permet de traiter de mani\u00e8re simple vos historiques des donn\u00e9es (alimentation, consultation). &nbsp;La mise en oeuvre est relativement simple. Les tables temporelles fonctionnent par paire. Une table pour vos donn\u00e9es, une table pour l&rsquo;historique. La premi\u00e8re table contient toutes les donn\u00e9es de base [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"http:\/\/www.sepchat.com\/SC\/index.php\/wp-json\/wp\/v2\/posts\/236"}],"collection":[{"href":"http:\/\/www.sepchat.com\/SC\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sepchat.com\/SC\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sepchat.com\/SC\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sepchat.com\/SC\/index.php\/wp-json\/wp\/v2\/comments?post=236"}],"version-history":[{"count":2,"href":"http:\/\/www.sepchat.com\/SC\/index.php\/wp-json\/wp\/v2\/posts\/236\/revisions"}],"predecessor-version":[{"id":350,"href":"http:\/\/www.sepchat.com\/SC\/index.php\/wp-json\/wp\/v2\/posts\/236\/revisions\/350"}],"wp:attachment":[{"href":"http:\/\/www.sepchat.com\/SC\/index.php\/wp-json\/wp\/v2\/media?parent=236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sepchat.com\/SC\/index.php\/wp-json\/wp\/v2\/categories?post=236"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sepchat.com\/SC\/index.php\/wp-json\/wp\/v2\/tags?post=236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}