From 4d32f73a4dc5d0316bfa6356c35becfac7e7b78f Mon Sep 17 00:00:00 2001 From: medina5 Date: Tue, 4 Nov 2025 23:29:06 +0100 Subject: [PATCH] deposit --- banque.correction.md | 3 --- banque.sql | 38 ++++++++++++++++++++++++-------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/banque.correction.md b/banque.correction.md index 91e6b4c..8091f23 100644 --- a/banque.correction.md +++ b/banque.correction.md @@ -2,15 +2,12 @@ ## 1. Les titulaires - `person` : informations propres aux personnes physiques - prénom, nom, date de naissance `company` : informations propres aux entreprises - raison sociale, numéro d’immatriculation, date de création - - ### 1.6 Contôle de l'âge Contrainte déclarative diff --git a/banque.sql b/banque.sql index ebf2f77..1ac2ff4 100644 --- a/banque.sql +++ b/banque.sql @@ -1,5 +1,6 @@ -drop schema bank cascade; +drop schema if exists bank cascade; create schema bank; +set search_path TO bank; /* * Holder : table commune à tous les titulaires @@ -71,12 +72,12 @@ insert into holder(type) values ('PERSON') returning id; insert into person(id, firstname, lastname, birthdate) values (3, 'Lucas', 'Zanetti', '2002-05-09'); -select * from person; +select * from holders_list; -delete from holder where id = 3; +delete from holder where id = 3; -- La suppresion du titulaire entraine la suppression de l'individu -select * from person; +select * from holders_list; -- Contrainte sur l'âge du titulaire alter table person @@ -118,7 +119,7 @@ create table account_holder ( * Compte individuel pour Françoise Zanetti. */ -insert into account (number) +insert into account (number) values ('AA') returning id; insert into account_holder @@ -137,7 +138,7 @@ insert into person(id, firstname, lastname, birthdate) * Compte joint pour Françoise et Justin. */ -insert into account (number) +insert into account (number) values ('AB') returning id; insert into account_holder @@ -147,7 +148,7 @@ insert into account_holder values (2, 5, 0.5); create view accounts_list as -select +select a.number, h.type, coalesce(p.firstname || ' ' || p.lastname, c.name) as holder_name, @@ -170,7 +171,7 @@ begin; insert into holder(type) values ('PERSON') returning id; insert into person(id, firstname, lastname, birthdate) - values (6, 'Justin', 'Hébrard', '1993-03-11'); + values (6, 'Mattéo', 'Zanetti', '2012-12-12'); commit; @@ -260,7 +261,7 @@ begin values (v_account_id, p_holders[i], p_shares[i]); end loop; - raise notice 'Compte créé avec succès (ID=%) avec % titulaires.', + raise notice 'Compte créé avec succès (ID=%) avec % titulaires.', v_account_id, array_length(p_holders, 1); end; $$; @@ -303,16 +304,16 @@ begin insert into operation(account_id, kind, amount, description) values (p_account_id, p_kind, p_amount, p_description); - raise notice 'Opération enregistrée : % de %.2f sur compte %', + raise notice 'Opération enregistrée : % de %.2f sur compte %', p_kind, p_amount, p_account_id; end; $$; CREATE TABLE transaction ( - id SERIAL PRIMARY KEY, - account_id INT NOT NULL REFERENCES account(id), - amount NUMERIC(12, 2) NOT NULL CHECK (amount <> 0), - operation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP + id bigint primary key generated always as identity, + account_id INT NOT NULL REFERENCES account(id), + amount NUMERIC(18, 6) NOT NULL CHECK (amount <> 0), + operation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); CREATE OR REPLACE PROCEDURE deposit(p_account_id INT, p_amount NUMERIC) @@ -368,4 +369,13 @@ BEGIN END; $$; +CALL deposit(1, 500); +CALL withdraw(1, 200); +CALL withdraw(1, 1000); -- Doit échouer +create table entry ( + id bigint primary key generated always as identity, + transaction_id bigint, + amount NUMERIC(18, 6) NOT NULL CHECK (amount <> 0), + sens +)