Why are global temporary tables truncated when the connection is closed

oracle

I'm a relatively new Oracle database developer coming from SQL Server.

I noticed that global temporary tables (created with ON COMMIT PRESERVE ROWS) lose their contents when the session times out, but the tables themselves still exist and have to be manually dropped before rerunning whatever script I have that creates them.

In SQL Server, a temporary table (which isn't global in any sense) is dropped and completely gone when the session is closed.

What is the logical or technical reason for Oracle keeping temporary tables around – but truncating them – when the session is closed?

(If there is a database or server option to avoid this behavior, it is irrelevant to my use-case since I'm only a developer and will be creating scripts that are run on an outside database.)

Best Answer

Column names must be known at compile time.

You can't create a table in a pl/sql block and then use it in the same pl/sql block. This even applies to oracle-18c private temporary tables.

As such, you create a Global Temporary Table (GTT) once and re-use it throughout the applications life.

Also, the data in GTTs are private to the session. Let's say there exist a GTT called TEMP_ID_LIST. Your session won't see my data in TEMP_ID_LIST. My session won't see your data in TEMP_ID_LIST.

Oracle-Base URLs for reference