postgresqlHow do I create a temporary table in PostgreSQL?
Creating a temporary table in PostgreSQL is a simple process. Here is an example of how to do it:
-- Create a temporary table
CREATE TEMP TABLE items (
id INTEGER,
name VARCHAR(50)
);
-- Insert some data
INSERT INTO items (id, name)
VALUES (1, 'Apple'), (2, 'Banana'), (3, 'Orange');
-- Select the data
SELECT * FROM items;
-- Output
id | name
----+--------
1 | Apple
2 | Banana
3 | Orange
The code above creates a temporary table called items with two columns, id and name. Then it inserts a few rows of data into the table. Finally, it selects the data from the table and prints the output.
The key parts of the code are:
CREATE TEMP TABLE- creates a temporary tableINSERT INTO- inserts data into the tableSELECT * FROM- selects data from the table
For more information, see the PostgreSQL documentation:
More of Postgresql
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How can I extract the year from a PostgreSQL timestamp?
- How do I use PostgreSQL with Qt?
- How can I extract the year from a date in PostgreSQL?
- How do I use the PostgreSQL VARCHAR data type?
- How do I use the PostgreSQL row_number function?
- How do I use the PostgreSQL quote_ident function?
- How can I use PostgreSQL types to create a database?
- How do I rename a column in PostgreSQL?
- How do I use PostgreSQL's ON CONFLICT DO NOTHING clause?
See more codes...