In PostgreSQL, you can create a temporary table using the CREATE TEMPORARY TABLE statement. Temporary tables are session-specific and are automatically dropped at the end of the session.
Here's how you can create a temporary table in PostgreSQL:
CREATE TEMPORARY TABLE TempTable (
ID SERIAL PRIMARY KEY,
Name VARCHAR(50)
);
This statement creates a temporary table named TempTable with columns ID and Name. The SERIAL type is used for the ID column to automatically generate unique values for each row.
You can then use this temporary table in your SQL queries within the same session:
INSERT INTO TempTable (Name) VALUES ('John'), ('Alice'), ('Bob');
SELECT * FROM TempTable;
To drop the temporary table explicitly when you're done using it (although this is not necessary as it will be dropped automatically when the session ends):
DROP TABLE TempTable;
That's it! You have created and used a temporary table in PostgreSQL.
No comments:
Post a Comment