CREATE TABLE "GALAXY" (
  "objid_rcsed2" int128 PRIMARY KEY,
  "coord_j2000" double,
  "z_best" double,
  "z_best_err" double,
  "z_best_flag" char,
  "ebv" double
);

CREATE TABLE "SPECTRUM" (
  "objid_spec" int PRIMARY KEY,
  "objid_rcsed2" int128,
  "coord_j2000" double,
  "aperture" double,
  "exptime" double,
  "sdss_mjd" int,
  "sdss_plate" int,
  "sdss_fiberID" int,
  "2df_seqnum" int,
  "6df_specid" int,
  "uzc_zname" char,
  "hectospec_date" char,
  "hectospec_dataset" char,
  "hectospec_spec" char,
  "lamost_mjd" int,
  "lamost_spid" int,
  "lamost_fiberid" int,
  "deep2_objno" int,
  "deep3_objno" int,
  "wigglez_specfile" char,
  "gama_specid" int,
  "2dflens_target" int
);

CREATE TABLE "TOT_PHOT" (
  "objid_rcsed2" int128,
  "phot_source" array[],
  "mag_type" char,
  "mag_radius" double,
  "FUV" double,
  "FUV_err" double,
  "kcorr_FUV" double,
  "NUV" double,
  "NUV_err" double,
  "kcorr_NUV" double,
  "u" double,
  "u_err" double,
  "kcorr_u" double,
  "g" double,
  "g_err" double,
  "kcorr_g" double,
  "r" double,
  "r_err" double,
  "kcorr_r" double,
  "i" double,
  "i_err" double,
  "kcorr_i" double,
  "z" double,
  "z_err" double,
  "kcorr_z" double,
  "Y" double,
  "Y_err" double,
  "kcorr_Y" double,
  "J" double,
  "J_err" double,
  "kcorr_J" double,
  "H" double,
  "H_err" double,
  "kcorr_H" double,
  "Ks" double,
  "Ks_err" double,
  "kcorr_Ks" double,
  "w1" double,
  "w1_err" double,
  "kcorr_w1" double,
  "w2" double,
  "w2_err" double,
  "kcorr_w2" double,
  "w3" double,
  "w3_err" double,
  "kcorr_w3" double
);

CREATE TABLE "APER_PHOT_SDSS" (
  "objid_rcsed2" int128,
  "phot_source" array[],
  "FUV" double,
  "FUV_err" double,
  "kcorr_FUV" double,
  "NUV" double,
  "NUV_err" double,
  "kcorr_NUV" double,
  "u" double,
  "u_err" double,
  "kcorr_u" double,
  "g" double,
  "g_err" double,
  "kcorr_g" double,
  "r" double,
  "r_err" double,
  "kcorr_r" double,
  "i" double,
  "i_err" double,
  "kcorr_i" double,
  "z" double,
  "z_err" double,
  "kcorr_z" double,
  "Y" double,
  "Y_err" double,
  "kcorr_Y" double,
  "J" double,
  "J_err" double,
  "kcorr_J" double,
  "H" double,
  "H_err" double,
  "kcorr_H" double,
  "Ks" double,
  "Ks_err" double,
  "kcorr_Ks" double,
  "w1" double,
  "w1_err" double,
  "kcorr_w1" double,
  "w2" double,
  "w2_err" double,
  "kcorr_w2" double,
  "w3" double,
  "w3_err" double,
  "kcorr_w3" double
);

CREATE TABLE "APER_PHOT_2DF" (
  "objid_rcsed2" int128,
  "phot_source" array[],
  "FUV" double,
  "FUV_err" double,
  "kcorr_FUV" double,
  "NUV" double,
  "NUV_err" double,
  "kcorr_NUV" double,
  "u" double,
  "u_err" double,
  "kcorr_u" double,
  "g" double,
  "g_err" double,
  "kcorr_g" double,
  "r" double,
  "r_err" double,
  "kcorr_r" double,
  "i" double,
  "i_err" double,
  "kcorr_i" double,
  "z" double,
  "z_err" double,
  "kcorr_z" double,
  "Y" double,
  "Y_err" double,
  "kcorr_Y" double,
  "J" double,
  "J_err" double,
  "kcorr_J" double,
  "H" double,
  "H_err" double,
  "kcorr_H" double,
  "Ks" double,
  "Ks_err" double,
  "kcorr_Ks" double,
  "w1" double,
  "w1_err" double,
  "kcorr_w1" double,
  "w2" double,
  "w2_err" double,
  "kcorr_w2" double,
  "w3" double,
  "w3_err" double,
  "kcorr_w3" double
);

CREATE TABLE "APER_PHOT_6DF" (
  "objid_rcsed2" int128,
  "phot_source" array[],
  "FUV" double,
  "FUV_err" double,
  "kcorr_FUV" double,
  "NUV" double,
  "NUV_err" double,
  "kcorr_NUV" double,
  "u" double,
  "u_err" double,
  "kcorr_u" double,
  "g" double,
  "g_err" double,
  "kcorr_g" double,
  "r" double,
  "r_err" double,
  "kcorr_r" double,
  "i" double,
  "i_err" double,
  "kcorr_i" double,
  "z" double,
  "z_err" double,
  "kcorr_z" double,
  "Y" double,
  "Y_err" double,
  "kcorr_Y" double,
  "J" double,
  "J_err" double,
  "kcorr_J" double,
  "H" double,
  "H_err" double,
  "kcorr_H" double,
  "Ks" double,
  "Ks_err" double,
  "kcorr_Ks" double,
  "w1" double,
  "w1_err" double,
  "kcorr_w1" double,
  "w2" double,
  "w2_err" double,
  "kcorr_w2" double,
  "w3" double,
  "w3_err" double,
  "kcorr_w3" double
);

CREATE TABLE "APER_PHOT_HECT" (
  "objid_rcsed2" int128,
  "phot_source" array[],
  "FUV" double,
  "FUV_err" double,
  "kcorr_FUV" double,
  "NUV" double,
  "NUV_err" double,
  "kcorr_NUV" double,
  "u" double,
  "u_err" double,
  "kcorr_u" double,
  "g" double,
  "g_err" double,
  "kcorr_g" double,
  "r" double,
  "r_err" double,
  "kcorr_r" double,
  "i" double,
  "i_err" double,
  "kcorr_i" double,
  "z" double,
  "z_err" double,
  "kcorr_z" double,
  "Y" double,
  "Y_err" double,
  "kcorr_Y" double,
  "J" double,
  "J_err" double,
  "kcorr_J" double,
  "H" double,
  "H_err" double,
  "kcorr_H" double,
  "Ks" double,
  "Ks_err" double,
  "kcorr_Ks" double,
  "w1" double,
  "w1_err" double,
  "kcorr_w1" double,
  "w2" double,
  "w2_err" double,
  "kcorr_w2" double,
  "w3" double,
  "w3_err" double,
  "kcorr_w3" double
);

CREATE TABLE "APER_PHOT_LAMOST" (
  "objid_rcsed2" int128,
  "phot_source" array[],
  "FUV" double,
  "FUV_err" double,
  "kcorr_FUV" double,
  "NUV" double,
  "NUV_err" double,
  "kcorr_NUV" double,
  "u" double,
  "u_err" double,
  "kcorr_u" double,
  "g" double,
  "g_err" double,
  "kcorr_g" double,
  "r" double,
  "r_err" double,
  "kcorr_r" double,
  "i" double,
  "i_err" double,
  "kcorr_i" double,
  "z" double,
  "z_err" double,
  "kcorr_z" double,
  "Y" double,
  "Y_err" double,
  "kcorr_Y" double,
  "J" double,
  "J_err" double,
  "kcorr_J" double,
  "H" double,
  "H_err" double,
  "kcorr_H" double,
  "Ks" double,
  "Ks_err" double,
  "kcorr_Ks" double,
  "w1" double,
  "w1_err" double,
  "kcorr_w1" double,
  "w2" double,
  "w2_err" double,
  "kcorr_w2" double,
  "w3" double,
  "w3_err" double,
  "kcorr_w3" double
);

CREATE TABLE "FILTER_DATA" (
  "filter_id" int,
  "zp_ab" double,
  "zp_vega" double,
  "zp_st" double,
  "transmission" "hstore(array[], array[])",
  "lambda_eff" double,
  "fwhm" double,
  "R" double
);

CREATE TABLE "PHOT_INFO" (
  "objid_rcsed2" int128,
  "filter_id" int,
  "coord_j2000" double,
  "data_src_phot" char,
  "aper_corr" double,
  "ebv" double,
  "ell" double,
  "pa" double
);

CREATE TABLE "FIT_EMIS_RESULT" (
  "objid_rcsed2" int128,
  "objid_spec" int PRIMARY KEY,
  "aper_ids" int,
  "nburst_run_id" int,
  "emis_line" char,
  "wl_atm" double,
  "wl_vac" double,
  "wl_obs_atm" double,
  "wl_obs_vac" double,
  "flux" double,
  "flux_err" double,
  "cont" double,
  "cont_err" double,
  "ew" double,
  "ew_err" double,
  "v" double,
  "v_err" double,
  "h3" double,
  "h3_err" double,
  "h4" double,
  "h4_err" double,
  "h5" double,
  "h5_err" double,
  "h6" double,
  "h6_err" double
);

CREATE TABLE "FIT_RESULT" (
  "objid_rcsed2" int128,
  "objid_spec" int PRIMARY KEY,
  "aper_ids" int,
  "nburst_run_id" int,
  "type" char,
  "v" double,
  "v_err" double,
  "sig" double,
  "sig_err" double,
  "age" double,
  "age_err" double,
  "h3" double,
  "h3_err" double,
  "h4" double,
  "h4_err" double,
  "met" double,
  "met_err" double,
  "mgfe" double,
  "mgfe_err" double,
  "chi2" double
);

CREATE TABLE "OBJTYPE" (
  "objid_rcsed2" int128 PRIMARY KEY,
  "type_id" int
);

CREATE TABLE "TYPE" (
  "type_id" int PRIMARY KEY,
  "type_label" char
);

ALTER TABLE "SPECTRUM" ADD FOREIGN KEY ("objid_rcsed2") REFERENCES "GALAXY" ("objid_rcsed2");

ALTER TABLE "TOT_PHOT" ADD FOREIGN KEY ("objid_rcsed2") REFERENCES "GALAXY" ("objid_rcsed2");

ALTER TABLE "APER_PHOT_SDSS" ADD FOREIGN KEY ("objid_rcsed2") REFERENCES "GALAXY" ("objid_rcsed2");

ALTER TABLE "APER_PHOT_2DF" ADD FOREIGN KEY ("objid_rcsed2") REFERENCES "GALAXY" ("objid_rcsed2");

ALTER TABLE "APER_PHOT_6DF" ADD FOREIGN KEY ("objid_rcsed2") REFERENCES "GALAXY" ("objid_rcsed2");

ALTER TABLE "APER_PHOT_HECT" ADD FOREIGN KEY ("objid_rcsed2") REFERENCES "GALAXY" ("objid_rcsed2");

ALTER TABLE "APER_PHOT_LAMOST" ADD FOREIGN KEY ("objid_rcsed2") REFERENCES "GALAXY" ("objid_rcsed2");

ALTER TABLE "PHOT_INFO" ADD FOREIGN KEY ("objid_rcsed2") REFERENCES "GALAXY" ("objid_rcsed2");

ALTER TABLE "PHOT_INFO" ADD FOREIGN KEY ("filter_id") REFERENCES "FILTER_DATA" ("filter_id");

ALTER TABLE "FIT_EMIS_RESULT" ADD FOREIGN KEY ("objid_rcsed2") REFERENCES "GALAXY" ("objid_rcsed2");

ALTER TABLE "FIT_EMIS_RESULT" ADD FOREIGN KEY ("objid_spec") REFERENCES "SPECTRUM" ("objid_spec");

ALTER TABLE "FIT_EMIS_RESULT" ADD FOREIGN KEY ("nburst_run_id") REFERENCES "FIT_RESULT" ("nburst_run_id");

ALTER TABLE "FIT_RESULT" ADD FOREIGN KEY ("objid_rcsed2") REFERENCES "GALAXY" ("objid_rcsed2");

ALTER TABLE "FIT_RESULT" ADD FOREIGN KEY ("objid_spec") REFERENCES "SPECTRUM" ("objid_spec");

ALTER TABLE "OBJTYPE" ADD FOREIGN KEY ("objid_rcsed2") REFERENCES "GALAXY" ("objid_rcsed2");

ALTER TABLE "OBJTYPE" ADD FOREIGN KEY ("type_id") REFERENCES "TYPE" ("type_id");
