create sequence movie_showtimes_id_seq;
create table movies_showtimes (
  id integer not null
    default nextval('movie_showtimes_id_seq'),
  movie_id integer not null
    references movies(id),
  theatre_id integer not null
    references theatres(id),
  room varchar(64) not null,
  start_time timestamp with time zone not null,
  primary key (id),
  unique (movie_id, theatre_id, room, start_time),
  foreign key (theatre_id, room)
    references auditoriums(theatre_id, room) intially deferred
);

create sequence orders_id_seq;
create table orders (
  confirmation_code varchar(16) not null
    check(length(confirmation_code) > 0),
  movie_showtime_id integer not null
    references movie_showtimes(id),
  movie_id integer not null,
  room varchar(64) not null,
  start_time timestamp with time zone,
  purchaser_name varchar(128) not null
    check (length(purchaser_name) > 0),
  primary key (confirmation_code),
  foreign key (movie_id, theatre_id, room, start_time)
    references movie_showtimes(movie_id, theatre_id, room, start_time)
) inherits (addresses);
