pragma foreign_keys = 0;

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

create temp table if not exists appearancespost (
	"year"	numeric,
	"player"	text,
	"team"	text
);

insert into appearancespost
select distinct yearID, playerID, teamID
from 'raw'.battingpost
union
select distinct yearID, playerID, teamID
from 'raw'.pitchingpost
union
select distinct yearID, playerID, teamID
from 'raw'.fieldingpost;

create table if not exists appearancespost (
	"year"	numeric,
	"player"	text,
	"team"	text,
	primary key("year","player"),
	foreign key("player") references "people"("ID"),
	foreign key("year","team") references "teamseasons"("year","team")
);

insert into main.appearancespost select distinct * from temp."appearancespost";
commit;
