deposit
This commit is contained in:
@@ -2,15 +2,12 @@
|
|||||||
|
|
||||||
## 1. Les titulaires
|
## 1. Les titulaires
|
||||||
|
|
||||||
|
|
||||||
`person` : informations propres aux personnes physiques
|
`person` : informations propres aux personnes physiques
|
||||||
- prénom, nom, date de naissance
|
- prénom, nom, date de naissance
|
||||||
|
|
||||||
`company` : informations propres aux entreprises
|
`company` : informations propres aux entreprises
|
||||||
- raison sociale, numéro d’immatriculation, date de création
|
- raison sociale, numéro d’immatriculation, date de création
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### 1.6 Contôle de l'âge
|
### 1.6 Contôle de l'âge
|
||||||
|
|
||||||
Contrainte déclarative
|
Contrainte déclarative
|
||||||
|
|||||||
38
banque.sql
38
banque.sql
@@ -1,5 +1,6 @@
|
|||||||
drop schema bank cascade;
|
drop schema if exists bank cascade;
|
||||||
create schema bank;
|
create schema bank;
|
||||||
|
set search_path TO bank;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Holder : table commune à tous les titulaires
|
* 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)
|
insert into person(id, firstname, lastname, birthdate)
|
||||||
values (3, 'Lucas', 'Zanetti', '2002-05-09');
|
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
|
-- La suppresion du titulaire entraine la suppression de l'individu
|
||||||
select * from person;
|
select * from holders_list;
|
||||||
|
|
||||||
-- Contrainte sur l'âge du titulaire
|
-- Contrainte sur l'âge du titulaire
|
||||||
alter table person
|
alter table person
|
||||||
@@ -118,7 +119,7 @@ create table account_holder (
|
|||||||
* Compte individuel pour Françoise Zanetti.
|
* Compte individuel pour Françoise Zanetti.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
insert into account (number)
|
insert into account (number)
|
||||||
values ('AA') returning id;
|
values ('AA') returning id;
|
||||||
|
|
||||||
insert into account_holder
|
insert into account_holder
|
||||||
@@ -137,7 +138,7 @@ insert into person(id, firstname, lastname, birthdate)
|
|||||||
* Compte joint pour Françoise et Justin.
|
* Compte joint pour Françoise et Justin.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
insert into account (number)
|
insert into account (number)
|
||||||
values ('AB') returning id;
|
values ('AB') returning id;
|
||||||
|
|
||||||
insert into account_holder
|
insert into account_holder
|
||||||
@@ -147,7 +148,7 @@ insert into account_holder
|
|||||||
values (2, 5, 0.5);
|
values (2, 5, 0.5);
|
||||||
|
|
||||||
create view accounts_list as
|
create view accounts_list as
|
||||||
select
|
select
|
||||||
a.number,
|
a.number,
|
||||||
h.type,
|
h.type,
|
||||||
coalesce(p.firstname || ' ' || p.lastname, c.name) as holder_name,
|
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 holder(type) values ('PERSON') returning id;
|
||||||
|
|
||||||
insert into person(id, firstname, lastname, birthdate)
|
insert into person(id, firstname, lastname, birthdate)
|
||||||
values (6, 'Justin', 'Hébrard', '1993-03-11');
|
values (6, 'Mattéo', 'Zanetti', '2012-12-12');
|
||||||
|
|
||||||
commit;
|
commit;
|
||||||
|
|
||||||
@@ -260,7 +261,7 @@ begin
|
|||||||
values (v_account_id, p_holders[i], p_shares[i]);
|
values (v_account_id, p_holders[i], p_shares[i]);
|
||||||
end loop;
|
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);
|
v_account_id, array_length(p_holders, 1);
|
||||||
end;
|
end;
|
||||||
$$;
|
$$;
|
||||||
@@ -303,16 +304,16 @@ begin
|
|||||||
insert into operation(account_id, kind, amount, description)
|
insert into operation(account_id, kind, amount, description)
|
||||||
values (p_account_id, p_kind, p_amount, p_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;
|
p_kind, p_amount, p_account_id;
|
||||||
end;
|
end;
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE TABLE transaction (
|
CREATE TABLE transaction (
|
||||||
id SERIAL PRIMARY KEY,
|
id bigint primary key generated always as identity,
|
||||||
account_id INT NOT NULL REFERENCES account(id),
|
account_id INT NOT NULL REFERENCES account(id),
|
||||||
amount NUMERIC(12, 2) NOT NULL CHECK (amount <> 0),
|
amount NUMERIC(18, 6) NOT NULL CHECK (amount <> 0),
|
||||||
operation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
operation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE OR REPLACE PROCEDURE deposit(p_account_id INT, p_amount NUMERIC)
|
CREATE OR REPLACE PROCEDURE deposit(p_account_id INT, p_amount NUMERIC)
|
||||||
@@ -368,4 +369,13 @@ BEGIN
|
|||||||
END;
|
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
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user