begin;
attach database 'baseball-raw.db' as 'raw';

create temp table appearances as
select * from "raw".appearances;

alter table appearances drop column "lgID";

update appearances
set teamid = 'WS9'
where teamid = 'WAS' and yearid between 1891 and 1899;

update appearances
set teamid = 'PHP'
where teamid = 'PH4' and yearid between 1890 and 1891;

CREATE TABLE IF NOT EXISTS "appearances" (
	"year"	NUMERIC,
	"team"	TEXT,
	"player"	TEXT,
	"G_all"	NUMERIC,
	"GS"	NUMERIC,
	"G_batting"	NUMERIC,
	"G_defense"	NUMERIC,
	"G_p"	NUMERIC,
	"G_c"	NUMERIC,
	"G_1b"	NUMERIC,
	"G_2b"	NUMERIC,
	"G_3b"	NUMERIC,
	"G_ss"	NUMERIC,
	"G_lf"	NUMERIC,
	"G_cf"	NUMERIC,
	"G_rf"	NUMERIC,
	"G_of"	NUMERIC,
	"G_dh"	NUMERIC,
	"G_ph"	NUMERIC,
	"G_pr"	NUMERIC,
	PRIMARY KEY("year","team","player"),
	foreign key("player") references "people"("ID"),
	foreign key("year","team") references "teamseasons"("year","team")
);

insert into main.appearances select distinct * from temp.appearances;
commit;
