Sobes.tech
Senior

What will the relationship between the transfers and accounts tables look like (two FKs on one table)?

sobes.tech AI

Answer from AI

The transfers table will have two foreign keys (FK), both referencing the accounts table: one for the sender's account (from_account_id), and the other for the recipient's account (to_account_id). This allows to unambiguously specify from which account and to which account the transfer was made.

Example SQL schema:

CREATE TABLE accounts (
    id SERIAL PRIMARY KEY,
    account_number VARCHAR(20) UNIQUE NOT NULL,
    balance NUMERIC NOT NULL
);

CREATE TABLE transfers (
    id SERIAL PRIMARY KEY,
    from_account_id INT NOT NULL,
    to_account_id INT NOT NULL,
    amount NUMERIC NOT NULL,
    transfer_date TIMESTAMP NOT NULL,
    CONSTRAINT fk_from_account FOREIGN KEY (from_account_id) REFERENCES accounts(id),
    CONSTRAINT fk_to_account FOREIGN KEY (to_account_id) REFERENCES accounts(id)
);

Thus, each record in transfers links two different accounts from the accounts table.

What will the relationship between the transfers and… - sobes.tech