Skip to content

Instantly share code, notes, and snippets.

@porsager
Last active June 20, 2019 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save porsager/5f40f62365930f4ce412fa6f17328752 to your computer and use it in GitHub Desktop.
Save porsager/5f40f62365930f4ce412fa6f17328752 to your computer and use it in GitHub Desktop.
create table countries (
country text not null unique,
country_code char(2) not null unique
);
create table country_areas (
country_area text not null,
country text not null references countries(country) on update cascade,
unique (country_area, country)
);
create table country_subareas (
country_subarea text not null,
country_area text not null references country_areas(country_area, country) on update cascade,
country text not null references countries(country) on update cascade,
unique (country_subarea, country_area, country)
);
create table cities (
city text not null,
country_area text references country_areas(country_area, country) on update cascade,
country_subarea text references country_subareas(country_subarea, country_area, country) on update cascade
country text not null references countries(country) on update cascade,
unique (city, country, country_area, country_subarea)
);
create table postal_codes (
postal_code text not null,
city text not null references cities(city, country_subarea, country_area, country) on update cascade,
country_area text not null references country_areas(country_area, country) on update cascade,
country_subarea text not null references country_subareas(country_subarea, country_area, country) on update cascade
country text not null references countries(country) on update cascade,
unique (postal_code, city, country_area, country_subarea, country)
);
create table customers (
customer_id uuid primary key default uuid_generate_v4(),
name text,
street text,
street2 text,
postal_code text references postal_codes(postal_code, city, country_subarea, country_area, country) on update cascade,
city text references cities(city, country_subarea, country_area, country) on update cascade,
country_area text references country_areas(country_area, country) on update cascade,
country_subarea text references country_subareas(country_subarea, country_area, country) on update cascade
country text references countries(country) on update cascade
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment