2025-11-18 20:40:28 +01:00
|
|
|
|
# Les vues
|
|
|
|
|
|
|
|
|
|
|
|
Une vue SQL est un objet de la base de données qui représente le résultat d’une requête SQL enregistrée.
|
|
|
|
|
|
Elle se comporte comme une table virtuelle : on peut la consulter avec SELECT comme une table, mais elle ne stocke pas de données.
|
|
|
|
|
|
|
|
|
|
|
|
La définition de la vue est enregistrée dans la base, et chaque fois qu’on interroge la vue, la base réexécute la requête sous-jacente.
|
|
|
|
|
|
|
2025-11-16 09:10:10 +01:00
|
|
|
|
```sql
|
|
|
|
|
|
create or replace view holder_detail as
|
|
|
|
|
|
select h.id, h.type,
|
|
|
|
|
|
case
|
2025-11-18 20:40:28 +01:00
|
|
|
|
when type = 'PERSON' then firstname || ' ' || lastname
|
|
|
|
|
|
when type = 'COMPANY' then c.name
|
|
|
|
|
|
when type = 'BANK' then b.name
|
2025-11-16 09:10:10 +01:00
|
|
|
|
end as nom,
|
|
|
|
|
|
case
|
2025-11-18 20:40:28 +01:00
|
|
|
|
when type = 'PERSON' then age(birthdate)
|
|
|
|
|
|
when type = 'COMPANY' then age(c.created_at)
|
2025-11-16 09:10:10 +01:00
|
|
|
|
end as age
|
|
|
|
|
|
from holder h
|
|
|
|
|
|
left join person p on p.id = h.id
|
|
|
|
|
|
left join company c on c.id = h.id;
|
2025-11-18 20:40:28 +01:00
|
|
|
|
left join bank b on b.id = h.id;
|
2025-11-16 09:10:10 +01:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
|
create or replace view account_detail as
|
|
|
|
|
|
select a.balance,
|
|
|
|
|
|
a.balance * ah.share as balance_currency,
|
|
|
|
|
|
a.balance * ah.share / latest_exchange_rate(a.currency_code, current_date),
|
|
|
|
|
|
a.currency_code,
|
|
|
|
|
|
hd.nom
|
|
|
|
|
|
from account a
|
|
|
|
|
|
join account_holder ah on ah.account_id = a.id
|
|
|
|
|
|
join holder_detail hd on ah.holder_id = hd.id;
|
|
|
|
|
|
```
|