-- Lecture 7: SQL Constraints and Triggers
-- Example: Bank accounts and transfers

-- This started as a skeleton, the definitions were filled in during the lecture

-- Tables

-- Accounts(_holder,balance)
CREATE TABLE Accounts(
  holder TEXT PRIMARY KEY,
  balance INT CHECK(balance>=0)
);

-- Transfers(sender,recipient,amount)
CREATE TABLE Transfers(
  sender TEXT REFERENCES Accounts(holder) ON UPDATE CASCADE,
  receiver TEXT REFERENCES Accounts(holder) ON UPDATE CASCADE,
  amount INT CHECK (amount>0),
  CHECK (receiver<>sender)
);
