wiki:WikiStart/DB

Релациона шема

  • account (id, username, e-mail, password, balance)
  • transaction (id, amount, type, timestamp, account_id*)
  • transfer (id, s_id*, r_id*)
  • withdraw (id, a_id*, amount, timestamp)
  • deposit (id, a_id*, amount, timestamp)
  • transferred_money (id, s_id*, r_id*, t_id*, currency)

DDL код

create table account(
    id serial primary key,
    username varchar(255) not null,
    email varchar(255) not null,
    password varchar(255) not null,
    balance decimal(15, 2) not null 
);

create table "transaction"(
    id serial primary key,
    amount decimal(15, 2) not null,
    type varchar(50) not null,
    timestamp timestamp default current_timestamp,
    account_id int not null,
    foreign key (account_id) references account(id)
);

create table transfer(
    id serial primary key,
    s_id int not null,
    r_id int not null, 
    foreign key(s_id) references account(id),
    foreign key(r_id) references
    account(id)

);

create table deposit(
    id serial primary key,
    account_id int not null,
    amount decimal(15,2) not null,
    timestamp timestamp default current_timestamp,
    foreign key(account_id) references account(id)
);

create table withdraw(
    id serial primary key,
    account_id int not null,
    amount decimal(15,2) not null,
    timestamp timestamp default current_timestamp,
    foreign key(account_id) references account(id)
);

create table transferred_money(
    id serial primary key, 
    s_account_id int not null, 
    r_account_id int not null, 
    t_id int not null, 
    currency varchar(50) not null, 
    foreign key (s_account_id) references account(id),
    foreign key (r_account_id) references account(id),
    foreign key (t_id) references transaction(id)
);


Triggers

create or replace function after_transaction_trigger()
returns trigger as $$
begin

	if new.type = 'Deposit' then
		insert into Deposit(account_id,amount, timestamp)
		values (new.account_id, new.amount, new.timestamp);
	
	elsif new.type = 'Withdrawal' then
		insert into Withdraw(account_id,amount, timestamp)
		values (new.account_id, new.amount, new.timestamp);
	
	elsif new.type like '(MKD) Transfer Out to%' then
		insert into transferred_money(s_account_id, r_account_id, t_id, currency)
		select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1),
			new.id, 'MKD'; 

	elsif new.type like '(USD) Transfer Out to%' then
		insert into transferred_money(s_account_id, r_account_id, t_id, currency)
		select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1),
			new.id, 'USD';
	
	elsif new.type like '(EUR) Transfer Out to%' then
		insert into transferred_money(s_account_id, r_account_id, t_id, currency)
		select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1),
			new.id, 'EUR';
			
	elsif new.type like '(GBP) Transfer Out to%' then
		insert into transferred_money(s_account_id, r_account_id, t_id, currency)
		select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1),
			new.id, 'GBP';

	end if;
	return new;
end;
$$ language plpgsql;

create trigger after_transaction
after insert on transaction
for each row
execute function after_transaction_trigger()

CREATE OR REPLACE FUNCTION after_transaction_trigger2()
RETURNS TRIGGER AS $$
BEGIN
    IF NEW.type = 'Deposit' THEN
     
        INSERT INTO transfer(s_id, r_id)
        VALUES (NEW.account_id, NEW.account_id);

    ELSIF NEW.type = 'Withdrawal' THEN
      
        INSERT INTO transfer(s_id, r_id)
        VALUES (NEW.account_id, NEW.account_id);

    ELSIF NEW.type LIKE '(MKD) Transfer Out to%' OR 
          NEW.type LIKE '(USD) Transfer Out to%' OR 
          NEW.type LIKE '(EUR) Transfer Out to%' OR 
          NEW.type LIKE '(GBP) Transfer Out to%' THEN
          
    
        INSERT INTO transfer(s_id, r_id)
        SELECT NEW.account_id, 
               (SELECT id FROM account 
                WHERE username = TRIM(SUBSTRING(NEW.type FROM POSITION('Transfer Out to ' IN NEW.type) + 15)) 
                LIMIT 1);

    END IF;
    
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER after_transaction2
AFTER INSERT ON transaction
FOR EACH ROW
EXECUTE FUNCTION after_transaction_trigger2();


Last modified 11 days ago Last modified on 03/23/25 20:44:44
Note: See TracWiki for help on using the wiki.