COPY & TEMP Tables πΒΆ
Prerequisites: CREATE TABLE, SELECT Statement
Mentor's Note: Sometimes you need a scratchpad. You don't want to mess up the main database, or you need to do some heavy calculations that require intermediate steps. That's where Temporary Tables shine. π‘
π The Scenario: The Worksheet πΒΆ
- Main Table: The Final Exam Paper (Permanent).
- Temp Table: The Rough Sheet (Exists only while you are working).
- Copy Table: The Photocopy (A backup before you start grading).
π» 1. Creating a Temporary Table β³ΒΆ
Temp tables are visible only to the current session and are deleted automatically when you disconnect.
π» 2. Copying a Table (Cloning) πΈΒΆ
There isn't a standard COPY command, but we use CREATE TABLE AS SELECT.
A. Copy Structure AND DataΒΆ
B. Copy Structure ONLY (No Data)ΒΆ
We use a condition that is always false!
π‘ Use CasesΒΆ
- Complex Reporting: Load raw data into a Temp Table -> Clean it -> Join it -> Export result.
- Testing: Copy the Production table to a Test table -> Run your risky
UPDATEquery -> Verify -> Apply to Production.