Files
sql/banque.tables.md
2025-11-15 14:50:03 +01:00

2.9 KiB

Implémentation du modèle

1. Les titulaires (holder)

create type holder_type
  as enum ('BANK', 'PERSON', 'COMPANY');

Création de la table holder. C'est une table commune à tous les titulaires qu'ils soient des individus ou des sociétés. Elle sert à donner un identifiant unique et défini le type de titulaire.

create table holder (
  "id" bigint primary key generated always as identity,
  "type" holder_type not null,
  "created_at" timestamp not null default current_timestamp
);
  • bigint : entier sur 64 bits
  • primary key : clé primaire, identification unique de l'enregristrement
  • generated always as identity : incrémentation automatique (1, 2, 3, 4, ...)
  • not null : Il est obligatoire de spécifier une valeur.
  • Le type est forcé aux 3 valeurs de l'énumération holder_type
  • current_timestamp date et heure courante. Version moderne de now()

1.1 Les individus (person)

Création de la table person.

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 check (birthdate <= current_date - interval '15 years')
);

1.2 les sociétés (company)

create table company (
  "id" bigint primary key references holder(id) on delete cascade,
  "name" text not null,
  "registration_number" text unique not null,
  "creation_date" date not null
);

1.3 La banque (bank)

create table bank (
  "id" bigint primary key references holder(id),
  "name" text
);

2. Les monnaies

create table currency (
  "code" text primary key
);
create table exchange_rate (
  "currency_code" text references currency(code) on delete cascade,
  "date" date ,
  "rate" decimal not null,
  primary key (currency_code, date)
);

3. Les comptes

create table account (
  "id" bigint primary key generated always as identity,
  "opened_at" date not null default current_date,
  "balance" numeric(18,6) not null default 0,
  "currency_code" text not null references currency (code)
);
create table account_holder (
  "account_id" int not null references account(id) on delete cascade,
  "holder_id" int not null references holder(id) on delete cascade,
  "share" decimal(4,3) not null default 1 check (share > 0 and share <= 1),
  primary key (account_id, holder_id)
);

4. Les transactions

create table transaction (
  "id" bigint primary key generated always as identity,
  "transaction_date" timestamp not null default current_timestamp,
  amount decimal not null check (amount > 0)
);
create table operation (
  "id" bigint primary key generated always as identity,
  "transaction_id" bigint not null references transaction(id),
  "account_id" bigint not null references account(id),
  amount decimal not null check (amount > 0),
  direction text not null check (direction in ('DEBIT', 'CREDIT'))
);