BEGIN;
attach database 'baseball-transformed.db' as 'transformed';

create table if not exists "awards" (
	"ID"	text,
	primary key("ID")
);

insert into awards
select distinct * from "transformed"."awards";

CREATE TABLE IF NOT EXISTS "franchises" (
	"franchID"	TEXT,
	"franchName"	TEXT,
	"active"	TEXT,
	"NAassoc"	TEXT,
	PRIMARY KEY("franchID")
);

insert into franchises
select distinct * from "transformed"."franchises";

create table if not exists "franchiseseasons" (
	"yearID"	NUMERIC,
	"lgID"	TEXT,
	"teamID"	TEXT,
	"franchID"	TEXT,
	"divID"	TEXT,
	"Rank"	NUMERIC,
	"G"	NUMERIC,
	"Ghome"	NUMERIC,
	"W"	NUMERIC,
	"L"	NUMERIC,
	"DivWin"	NUMERIC,
	"WCWin"	NUMERIC,
	"LgWin"	NUMERIC,
	"WSWin"	NUMERIC,
	"R"	NUMERIC,
	"AB"	NUMERIC,
	"H"	NUMERIC,
	"2B"	NUMERIC,
	"3B"	NUMERIC,
	"HR"	NUMERIC,
	"BB"	NUMERIC,
	"SO"	NUMERIC,
	"SB"	NUMERIC,
	"CS"	NUMERIC,
	"HBP"	NUMERIC,
	"SF"	NUMERIC,
	"RA"	NUMERIC,
	"ER"	NUMERIC,
	"ERA"	NUMERIC,
	"CG"	NUMERIC,
	"SHO"	NUMERIC,
	"SV"	NUMERIC,
	"IPouts"	NUMERIC,
	"HA"	NUMERIC,
	"HRA"	NUMERIC,
	"BBA"	NUMERIC,
	"SOA"	NUMERIC,
	"E"	NUMERIC,
	"DP"	NUMERIC,
	"FP"	NUMERIC,
	"name"	NUMERIC,
	"park"	NUMERIC,
	"attendance"	NUMERIC,
	"BPF"	NUMERIC,
	"PPF"	NUMERIC,
	"teamIDBR"	TEXT,
	"teamIDlahman45"	TEXT,
	"teamIDretro"	TEXT,
	PRIMARY KEY("yearID","franchID"),
	foreign key("yearID") references "seasons"("year"),
	foreign key("franchID") references "franchises"("ID")
);

insert into franchiseseasons
select distinct * from "transformed"."franchiseseasons";

create table if not exists "seasons" (
	"year"	numeric,
	primary key("year")
);

insert into seasons
select distinct * from "transformed"."seasons";

CREATE TABLE "people" (
	"playerID"	text,
	"birthYear"	NUMERIC,
	"birthMonth"	NUMERIC,
	"birthDay"	NUMERIC,
	"birthCountry"	text,
	"birthState"	text,
	"birthCity"	text,
	"deathYear"	text,
	"deathMonth"	text,
	"deathDay"	text,
	"deathCountry"	text,
	"deathState"	text,
	"deathCity"	text,
	"nameFirst"	text,
	"nameLast"	text,
	"nameGiven"	text,
	"weight"	NUMERIC,
	"height"	NUMERIC,
	"bats"	text,
	"throws"	text,
	"debut"	text,
	"finalGame"	text,
	"retroID"	text,
	"bbrefID"	text,
	primary key("playerID")
);

INSERT INTO "people" SELECT DISTINCT * FROM "transformed"."people";

CREATE TABLE IF NOT EXISTS "parks" (
	"park.key"	TEXT,
	"park.name"	TEXT,
	"city"	TEXT,
	"state"	TEXT,
	"country"	TEXT,
	PRIMARY KEY("park.key")
);

insert into "parks" select distinct * from "transformed"."parks";

CREATE TABLE IF NOT EXISTS "parkaliases" (
	"ID"	TEXT,
	"alias"	TEXT,
	PRIMARY KEY("ID","alias"),
	foreign key("ID") references "parks"("park.key")
);

insert into "parkaliases" select distinct * from "transformed"."parkaliases";
COMMIT;
