The tables will now look like this.
We'll now move to the 3rd and the last table.
iii. SD_Tut_ReferringURLs
Right-click in the empty white space of the diagram and from the options menu that pops-up, click 'New Table'. Then enter the name SD_Tut_ReferringURLs and hit 'Ok'.
Now add 3 columns to the table as column names, data types, length, and 'allow nulls' given below:
As for the Primary Key, select TrackerDateID and ReferringURL columns and then click the 'Set Primary Key' button in the toolbar. And then click the 'Save' button in the toolbar to create this table in the database.
Explanation
The 3 columns of SD_Tut_ReferringURLs serve the same purpose as the ones in SD_Tut_ReferringDomains, except that the ReferringURL column contains the full referring URL whereas ReferringDomain column contained only the referring domain name. The logic for setting Primary Key on two columns is the same as was in SD_Tut_ReferringDomains table.
To give you an idea as to what kind of data this table will contain, have a look at the example row from this table:
Now that we have created the 3 tables, the last remaining step is to add relationships between them. For that, the first thing to do is to switch the view of SD_Tut_ReferringURLs to 'Column Names' as was described earlier in this tutorial.
Creating Relationships between Tables
We'll be creating two 'One-to-Many' relationships with the column TrackerDateID of SD_Tut_Tracker as the Primary Key and TrackerDateID columns of SD_Tut_ReferringDomains and SD_Tut_ReferringURLs as the Foreign Keys.
To create these relationships in the diagram view of the SQL Server Enterprise Manager, first of all click the TrackerDateID column of SD_Tut_Tracker table. While keeping the left-mouse button pressed, drag that column to the TrackerDateID column of SD_Tut_ReferringDomains table. Now, release the button. A 'Create Relationship' dialogue box will appear on your screen as shown below:
Check both 'Cascade Update Related Fields' and 'Cascade Delete Related Records' checkboxes. This will make sure that no ghost records in the SD_Tut_ReferringDomains and SD_Tut_ReferringURLs tables will exist once the Primary Key of this relationship is updated or deleted in the SD_Tut_Tracker table. Now click 'Ok'.
Repeat the same procedure for creating a 'One-to-Many' relationship between SD_Tut_Tracker and SD_Tut_ReferringURLs tables where the TrackerDateID column of SD_Tut_Tracker will act as the Primary Key and TrackerDateID column of SD_Tut_ReferringURLs table as the Foreign Key. The procedure has been explained already in this tutorial. Also make sure that you check 'Cascade Update Related Fields' and 'Cascade Delete Related Records' checkboxes as was the case with the relationship between SD_Tut_Tracker and SD_Tut_ReferringDomains tables. Now click the 'Save' button in the toolbar to create these relationships in the database.
When you are finished creating these two relationships as was explained above, click the 'Arrange Tables' icon in the toolbar and you will have something like the following image displayed before your eyes:
Overview of the Stored Procedures
We will be creating a total of 14 stored procedures, out of which only one stored procedure will write to the 3 tables we created above, while the other thirteen stored procedures will be used to generate reports which will be displayed by ASP.NET pages later in this tutorial. Following is a list of names of the stored procedures that we will create:
- SD_Tut_GetDailyReport
- SD_Tut_GetDailyReportForReferringDomains
- SD_Tut_GetDailyReportForReferringURLs
- SD_Tut_GetDetailedDailyReportForReferringDomains
- SD_Tut_GetDetailedDailyReportForReferringURLs
- SD_Tut_GetDetailedMonthlyReportForReferringDomains
- SD_Tut_GetDetailedMonthlyReportForReferringURLs
- SD_Tut_GetMonthlyReport
- SD_Tut_GetMonthlyReportForReferringDomains
- SD_Tut_GetMonthlyReportForReferringURLs
- SD_Tut_GetYearlyReport
- SD_Tut_GetYearlyReportForReferringDomains
- SD_Tut_GetYearlyReportForReferringURLs
- SD_Tut_Track
Creating the Stored Procedures
Unlike the procedure for creating the tables, which was a bit lengthy to explain, we will create these stored procedures one-by-one using the SQL Server Query Analyzer, which is a simple and quick procedure and you will be able to execute it within seconds.
i. SD_Tut_GetDailyReport
This stored procedure takes two arguments which can be NULL. What it means is that if you don't supply any arguments, it will use current year and month as the arguments for generating the report. The code for this stored procedure is:
CREATE PROC [dbo].[SD_Tut_GetDailyReport]
@ForYear int = NULL,
@ForMonth int = NULL
AS
SELECT @ForYear = ISNULL(@ForYear, YEAR(GETDATE()))
SELECT @ForMonth = ISNULL(@ForMonth, MONTH(GETDATE()))
SELECT DAY(TrackerDateID) AS 'Day', TrackerUniqueUsers AS 'Unique Users',
TrackerPageViews AS 'Page Views' FROM SD_Tut_Tracker WHERE
YEAR(TrackerDateID) = @ForYear AND MONTH(TrackerDateID) = @ForMonth
ORDER BY DAY(TrackerDateID) ASC
GO
To create this stored procedure, first of all open SQL Server Query Analyzer and complete the 'log-in' procedure so that you connect to the database you are creating the tables/stored procedures in. Then simply copy the code given above and paste it in the Query Analyzer code window and execute it (by pressing the 'F5' key).
Explanation
It displays all the records of the given year and month from the SD_Tut_Tracker table in the format shown below: