From 3c78b69a236e7d9492226ee7dfd7a4be231de75f Mon Sep 17 00:00:00 2001 From: medina5 Date: Fri, 7 Nov 2025 21:29:15 +0100 Subject: [PATCH] banque --- banque.final.sql | 26 +++++++++----------- banque.md | 6 ++--- banque.proc.sql | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 17 deletions(-) create mode 100644 banque.proc.sql diff --git a/banque.final.sql b/banque.final.sql index d7b1300..6019da9 100644 --- a/banque.final.sql +++ b/banque.final.sql @@ -9,7 +9,7 @@ create type holder_type as enum ('BANK', 'PERSON', 'COMPANY'); create table holder ( "id" bigint primary key generated always as identity, -- identifiant unique - "type" holder_type not null, + "type" holder_type not null, "created_at" timestamp not null default current_timestamp -- date et heure de création ); @@ -28,7 +28,7 @@ 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 + "birthdate" date not null check (birthdate <= current_date - interval '15 years') ); /* @@ -47,8 +47,8 @@ create table currency ( create table exchange_rate ( "currency_code" text references currency(code) on delete cascade, - "date" date, - "rate" decimal, + "date" date , + "rate" decimal not null, primary key (currency_code, date) ); /* @@ -59,7 +59,7 @@ 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 check (balance >= 0), - "currency_code" text references currency (code) + "currency_code" text not null references currency (code) ); /* @@ -71,23 +71,21 @@ create table account ( 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) default 1 check (share > 0 and share <= 1), + "share" decimal(4,3) not null default 1 check (share > 0 and share <= 1), primary key (account_id, holder_id) ); - - create table transaction ( "id" bigint primary key generated always as identity, - "transaction_date" timestamp default current_timestamp, - amount decimal check (amount > 0) + "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 references transaction(id), - "account_id" bigint references account(id), - amount decimal check (amount > 0), - direction text check (direction in ('DEBIT', 'CREDIT')) + "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')) ); diff --git a/banque.md b/banque.md index ef30837..0a9e973 100644 --- a/banque.md +++ b/banque.md @@ -357,8 +357,8 @@ erDiagram } account_holder { - bigint account_id FK - bigint holder_id FK + bigint account_id PK,FK + bigint holder_id PK,FK decimal share } @@ -368,7 +368,7 @@ erDiagram exchange_rate { date date PK - text currency_code PK + text currency_code PK,FK decimal rate } diff --git a/banque.proc.sql b/banque.proc.sql new file mode 100644 index 0000000..60a9b27 --- /dev/null +++ b/banque.proc.sql @@ -0,0 +1,63 @@ +create or replace procedure add_person ( + p_firstname text, + p_lastname text, + p_birthdate date +) +language plpgsql +as $$ +declare + v_holder_id bigint; +begin + + insert into holder(type) values ('PERSON') + returning id into v_holder_id; + + insert into person(id, firstname, lastname, birthdate) + values (v_holder_id, p_firstname, p_lastname, p_birthdate); + + raise notice 'Titulaire créé : % = % %', + v_holder_id, p_firstname, p_lastname; +end; +$$; + +create or replace procedure add_bank ( + name text +) +language plpgsql +as $$ +declare + v_holder_id bigint; +begin + + insert into holder(type) values ('BANK') + returning id into v_holder_id; + + insert into bank(id, name) + values (v_holder_id, p_name); + + raise notice 'Titulaire créé : % = %', + v_holder_id, p_name; +end; +$$; + +create or replace procedure add_company ( + p_name text, + p_registration_number text, + p_created_at date +) +language plpgsql +as $$ +declare + v_holder_id bigint; +begin + + insert into holder(type) values ('COMPANY') + returning id into v_holder_id; + + insert into company(id, name, registration_number, created_at) + values (v_holder_id, p_name, p_registration_number, p_created_at); + + raise notice 'Titulaire créé : % = % %', + v_holder_id, p_name, p_registration_number; +end; +$$;