Category Archives: Oracle

In this we are handling Oracle Database Administration and development task. If provide solution of ORA Errors and configuration Steps for setup in Oracle.

Flashback a dropped table from recycle bin in Oracle

Restore table from Flashback in Oracle

You can flashback a dropped table from recyclebin using flashback table commands

Check the recyclebin is enabled

SQL> show parameter recyclebin
NAME				     TYPE	 VALUE
------------------------------------ ----------- ------------------
recyclebin			     string	 on

Commands to flashback the table after drop commands:

-- Flashback a dropped table from recyclebin using flashback commands:
SQL> SHOW RECYCLEBIN;

SQL> FLASHBACK TABLE "BIN$Fh1VKrueZFngYw8CAAq+WA==$0" TO BEFORE DROP;
OR
SQL> FLASHBACK TABLE SCOTT.TEST TO BEFORE DROP;

--Rename table while flashing it back from recyclebin
SQL> FLASHBACK TABLE SCOTT.TEST TO BEFORE DROP RENAME TO NEW_TEST;

Example to show the use of flashback commands to recover a dropped table:

SQL> create table test (id number);
Table created.

SQL> insert into test values (1);
1 row created.

SQL> insert into test values (2);
1 row created.

SQL> commit;
Commit complete.

SQL> drop table test;
Table dropped.

SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME		     OBJECT TYPE DROP TIME
------------- ------------------------------ ----------- -------------------
TEST	      BIN$Fh1VKrueZFngYw8CAAq+WA==$0 TABLE	 2024-04-15:11:30:03
 
SQL> FLASHBACK TABLE TEST TO BEFORE DROP;
Flashback complete.

SQL> select * from test;
	ID
----------
	 1
	 2
-- As you see recyclebin having one entry for TEST that go away
SQL> show recyclebin;
SQL> 

You can check from this views recycle objects:

Select * from user_recyclebin;

Show recycle is not showing anything in Oracle

Enabled Recycle bin is not showing anything in Oracle

Reason for showing the recyclebin empty is you are able to login with SYS user and using the SYSTEM tablespace. So that it show recycle bin empty.

Check Recycle Bin is enabled

SQL> show parameter recyclebin
NAME		     TYPE	 VALUE
-------------------- ----------- ------------
recyclebin	     string	 on

I am login with the sys user that why its not capturing the result as we are using system tables with SYS user. We can see the example below by connecting with SYS user:

Note: Please connect with Normal user to avoid this error.

SQL> show user 
USER is "SYS"
SQL> create table test (id number);

Table created.

SQL> insert into test values ( 1);

1 row created.

SQL> insert into test values (2);

1 row created.

SQL> commit;

Commit complete.

SQL> drop table test;

Table dropped.

SQL> show recyclebin
SQL> 
SQL> 

Now connect with Normal user SCOTT and show the example again for Recyclebin usage:

SQL> show pdbs;
    CON_ID CON_NAME			  OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
	 2 PDB$SEED			  MOUNTED
	 3 PDB1 			  READ WRITE NO
SQL> conn scott@pdb1
Enter password: 
Connected.

SQL> create table test (id number);
Table created.

SQL> insert into test values (1);
1 row created.

SQL> insert into test values (2);
1 row created.

SQL> commit;
Commit complete.

SQL> drop table test;
Table dropped.

SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME		     OBJECT TYPE DROP TIME
------------- ------------------------------ ----------- ----------------
TEST	      BIN$Fh1VKrueZFngYw8CAAq+WA==$0 TABLE	 2024-04-15:11:30:03

Day 2: Getting Started with psql

Day 2: Getting Started with psql

Introduction to psql Command-Line Tool

psql is the PostgreSQL interactive terminal, providing a command-line interface to interact with PostgreSQL databases. It allows users to execute SQL commands, manage databases, and perform administrative tasks efficiently.

Connecting to a PostgreSQL Database

To connect to a PostgreSQL database using psql, follow these steps:

  1. Open a terminal window or command prompt.
  2. Use the following command to connect to a PostgreSQL database:
psql -U username -d dbname -h hostname -p port

Replace the placeholders with actual values:

  • username: PostgreSQL username
  • dbname: Name of the database you want to connect to
  • hostname: Hostname or IP address of the PostgreSQL server
  • port: Port number on which PostgreSQL is listening (default is 5432)

If the PostgreSQL server is running locally and you’re connecting with the default settings, you can simply use:

psql

This command connects to the default database with the default username (typically your system username).

Basic psql Commands for Database Interaction

Once connected to the PostgreSQL database, you can use various psql commands to perform database operations:

  • \l: List all databases.
  • \c dbname: Connect to a different database.
  • \dt: List all tables in the current database.
  • \d table_name: Describe a specific table, showing its structure (columns, data types, constraints).
  • \q: Quit the psql session and disconnect from the database.

Exploring the PostgreSQL Command-Line Interface

After connecting to the database, you’ll see a prompt similar to this:

dbname=#

This indicates that you’re connected to the specified database, and you can start executing SQL commands.

Example: Basic Database Operations

Let’s go through a simple example to demonstrate basic psql commands:

  1. Connect to a PostgreSQL database:
psql -U myuser -d mydatabase

List all tables in the current database:

\dt

Describe a specific table (e.g., “users”):

\d users

Execute a SQL query to retrieve data from the “users” table:

SELECT * FROM users;

Quit the psql session:

\q

By following these steps, you can explore and interact with PostgreSQL databases using the psql command-line tool.

Tomorrow, we’ll delve deeper into creating databases and tables in PostgreSQL. Stay tuned for more!

Certainly! Let’s dive deeper into each step with more detailed explanations and examples.

Connecting to a PostgreSQL Database:

To connect to a PostgreSQL database using psql, you’ll typically use the following command:

psql -U username -d dbname -h hostname -p port
  • -U username: Specifies the username to connect to the PostgreSQL database. You should replace username with your actual PostgreSQL username.
  • -d dbname: Specifies the name of the database you want to connect to. Replace dbname with the name of your target database.
  • -h hostname: (Optional) Specifies the hostname or IP address of the PostgreSQL server. If your database is running locally, you can omit this option.
  • -p port: (Optional) Specifies the port number on which PostgreSQL is listening. The default port for PostgreSQL is 5432. If your PostgreSQL server is running on a different port, specify it here.

Example:

psql -U myuser -d mydatabase -h localhost -p 5432

This command connects to the PostgreSQL database named mydatabase using the username myuser, on the local machine (localhost) and default port 5432.

If you’re connecting to a PostgreSQL server running on the default port (5432) and your username matches your system username, you can simply run psql without any arguments:

psql

This will connect you to the default PostgreSQL database with your system username.

Basic psql Commands for Database Interaction:

Once connected to the PostgreSQL database, you can use various psql commands to perform database operations:

  • \l: Lists all databases.
  • \c dbname: Connects to a different database.
  • \dt: Lists all tables in the current database.
  • \d table_name: Describes a specific table, showing its structure (columns, data types, constraints).
  • \q: Quits the psql session and disconnects from the database.

These commands are prefixed with a backslash (\) when used in psql to distinguish them from SQL commands.

Example: Basic Database Operations

Let’s walk through a simple example to demonstrate these basic psql commands:

  1. Connect to a PostgreSQL database:
psql -U myuser -d mydatabase

2. List all tables in the current database:

\dt

This command displays a list of all tables in the mydatabase database.

  1. Describe a specific table (e.g., “users”):
\d users

This command shows the structure of the users table, including its columns, data types, and constraints.

  1. Execute a SQL query to retrieve data from the “users” table:
SELECT * FROM users;

This SQL query retrieves all rows and columns from the users table and displays the results.

  1. Quit the psql session:
\q

This command exits the psql session and returns you to the command prompt.

By following these steps and commands, you can explore and interact with PostgreSQL databases effectively using the psql command-line tool.