-- Update single row
update table_name
set first_name = 'Peter',
last_name = 'Müller'
where id = 1;
-- Update multiple rows
update table_name t
set first_name = u.first_name
from (values (1, 'Peter'),
(2, 'Anna')
) u(id, first_name)
where u.id = t.id;
-- Update multiple rows
update table_name t
set my_value = true
from (select unnest(array [1, 2, 3])) as u(id)
where t.id = u.id;
select * from table_name;
-- subquery
select * from (select id, name from table_name) sub;
-- case
select case when table1.name is null then table2.name else table1.col1 end as name
from table1
full outer join table2 on table1.id = table2.id;
-- joins
inner join
left outer join, left join
right outer join, right join
full outer join
cross join
natural join
-- alter table
alter table table_name
add column last_name varchar not null;
alter table table_name
drop column first_name;
alter table table_name
alter column first_name drop not null;
alter table table_name
alter column first_name set not null;
-- alter type
alter type my_enum add value if not exists 'MyVariant';
alter table table_name add constraint table_name_column_name_unique unique (column_name);
alter table table_name drop constraint table_name_column_name_unique;
-- Show constraints
select * from pg_catalog.pg_constraint;
select * from information_schema.columns where table_name = 'column_name';
-- Show views
select table_name
from information_schema.views
where table_schema = any (current_schemas(false));
create view my_view as
my_query
create or replace view my_view
as
my_query
alter view my_view rename to my_new_view;
drop view if exists my_view;