-- Amended 01/27/1999 to fix bugs

-- the library's books; some info about them is in other tables
create table books (
  bookid      integer
    constraint bookid_primary primary key,
  title       varchar(255) not null, 
    --should really be table like authors
  series      varchar(255), 
    -- should really be table like authors
  position    integer,  -- where in series
  checkouts   integer default 0 not null -- number of times checked out
);
create sequence bookid_seq;


-- the author(s) of each book; must be a separate table since each
-- book could have multiple authors (so multiple rows here)
create table authors (
  bookid      integer
  constraint authors_bookid_exists
    foreign key(bookid) references books(bookid),  
  author      varchar(255),
  constraint authors_primary primary key (bookid, author)
);


-- library members / book borrowers; some are librarians
create table members (
  memberid    integer, -- encoded in membership card
  constraint members_primary primary key (memberid),
  name_prefix varchar(255),
  name_first  varchar(255),
  name_middle varchar(255),
  name_last   varchar(255),
  name_suffix varchar(255),
  email       varchar(255),
  credit      integer default 0 not null,  -- fine credit, in cents
  mem_start   date not null,
  mem_end     date not null,
  mem_lib     integer not null, -- librarian who sold the membership
  constraint mem_lib_exists
    foreign key(mem_lib) references members(memberid),
  librarian   integer -- librarian bits; null for non-librarians
);
create sequence memberid_seq;























-- information about all the individual copies of a book; again, this
-- is a separate table since most books will have multiple rows here.
-- we assume each copy gets its own barcode and key on that.
create table copies (
  barcode     integer,
  constraint copies_primary primary key (barcode),
  bookid      integer not null,
  constraint copies_bookid_exists 
    foreign key(bookid) references books(bookid),
  circ        integer default 0 not null,  -- does this book circulate?
  constraint circ_boolean check (circ in (0, 1)),
  out_to      integer,     -- who has this checked out?
  constraint out_to_exists 
    foreign key (out_to) references members(memberid),
  out_by      integer not null, -- what librarian checked it out?
  constraint out_by_exists
    foreign key (out_by) references members(memberid),
  due_by      date,
  obtained    date not null
);
create sequence barcode_seq;


-- library fines incurred 
create table fines (
  fineid      integer,
  constraint fines_primary primary key (fineid),
  memberid    integer not null,
  constraint fines_memberid_exists
    foreign key(memberid) references members(memberid),
  librarian   integer not null,
  constraint fines_librarian_exists
    foreign key(librarian) references members(memberid),
  amount      integer not null, -- in cents
  reason      varchar(255) not null,
  incurred    date not null,
  paid        date
);
create sequence fineid_seq;


