Create a new table from Union two tables with union in postgres - postgresql

I would like to create a new table as the result of the union of two tables without duplicates. I searched in stackoverflow and I found a question with exactly what I want but using mysql Create a new table from merging two tables with union.
Solution in mysql
CREATE TABLE new_table
SELECT * FROM table1
UNION
SELECT * FROM table2;
I tried to do something similar but I got:
SQL error.
I would like to achieve this if is possible with an statement similar to mysql.
I know that if you create a new table first with the fields that I want. I can do a select into this table over the union of this tables. If there aren't other option well I have to do something like this.
But in summary If possible to do something similar to the question with mysql in postgres. I would like to use syntactic sugar to do that
Thanks in advance
Update
In order to clarify I have two table with equal structure
TABLE1(id,field1,field2,field3)
TABLE2(id,field1,field2,field3)
and The table that I want
TABLE3(id,field1,field2,field3)
Notice that I tried
CREATE TABLE new_table as
SELECT * FROM table1
UNION
SELECT * FROM table2;
and it works but didn't put the fields in the correct place for example put field3 of table 1 in field 1 of table_result

You are missing the AS keyword:
CREATE TABLE new_table
AS
SELECT * FROM table1
UNION
SELECT * FROM table2;
If you need the columns in a specific order, then specify them in the select:
CREATE TABLE new_table
AS
SELECT id, column1, column2, column3
FROM table1
UNION
SELECT id, column1, column2, column3
FROM table2;
More details in the manual:
https://www.postgresql.org/docs/current/static/sql-createtableas.html

Related

Liquibase insert select multiple rows postgres

I want to insert into table1 multiple rows from table2. The problem is that I have some fields in table1 that I want to compute, and some rows that I want to select from table2. For example something like this:
insert into table1 (id, selectField1, selectField2, constant)
values ((gen_random_uuid()), (select superField1 from table2), (select superField2 from table2), 'test');
So the logic is to select superField1 and superField2 from all the rows in the table2 and insert them into table1 with constant value test and generated uids. superField1 and superField2 should be from the same row in table2 when inserting in table1. How can I achieve something like this using liquibase?
P.S: I'm using <sql> tag since it's easier to implement using SQL than using XML changeset, but if you know how to do it in XML that would be appreciated too, but just in SQL will be enough too. DBMS is postgres.
Don't use the VALUES clause if the source is a SELECT statement:
insert into table1 (id, selectField1, selectField2, constant)
select gen_random_uuid(), superField1, superField2, 'test'
from table2;

How to use the same common table expression in two consecutive psql statements?

I'm trying to perform a pretty basic operation with a few steps:
SELECT data from table1
Use id column from my selected table to remove data from table2
Insert the selected table from step 1 into table2
I would imagine that this would work
begin;
with temp as (
select id
from table1
)
delete from table2
where id in (select id from temp);
insert into table2 (id)
select id from temp;
commit;
But I'm getting an error saying that temp is not defined during my insert step?
Only other post I found about this is this one but it didn't really answer my question.
Thoughts?
From Postgres documentation:
WITH provides a way to write auxiliary statements for use in a larger
query. These statements, which are often referred to as Common Table
Expressions or CTEs, can be thought of as defining temporary tables
that exist just for one query.
If you need a temp table for more than one query you can do instead:
begin;
create temp table temp_table as (
select id
from table1
);
delete from table2
where id in (select id from temp_table);
insert into table2 (id)
select id from temp_table;
commit;

Is there a way to treat two Postgres tables as one?

I want to select all rows from two tables that have the same number of columns where the columns have the same name and type.
e.g. table 2 is basically a continuation of table 1 and so I want any queries to look through both tables to find the best match.
How would I do that since SELECT would normally just add the second table's columns to the first table.
Yes, there is. For SELECT only you could use compound query:
SELECT *
FROM tab1
UNION ALL
SELEECT *
FROM tab2;
or create view:
CREATE VIEW my_view
AS
SELECT *
FROM tab1
UNION ALL
SELECT *
FROM tab2;
SELECT * FROM my_view;

how to copy a table without importing data and constraints in Oracle?

I want to create a new table called table2 from another table called table1 without importing data and constraints. I used this query:
create table2 as select * from table1 where 1=2;
this code created table2 without any data, but imports constraints from table1. Is there a way not to import constraints from table1?
The answer can be found in the question create table with select union has no constraints.
If the select is a union, Oracle will not add any constraints, so simply use the same select twice, and be sure not to include any records in the second select:
create table2 as
select * from table1 where 1=2
union all
select * from table1 where 1=2;

select data from 8 tabels into one temp table

I need to insert data from several tables with all the same field names into one temp table, i know i can use cursor/loop to do this, i wanted to know is there a quicker way of doing this.
select from table 1, table 2, table 3, into #temptable.
select * into #temptable from table1
insert into #temptable select * from table2
insert into #temptable select * from table3
The first query creates the temp table on insert, the rest just keep adding data.