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

View File

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