From cd57f5e4128ed55d35fa1b38aee893b0bf845682 Mon Sep 17 00:00:00 2001 From: Gregory Kinne Date: Sun, 9 Jun 2024 17:35:31 -0400 Subject: [PATCH 1/3] making a location table --- hows-the-weather/data/{ => Tables}/airquality.sql | 0 hows-the-weather/data/Tables/location.sql | 7 +++++++ hows-the-weather/data/{ => Tables}/moon.sql | 0 hows-the-weather/data/{ => Tables}/weather.sql | 0 4 files changed, 7 insertions(+) rename hows-the-weather/data/{ => Tables}/airquality.sql (100%) create mode 100644 hows-the-weather/data/Tables/location.sql rename hows-the-weather/data/{ => Tables}/moon.sql (100%) rename hows-the-weather/data/{ => Tables}/weather.sql (100%) diff --git a/hows-the-weather/data/airquality.sql b/hows-the-weather/data/Tables/airquality.sql similarity index 100% rename from hows-the-weather/data/airquality.sql rename to hows-the-weather/data/Tables/airquality.sql diff --git a/hows-the-weather/data/Tables/location.sql b/hows-the-weather/data/Tables/location.sql new file mode 100644 index 0000000..d0c6d50 --- /dev/null +++ b/hows-the-weather/data/Tables/location.sql @@ -0,0 +1,7 @@ +CREATE TABLE [data].[Location]( + [city_name] [varchar](255) NOT NULL, + [state_code] [varchar](2) NOT NULL, + [country_code] [varchar](2) NOT NULL, + [lat] [float](24) NOT NULL, + [lon] [float](24) NOT NULL +); \ No newline at end of file diff --git a/hows-the-weather/data/moon.sql b/hows-the-weather/data/Tables/moon.sql similarity index 100% rename from hows-the-weather/data/moon.sql rename to hows-the-weather/data/Tables/moon.sql diff --git a/hows-the-weather/data/weather.sql b/hows-the-weather/data/Tables/weather.sql similarity index 100% rename from hows-the-weather/data/weather.sql rename to hows-the-weather/data/Tables/weather.sql From 816627c56866d3c25ee14fd30bc8d857891a0ca2 Mon Sep 17 00:00:00 2001 From: Gregory Kinne Date: Sun, 9 Jun 2024 17:37:25 -0400 Subject: [PATCH 2/3] adding some primary keys --- hows-the-weather/data/Tables/location.sql | 9 ++++++++- hows-the-weather/data/Tables/moon.sql | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/hows-the-weather/data/Tables/location.sql b/hows-the-weather/data/Tables/location.sql index d0c6d50..de2725b 100644 --- a/hows-the-weather/data/Tables/location.sql +++ b/hows-the-weather/data/Tables/location.sql @@ -3,5 +3,12 @@ CREATE TABLE [data].[Location]( [state_code] [varchar](2) NOT NULL, [country_code] [varchar](2) NOT NULL, [lat] [float](24) NOT NULL, - [lon] [float](24) NOT NULL + [lon] [float](24) NOT NULL, + + PRIMARY KEY CLUSTERED + ( + [city_name] ASC, + [state_code] ASC, + [country_code] ASC + ) ); \ No newline at end of file diff --git a/hows-the-weather/data/Tables/moon.sql b/hows-the-weather/data/Tables/moon.sql index dc017d4..e4f6e31 100644 --- a/hows-the-weather/data/Tables/moon.sql +++ b/hows-the-weather/data/Tables/moon.sql @@ -13,5 +13,13 @@ CREATE TABLE [data].[Moon]( [country_code] [varchar](2) NOT NULL, [lat] [float](24) NOT NULL, [lon] [float](24) NOT NULL, - [generated_on] [datetime] NOT NULL + [generated_on] [datetime] NOT NULL, + + PRIMARY KEY CLUSTERED + ( + [city_name] ASC, + [state_code] ASC, + [country_code] ASC, + [report_date] ASC + ) ); \ No newline at end of file From c8a638b89e7b93e73d775aab357691eb07904af6 Mon Sep 17 00:00:00 2001 From: Gregory Kinne Date: Sun, 9 Jun 2024 19:11:42 -0400 Subject: [PATCH 3/3] setting up fks to protect location --- hows-the-weather/data/Tables/airquality.sql | 15 ++++----- hows-the-weather/data/Tables/location.sql | 18 ++++++----- hows-the-weather/data/Tables/moon.sql | 35 ++++++++++----------- hows-the-weather/data/Tables/weather.sql | 25 +++++++-------- 4 files changed, 43 insertions(+), 50 deletions(-) diff --git a/hows-the-weather/data/Tables/airquality.sql b/hows-the-weather/data/Tables/airquality.sql index 455056d..3d4dc77 100644 --- a/hows-the-weather/data/Tables/airquality.sql +++ b/hows-the-weather/data/Tables/airquality.sql @@ -1,19 +1,16 @@ CREATE TABLE [data].[AirQuality]( - [city_name] [varchar](255) NOT NULL, - [state_code] [varchar](2) NOT NULL, - [country_code] [varchar](2) NOT NULL, - [lat] [float](24) NULL, - [lon] [float](24) NULL, + [location_id] [int] NOT NULL, [report_date] [date] NOT NULL, [air_quality_index] [float](24) NOT NULL, [dominant_pollutant] [varchar](10) NOT NULL, [generated_on] [datetime] NOT NULL, + INDEX [ix_location_id] NONCLUSTERED ([location_id]), + CONSTRAINT [fk_aq_location_id] FOREIGN KEY ([location_id]) REFERENCES [data].[Location](location_id), + PRIMARY KEY CLUSTERED ( - [city_name] ASC, - [state_code] ASC, - [country_code] ASC, - [report_date] ASC + [report_date] ASC, + [location_id] ASC ) ); \ No newline at end of file diff --git a/hows-the-weather/data/Tables/location.sql b/hows-the-weather/data/Tables/location.sql index de2725b..7760d0d 100644 --- a/hows-the-weather/data/Tables/location.sql +++ b/hows-the-weather/data/Tables/location.sql @@ -1,14 +1,16 @@ CREATE TABLE [data].[Location]( - [city_name] [varchar](255) NOT NULL, - [state_code] [varchar](2) NOT NULL, - [country_code] [varchar](2) NOT NULL, - [lat] [float](24) NOT NULL, - [lon] [float](24) NOT NULL, + [city_name] [varchar](255) NOT NULL, + [state_code] [varchar](2) NOT NULL, + [country_code] [varchar](2) NOT NULL, + [lat] [float](24) NOT NULL, + [lon] [float](24) NOT NULL, + [location_id] [int] IDENTITY(1,1) NOT NULL, + + INDEX [ix_location_id] NONCLUSTERED ([location_id]), + CONSTRAINT [ix_location] UNIQUE ([city_name], [state_code], [country_code]), PRIMARY KEY CLUSTERED ( - [city_name] ASC, - [state_code] ASC, - [country_code] ASC + [location_id] ASC ) ); \ No newline at end of file diff --git a/hows-the-weather/data/Tables/moon.sql b/hows-the-weather/data/Tables/moon.sql index e4f6e31..770d27b 100644 --- a/hows-the-weather/data/Tables/moon.sql +++ b/hows-the-weather/data/Tables/moon.sql @@ -1,25 +1,22 @@ CREATE TABLE [data].[Moon]( - [report_date] [date] NOT NULL, - [moonrise_ts] [datetime] NULL, - [moonrise_az] [float](24) NULL, - [moonset_ts] [datetime] NULL, - [moonset_az] [float](24) NULL, - [high_moon_ts] [datetime] NULL, - [high_moon_dce] [float](24) NULL, - [moonphase_deg] [float](24) NOT NULL, - [moonphase_desc] [varchar](20) NOT NULL, - [city_name] [varchar](255) NOT NULL, - [state_code] [varchar](2) NOT NULL, - [country_code] [varchar](2) NOT NULL, - [lat] [float](24) NOT NULL, - [lon] [float](24) NOT NULL, - [generated_on] [datetime] NOT NULL, + [report_date] [date] NOT NULL, + [moonrise_ts] [datetime] NULL, + [moonrise_az] [float](24) NULL, + [moonset_ts] [datetime] NULL, + [moonset_az] [float](24) NULL, + [high_moon_ts] [datetime] NULL, + [high_moon_dce] [float](24) NULL, + [moonphase_deg] [float](24) NOT NULL, + [moonphase_desc] [varchar](20) NOT NULL, + [location_id] [int] NOT NULL, + [generated_on] [datetime] NOT NULL, + + INDEX [ix_location_id] NONCLUSTERED ([location_id]), + CONSTRAINT [fk__mn_location_id] FOREIGN KEY ([location_id]) REFERENCES [data].[Location](location_id), PRIMARY KEY CLUSTERED ( - [city_name] ASC, - [state_code] ASC, - [country_code] ASC, - [report_date] ASC + [report_date] ASC, + [location_id] ASC ) ); \ No newline at end of file diff --git a/hows-the-weather/data/Tables/weather.sql b/hows-the-weather/data/Tables/weather.sql index 8396d59..f565824 100644 --- a/hows-the-weather/data/Tables/weather.sql +++ b/hows-the-weather/data/Tables/weather.sql @@ -1,21 +1,18 @@ CREATE TABLE [data].[Weather] ( - [city_name] [varchar](255) NOT NULL, - [state_code] [varchar](2) NOT NULL, - [country_code] [varchar](2) NOT NULL, - [lat] [float](24) NULL, - [lon] [float](24) NULL, - [report_date] [date] NOT NULL, - [temperature] [float](24) NOT NULL, - [wind_speed] [float](24) NOT NULL, - [humidity] [float](24) NOT NULL, - [generated_on] [datetime] NOT NULL, + [report_date] [date] NOT NULL, + [location_id] [int] NOT NULL, + [temperature] [float](24) NOT NULL, + [wind_speed] [float](24) NOT NULL, + [humidity] [float](24) NOT NULL, + [generated_on] [datetime] NOT NULL, + + INDEX [ix_location_id] NONCLUSTERED ([location_id]), + CONSTRAINT [fk_wt_location_id] FOREIGN KEY ([location_id]) REFERENCES [data].[Location](location_id), PRIMARY KEY CLUSTERED ( - [city_name] ASC, - [state_code] ASC, - [country_code] ASC, - [report_date] ASC + [report_date] ASC, + [location_id] ASC ) ); \ No newline at end of file