CREATE TABLE IF NOT EXISTS tlds (
  tld_id INTEGER PRIMARY KEY NOT NULL,
  tld TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS domains (
  id INTEGER PRIMARY KEY,
  tld_id INTEGER NOT NULL REFERENCES tlds(tld_id),
  domain_id INTEGER NOT NULL,
  domain TEXT NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS ndx_domains_uniq ON domains(tld_id, domain_id);


CREATE TABLE IF NOT EXISTS subdomains (
  id INTEGER PRIMARY KEY,
  tld_id INTEGER NOT NULL REFERENCES tlds(tld_id),
  domain_id INTEGER NOT NULL REFERENCES domains(domain_id),
  subdomain_id INTEGER NOT NULL,
  subdomain TEXT NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS ndx_subdomains_uniq ON subdomains(tld_id, domain_id, subdomain_id);

CREATE TABLE IF NOT EXISTS paths (
  id INTEGER PRIMARY KEY,
  tld_id INTEGER NOT NULL REFERENCES tlds(tld_id),
  domain_id INTEGER NOT NULL REFERENCES domains(domain_id),
  subdomain_id INTEGER NOT NULL REFERENCES subdomains(subdomain_id),
  path_id INTEGER NOT NULL,
  path TEXT NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS ndx_paths_uniq ON paths(tld_id, domain_id, subdomain_id, path_id);


CREATE TABLE IF NOT EXISTS domain64 (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  rfqdn TEXT,
  d64 BIGINT UNIQUE NOT NULL
);
