add team foreign keys

This commit is contained in:
Nick Griffey 2024-01-27 21:29:07 -06:00
parent 9f0ac4098b
commit 67fc105f66
2 changed files with 15 additions and 7 deletions

View File

@ -71,6 +71,7 @@ create table if not exists "franchiseseasons" (
"teamIDretro" TEXT, "teamIDretro" TEXT,
PRIMARY KEY("year","franchise"), PRIMARY KEY("year","franchise"),
foreign key("year") references "seasons"("year"), foreign key("year") references "seasons"("year"),
foreign key("team") references "teamcodes"("ID"),
foreign key("franchise") references "franchises"("ID") foreign key("franchise") references "franchises"("ID")
); );
@ -164,6 +165,7 @@ CREATE TABLE IF NOT EXISTS "salaries" (
"salary" NUMERIC, "salary" NUMERIC,
PRIMARY KEY("year","player","team"), PRIMARY KEY("year","player","team"),
foreign key("player") references "people"("ID"), foreign key("player") references "people"("ID"),
foreign key("team") references "teamcodes"("ID"),
foreign key("year") references "seasons"("year") foreign key("year") references "seasons"("year")
); );
@ -193,6 +195,7 @@ CREATE TABLE IF NOT EXISTS "batting" (
"GIDP" NUMERIC, "GIDP" NUMERIC,
PRIMARY KEY("player","year","stint","team"), PRIMARY KEY("player","year","stint","team"),
foreign key("player") references "people"("ID"), foreign key("player") references "people"("ID"),
foreign key("team") references "teamcodes"("ID"),
foreign key("year") references "seasons"("year") foreign key("year") references "seasons"("year")
); );
@ -218,6 +221,7 @@ CREATE TABLE IF NOT EXISTS "fielding" (
"ZR" NUMERIC, "ZR" NUMERIC,
PRIMARY KEY("player","year","stint","team","POS"), PRIMARY KEY("player","year","stint","team","POS"),
foreign key("player") references "people"("ID"), foreign key("player") references "people"("ID"),
foreign key("team") references "teamcodes"("ID"),
foreign key("year") references "seasons"("year") foreign key("year") references "seasons"("year")
); );
@ -255,6 +259,7 @@ CREATE TABLE IF NOT EXISTS "pitching" (
"GIDP" NUMERIC, "GIDP" NUMERIC,
PRIMARY KEY("player","year","stint","team"), PRIMARY KEY("player","year","stint","team"),
foreign key("player") references "people"("ID"), foreign key("player") references "people"("ID"),
foreign key("team") references "teamcodes"("ID"),
foreign key("year") references "seasons"("year") foreign key("year") references "seasons"("year")
); );
@ -283,6 +288,7 @@ CREATE TABLE IF NOT EXISTS "appearances" (
"G_pr" NUMERIC, "G_pr" NUMERIC,
PRIMARY KEY("year","team","player"), PRIMARY KEY("year","team","player"),
foreign key("player") references "people"("ID"), foreign key("player") references "people"("ID"),
foreign key("team") references "teamcodes"("ID"),
foreign key("year") references "seasons"("year") foreign key("year") references "seasons"("year")
); );
@ -299,6 +305,7 @@ CREATE TABLE IF NOT EXISTS "homegames" (
"attendance" NUMERIC, "attendance" NUMERIC,
PRIMARY KEY("year","team","park"), PRIMARY KEY("year","team","park"),
foreign key("park") references "parks"("ID"), foreign key("park") references "parks"("ID"),
foreign key("team") references "teamcodes"("ID"),
foreign key("year") references "seasons"("year") foreign key("year") references "seasons"("year")
); );
@ -338,15 +345,16 @@ CREATE TABLE IF NOT EXISTS "fieldingofsplit" (
"ZR" NUMERIC, "ZR" NUMERIC,
PRIMARY KEY("player","year","stint","team","POS"), PRIMARY KEY("player","year","stint","team","POS"),
foreign key("player") references "people"("ID"), foreign key("player") references "people"("ID"),
foreign key("team") references "teamcodes"("ID"),
foreign key("year") references "seasons"("year") foreign key("year") references "seasons"("year")
); );
insert into fieldingofsplit select distinct * from "transformed"."fieldingofsplit"; insert into fieldingofsplit select distinct * from "transformed"."fieldingofsplit";
create table if not exists teamcodes ( create table if not exists teamcodes (
"team" text, "ID" text,
"franchise" text, "franchise" text,
primary key("team"), primary key("ID"),
foreign key("franchise") references "franchises"("ID") foreign key("franchise") references "franchises"("ID")
); );

View File

@ -1,7 +1,7 @@
begin; begin;
attach database 'baseball-raw.db' as 'raw'; attach database 'baseball-raw.db' as 'raw';
create table if not exists teamcodes ( create table if not exists teamcodes (
"team" text, "ID" text,
"franchise" text "franchise" text
); );
@ -9,10 +9,10 @@ insert into teamcodes
select distinct teamid, franchid from "raw"."teams"; select distinct teamid, franchid from "raw"."teams";
update teamcodes update teamcodes
set team = 'WS9' set ID = 'WS9'
where team = 'WAS' and franchise = 'WAS'; where ID = 'WAS' and franchise = 'WAS';
update teamcodes update teamcodes
set team = 'PHP' set ID = 'PHP'
where team = 'PH4' and franchise = 'PHQ'; where ID = 'PH4' and franchise = 'PHQ';
commit; commit;