|
@@ -296,12 +296,86 @@ create table book_json(
|
|
|
)
|
|
|
);
|
|
|
|
|
|
+-- -----------------------------------------------------------------
|
|
|
+-- 11.33 Изменение структуры таблиц
|
|
|
+
|
|
|
+-- Добавить ограничение на таблицу
|
|
|
+alter table book_json add check (
|
|
|
+ data->>'name' is not null and
|
|
|
+ data->>'pages' is not null and
|
|
|
+ (data->>'pages')::int > 0
|
|
|
+);
|
|
|
+
|
|
|
+-- Добавить колонку к таблице
|
|
|
+alter table book_json
|
|
|
+add column created_at timestamp not null default current_timestamp;
|
|
|
+
|
|
|
+alter table book_json
|
|
|
+add column created_at timestamp not null default now();
|
|
|
+
|
|
|
+-- -----------------------------------------------------------------
|
|
|
+-- 11.38 INNER JOIN
|
|
|
+
|
|
|
+table species;
|
|
|
+table genus;
|
|
|
+
|
|
|
+select species_name as вид, genus_name as род from species
|
|
|
+join genus using(genus_id)
|
|
|
+order by species_name;
|
|
|
+
|
|
|
+-- Другие условия
|
|
|
+select species_name вид, genus_name род
|
|
|
+from species s join genus g on s.genus = g.genus_id
|
|
|
+order by species_name;
|
|
|
|
|
|
+--
|
|
|
+drop table if exists client, payment cascade;
|
|
|
+
|
|
|
+create temp table client (
|
|
|
+ client_id bigint generated always as identity primary key,
|
|
|
+ email varchar(360) not null
|
|
|
+);
|
|
|
|
|
|
+create temp table payment (
|
|
|
+ payment_id bigint generated always as identity primary key,
|
|
|
+ payment_time timestamp not null default current_timestamp,
|
|
|
+ amount int not null check (amount > 0),
|
|
|
+ client_id bigint not null references client(client_id)
|
|
|
+);
|
|
|
+
|
|
|
+insert into client (email) values
|
|
|
+ ('client1@mail.ru'),
|
|
|
+ ('client2@mail.ru'),
|
|
|
+ ('client3@mail.ru');
|
|
|
|
|
|
+insert into payment (payment_time, amount, client_id) values
|
|
|
+ ((now() - '1 month'::interval), 10, 1),
|
|
|
+ ((now() - '2 month'::interval), 11, 2),
|
|
|
+ ((now() - '3 month'::interval), 12, 3);
|
|
|
|
|
|
|
|
|
+table client;
|
|
|
+table payment;
|
|
|
|
|
|
+select email from client
|
|
|
+join payment using(client_id)
|
|
|
+where payment_time::date + interval '1 month' = current_date
|
|
|
+order by email
|
|
|
|
|
|
+-- -----------------------------------------------------------------
|
|
|
+-- 11.39 LEFT И RIGHT OUTER JOIN
|
|
|
+
|
|
|
+table species;
|
|
|
+table genus;
|
|
|
+
|
|
|
+select species_name вид, genus_name род
|
|
|
+from species s left join genus g using(genus_id)
|
|
|
+order by species_name;
|
|
|
+
|
|
|
+-- -----------------------------------------------------------------
|
|
|
+-- 11.40 FULL OUTER JOIN
|
|
|
|
|
|
+select species_name вид, genus_name род
|
|
|
+from species s full join genus g using(genus_id)
|
|
|
+order by species_name;
|
|
|
|