{
  "date": "2016-01-02T20:05:09",
  "slug": "flashback-table-after-multiple-drop",
  "link": "https://www.dbi-services.com/blog/flashback-table-after-multiple-drop/",
  "title": {
    "rendered": "Flashback table after multiple drop"
  },
  "content": {
    "rendered": "<h2>By Franck Pachot</h2>\n<p>.<br />\nFLASHBACK TABLE restores the latest version that is available in recycle bin. If you did multiple drop / create you may want to restore oldest versions. Of course it&#8217;s <a href=\"https://docs.oracle.com/database/121/SQLRF/statements_9012.htm#SQLRF55003\" target=\"_blank\" rel=\"noopener noreferrer\">documented</a> &#8211; everything is in the doc. But an example may be useful to understand it before you need it.<br />\n<!--more--><br />\nLet&#8217;s create and drop several times the DEMO table. I change the column name so that I can check easily which one has been restored</p>\n<pre><code>\n20:03:10 SQL&gt; create table DEMO(id number constraint DEMOPK primary key , C1 char );\nTable created.\n20:03:12 SQL&gt; drop table DEMO;\nTable dropped.\n20:03:12 SQL&gt; create table DEMO(id number constraint DEMOPK primary key , C2 char );\nTable created.\n20:03:14 SQL&gt; drop table DEMO;\nTable dropped.\n20:03:14 SQL&gt; create table DEMO(id number constraint DEMOPK primary key , C3 char );\nTable created.\n20:03:16 SQL&gt; drop table DEMO;\nTable dropped.\n20:03:16 SQL&gt; create table DEMO(id number constraint DEMOPK primary key , C4 char );\nTable created.\n20:03:18 SQL&gt; drop table DEMO;\nTable dropped.\n20:03:18 SQL&gt; create table DEMO(id number constraint DEMOPK primary key , C5 char );\nTable created.\n20:03:20 SQL&gt; drop table DEMO;\nTable dropped.\n20:03:20 SQL&gt; create table DEMO(id number constraint DEMOPK primary key , C6 char );\nTable created.\n20:03:22 SQL&gt; drop table DEMO;\nTable dropped.\n</code></pre>\n<p>Here is what I have in recycle bin</p>\n<pre><code>\n20:03:22 SQL&gt; select object_name,original_name,type,dropscn,createtime,droptime from user_recyclebin order by dropscn;\n&nbsp;\nOBJECT_NAME                    ORIGINAL_N TYPE          DROPSCN CREATETIME          DROPTIME\n------------------------------ ---------- ---------- ---------- ------------------- -------------------\nBIN$KF+J+xYlFjngU3VOqMB9Kw==$0 DEMOPK     INDEX         4350801 2016-01-02:20:03:10 2016-01-02:20:03:12\nBIN$KF+J+xYmFjngU3VOqMB9Kw==$0 DEMO       TABLE         4350804 2016-01-02:20:03:10 2016-01-02:20:03:12\nBIN$KF+J+xYoFjngU3VOqMB9Kw==$0 DEMOPK     INDEX         4350830 2016-01-02:20:03:12 2016-01-02:20:03:14\nBIN$KF+J+xYpFjngU3VOqMB9Kw==$0 DEMO       TABLE         4350833 2016-01-02:20:03:12 2016-01-02:20:03:14\nBIN$KF+J+xYrFjngU3VOqMB9Kw==$0 DEMOPK     INDEX         4350857 2016-01-02:20:03:14 2016-01-02:20:03:16\nBIN$KF+J+xYsFjngU3VOqMB9Kw==$0 DEMO       TABLE         4350861 2016-01-02:20:03:14 2016-01-02:20:03:16\nBIN$KF+J+xYuFjngU3VOqMB9Kw==$0 DEMOPK     INDEX         4350885 2016-01-02:20:03:16 2016-01-02:20:03:18\nBIN$KF+J+xYvFjngU3VOqMB9Kw==$0 DEMO       TABLE         4350889 2016-01-02:20:03:16 2016-01-02:20:03:18\nBIN$KF+J+xYxFjngU3VOqMB9Kw==$0 DEMOPK     INDEX         4350912 2016-01-02:20:03:18 2016-01-02:20:03:20\nBIN$KF+J+xYyFjngU3VOqMB9Kw==$0 DEMO       TABLE         4350915 2016-01-02:20:03:18 2016-01-02:20:03:20\nBIN$KF+J+xY0FjngU3VOqMB9Kw==$0 DEMOPK     INDEX         4350939 2016-01-02:20:03:20 2016-01-02:20:03:22\nBIN$KF+J+xY1FjngU3VOqMB9Kw==$0 DEMO       TABLE         4350943 2016-01-02:20:03:20 2016-01-02:20:03:22\n&nbsp;\n12 rows selected.\n</code></pre>\n<p>and my goal now is to restore a previous version.</p>\n<h3>issue flashback multiple times</h3>\n<p>So the documentation tells to issue multiple flashback commands:</p>\n<pre><code>\n20:03:22 SQL&gt; flashback table DEMO to before drop;\nFlashback complete.\n&nbsp;\n20:03:22 SQL&gt; desc DEMO;\n Name           Null?    Type\n -------------- -------- --------\n ID             NOT NULL NUMBER\n C6                      CHAR(1)\n</code></pre>\n<p>that&#8217;s the latest version. Let&#8217;s issue the command again:</p>\n<pre><code>\n20:03:22 SQL&gt; flashback table DEMO to before drop;\nflashback table DEMO to before drop\n*\nERROR at line 1:\nORA-38312: original name is used by an existing object\n</code></pre>\n<p>Yes of course, I have to drop it before.</p>\n<pre><code>\n20:03:22 SQL&gt; drop table DEMO;\nTable dropped.\n&nbsp;\n20:03:22 SQL&gt; flashback table DEMO to before drop;\nFlashback complete.\n&nbsp;\n20:03:22 SQL&gt; desc DEMO;\n Name           Null?    Type\n -------------- -------- --------\n ID             NOT NULL NUMBER\n C6                      CHAR(1)\n</code></pre>\n<p>Ok, as I dropped it just before, that the latest version that is restored&#8230;</p>\n<h3>purge</h3>\n<p>If I want to issue multiple flashback table commands, I have to drop purge so that the intermediate restored tables don&#8217;t go to recycle bin</p>\n<pre><code>\n20:03:22 SQL&gt; drop table DEMO purge;\nTable dropped.\n&nbsp;\n20:03:23 SQL&gt; flashback table DEMO to before drop;\nFlashback complete.\n&nbsp;\n20:03:23 SQL&gt; desc DEMO;\n Name           Null?    Type\n -------------- -------- --------\n ID             NOT NULL NUMBER\n C5                      CHAR(1)\n</code></pre>\n<p>that&#8217;s fine: I restored the N-1 version.</p>\n<h3>rename</h3>\n<p>The other solution is to restore it to another table, dropping that other table, or changing name each time:</p>\n<pre><code>\n20:03:23 SQL&gt; flashback table DEMO to before drop rename to DEMO1;\nFlashback complete.\n&nbsp;\n20:03:23 SQL&gt; desc DEMO1;\n Name           Null?    Type\n -------------- -------- --------\n ID             NOT NULL NUMBER\n C4                      CHAR(1)\n&nbsp;\n20:03:23 SQL&gt; flashback table DEMO to before drop rename to DEMO2;\nFlashback complete.\n&nbsp;\n20:03:23 SQL&gt; desc DEMO2;\n Name           Null?    Type\n -------------- -------- --------\n ID             NOT NULL NUMBER\n C3                      CHAR(1)\n</code></pre>\n<p>Here I rewind two older versions.</p>\n<h3>name the recycle bin object</h3>\n<p>But there is a direct possibility if you know the version you want from the DBA_RECYCLEBIN view.</p>\n<pre><code>\n20:03:23 SQL&gt; desc \"BIN$KF+J+xYpFjngU3VOqMB9Kw==$0\"\n Name           Null?    Type\n -------------- -------- --------\n ID             NOT NULL NUMBER\n C2                      CHAR(1)\n</code></pre>\n<p>And I restore that directly to a new table:</p>\n<pre><code>\n20:03:23 SQL&gt; flashback table \"BIN$KF+J+xYpFjngU3VOqMB9Kw==$0\" to before drop rename to DEMO3;\nFlashback complete.\n</code></pre>\n<p>So that&#8217;s probably the fastest way to restore an old version.</p>\n<p>All that is possible because each time we flashback to before drop, the restored version is removed from the recycle bin.<br />\nFrom my example, only one remains here:</p>\n<pre><code>\n20:03:24 SQL&gt; select object_name,original_name,type,dropscn,createtime,droptime from user_recyclebin order by dropscn;\n&nbsp;\nOBJECT_NAME                    ORIGINAL_N TYPE          DROPSCN CREATETIME          DROPTIME\n------------------------------ ---------- ---------- ---------- ------------------- -------------------\nBIN$KF+J+xYlFjngU3VOqMB9Kw==$0 DEMOPK     INDEX         4350801 2016-01-02:20:03:10 2016-01-02:20:03:12\nBIN$KF+J+xYmFjngU3VOqMB9Kw==$0 DEMO       TABLE         4350804 2016-01-02:20:03:10 2016-01-02:20:03:12\n</code></pre>\n<p>so the safest way is probably to flashback to a different table name each time, and clean that only when you&#8217;re sure you don&#8217;t need them anymore.</p>\n",
    "protected": false
  }
}
