tp 1
This commit is contained in:
114
banque.correction.md
Normal file
114
banque.correction.md
Normal file
@@ -0,0 +1,114 @@
|
||||
# Corrections
|
||||
|
||||
## 1. Les titulaires
|
||||
|
||||
`holder` : table commune à tous les titulaires
|
||||
|
||||
- identifiant unique (`id`)
|
||||
- type de titulaire (`type` = 'PERSON' ou 'COMPANY')
|
||||
- date de création
|
||||
|
||||
```sql
|
||||
create table holder (
|
||||
id bigint primary key generated always as identity,
|
||||
type text not null check (type in ('PERSON', 'COMPANY')),
|
||||
created_at timestamp not null default now()
|
||||
);
|
||||
```
|
||||
|
||||
`person` : informations propres aux personnes physiques
|
||||
- prénom, nom, date de naissance
|
||||
|
||||
```sql
|
||||
create table person (
|
||||
id bigint primary key references holder(id) on delete cascade,
|
||||
firstname text not null,
|
||||
lastname text not null,
|
||||
birthdate date not null
|
||||
);
|
||||
```
|
||||
|
||||
`company` : informations propres aux entreprises
|
||||
- raison sociale, numéro d’immatriculation, date de création
|
||||
|
||||
```sql
|
||||
create table company (
|
||||
id bigint primary key references holder(id) on delete cascade,
|
||||
name text not null,
|
||||
registration_number text unique not null,
|
||||
created_at date not null
|
||||
);
|
||||
```
|
||||
|
||||
### Tests d'insertion des données
|
||||
|
||||
```sql
|
||||
-- Création d’un titulaire de type personne physique
|
||||
insert into holder(type) values ('PERSON') returning id;
|
||||
|
||||
-- Utilisons l'id qui est retourné (1)
|
||||
insert into person(id, firstname, lastname, birthdate)
|
||||
values (1, 'Françoise', 'Zanetti', '1995-04-12');
|
||||
```
|
||||
|
||||
```sql
|
||||
-- Création d’un titulaire de type entreprise
|
||||
insert into holder(type) values ('COMPANY') returning id;
|
||||
|
||||
-- Utilisons l'id qui est retourné (2)
|
||||
insert into company(id, name, registration_number, created_at)
|
||||
values (2, 'Boulangerie de Valorgue', 'FR19803269968', '2014-08-19');
|
||||
```
|
||||
|
||||
### Vérification
|
||||
|
||||
```sql
|
||||
select h.id, h.type, h.created_at,
|
||||
p.firstname || ' ' || p.lastname as person,
|
||||
c.name as company
|
||||
from holder h
|
||||
left join person p on p.id = h.id
|
||||
left join company c on c.id = h.id
|
||||
order by h.id;
|
||||
```
|
||||
|
||||
#### 1.5.1 Pourquoi séparer `person` et `company` ?
|
||||
|
||||
Parce que leurs attributs diffèrent (nom/prénom vs raison sociale).
|
||||
Cela évite les colonnes inutiles et permet des contraintes spécifiques à chaque type.
|
||||
|
||||
#### 1.5.2 Pourquoi ne pas tout mettre dans une seule table holder ?
|
||||
|
||||
Cela forcerait la présence de nombreuses colonnes nulles. La séparation améliore la cohérence et la lisibilité du schéma.
|
||||
|
||||
#### 1.5.3 Quelle contrainte empêche d’insérer une person sans holder ?
|
||||
|
||||
La clé étrangère references holder(id) dans person.
|
||||
|
||||
### 1.6 Contôle de l'âge
|
||||
|
||||
Contrainte déclarative
|
||||
|
||||
```sql
|
||||
alter table person
|
||||
add constraint chk_person_minimum_age
|
||||
check (birthdate <= current_date - interval '15 years');
|
||||
```
|
||||
|
||||
Contrainte procédurale avec un trigger :
|
||||
|
||||
```sql
|
||||
create or replace function check_person_age()
|
||||
returns trigger as $$
|
||||
begin
|
||||
if new.birthdate > current_date - interval '15 years' then
|
||||
raise exception 'Holder must be at least 15 years old';
|
||||
end if;
|
||||
return new;
|
||||
end;
|
||||
$$ language plpgsql;
|
||||
|
||||
create trigger trg_check_person_age
|
||||
before insert or update on person
|
||||
for each row execute procedure check_person_age();
|
||||
```
|
||||
Reference in New Issue
Block a user