|
| 1 | +-- Create or select the database |
| 2 | +IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'CricketDB') |
| 3 | +BEGIN |
| 4 | + CREATE DATABASE CricketDB; |
| 5 | +END; |
| 6 | +GO |
| 7 | + |
| 8 | +-- Select the database |
| 9 | +USE CricketDB; |
| 10 | +GO |
| 11 | + |
| 12 | +-- Drop existing tables if they exist (for a fresh start) |
| 13 | +IF OBJECT_ID('MatchTeamBridge', 'U') IS NOT NULL DROP TABLE MatchTeamBridge; |
| 14 | +IF OBJECT_ID('MatchPlayerBridge', 'U') IS NOT NULL DROP TABLE MatchPlayerBridge; |
| 15 | +IF OBJECT_ID('MatchDateBridge', 'U') IS NOT NULL DROP TABLE MatchDateBridge; |
| 16 | +IF OBJECT_ID('MatchInningsBridge', 'U') IS NOT NULL DROP TABLE MatchInningsBridge; |
| 17 | +IF OBJECT_ID('FactMatch', 'U') IS NOT NULL DROP TABLE FactMatch; |
| 18 | +IF OBJECT_ID('DimDate', 'U') IS NOT NULL DROP TABLE DimDate; |
| 19 | +IF OBJECT_ID('DimCity', 'U') IS NOT NULL DROP TABLE DimCity; |
| 20 | +IF OBJECT_ID('DimWicket', 'U') IS NOT NULL DROP TABLE DimWicket; |
| 21 | +IF OBJECT_ID('DimDeliveries', 'U') IS NOT NULL DROP TABLE DimDeliveries; |
| 22 | +IF OBJECT_ID('DimOvers', 'U') IS NOT NULL DROP TABLE DimOvers; |
| 23 | +IF OBJECT_ID('DimInnings', 'U') IS NOT NULL DROP TABLE DimInnings; |
| 24 | +IF OBJECT_ID('DimPlayer', 'U') IS NOT NULL DROP TABLE DimPlayer; |
| 25 | +IF OBJECT_ID('DimTeam', 'U') IS NOT NULL DROP TABLE DimTeam; |
| 26 | +GO |
| 27 | + |
| 28 | + |
| 29 | +-- Create DimDate |
| 30 | +CREATE TABLE DimDate ( |
| 31 | + date_id INT PRIMARY KEY IDENTITY(1,1), |
| 32 | + date DATE NOT NULL, |
| 33 | + day_of_week INT NOT NULL, |
| 34 | + day_of_month INT NOT NULL, |
| 35 | + month INT NOT NULL, |
| 36 | + year INT NOT NULL, |
| 37 | + quarter INT NOT NULL, |
| 38 | + day_name VARCHAR(20) NOT NULL, |
| 39 | + month_name VARCHAR(20) NOT NULL, |
| 40 | + is_weekend BIT NOT NULL |
| 41 | +); |
| 42 | +GO |
| 43 | + |
| 44 | +-- Create DimTeam |
| 45 | +CREATE TABLE DimTeam ( |
| 46 | + team_id INT PRIMARY KEY IDENTITY(1,1), |
| 47 | + team_name VARCHAR(255) NOT NULL |
| 48 | +); |
| 49 | +GO |
| 50 | + |
| 51 | +-- Create DimPlayer |
| 52 | +CREATE TABLE DimPlayer ( |
| 53 | + player_id INT PRIMARY KEY IDENTITY(1,1), |
| 54 | + player_name VARCHAR(255) NOT NULL |
| 55 | +); |
| 56 | +GO |
| 57 | + |
| 58 | +-- Create DimVenue |
| 59 | +CREATE TABLE DimCity ( |
| 60 | + city_id INT PRIMARY KEY IDENTITY(1,1), |
| 61 | + city VARCHAR(255) NOT NULL, |
| 62 | +); |
| 63 | +GO |
| 64 | + |
| 65 | +-- Create DimInnings |
| 66 | +CREATE TABLE DimInnings ( |
| 67 | + innings_id INT PRIMARY KEY, |
| 68 | + innings_number INT NOT NULL, -- 1st innings, 2nd innings, etc. |
| 69 | + team_id INT NOT NULL, -- Batting team |
| 70 | + FOREIGN KEY (team_id) REFERENCES DimTeam(team_id) |
| 71 | +); |
| 72 | +GO |
| 73 | + |
| 74 | +-- Create DimOver |
| 75 | +CREATE TABLE DimOvers ( |
| 76 | + over_id INT PRIMARY KEY, |
| 77 | + innings_id INT NOT NULL, |
| 78 | + over_number INT NOT NULL, |
| 79 | + FOREIGN KEY (innings_id) REFERENCES DimInnings(innings_id) |
| 80 | +); |
| 81 | +GO |
| 82 | + |
| 83 | +-- Create DimDelivery |
| 84 | +CREATE TABLE DimDeliveries ( |
| 85 | + delivery_id INT PRIMARY KEY, |
| 86 | + over_id INT NOT NULL, |
| 87 | + delivery_number INT NOT NULL, |
| 88 | + bowler_id INT NOT NULL, |
| 89 | + batsman_id INT NOT NULL, |
| 90 | + non_striker_id INT NOT NULL, |
| 91 | + total_runs INT NOT NULL, |
| 92 | + batter_runs INT NOT NULL, |
| 93 | + extras INT DEFAULT 0, -- Byes, leg-byes, no-balls, etc. |
| 94 | + wicket_taken BIT DEFAULT 0, |
| 95 | + FOREIGN KEY (over_id) REFERENCES DimOvers(over_id), |
| 96 | + FOREIGN KEY (bowler_id) REFERENCES DimPlayer(player_id), |
| 97 | + FOREIGN KEY (batsman_id) REFERENCES DimPlayer(player_id), |
| 98 | + FOREIGN KEY (non_striker_id) REFERENCES DimPlayer(player_id) |
| 99 | +); |
| 100 | +GO |
| 101 | + |
| 102 | +-- Create DimWicket |
| 103 | +CREATE TABLE DimWicket ( |
| 104 | + wicket_id INT PRIMARY KEY, |
| 105 | + delivery_id INT NOT NULL, |
| 106 | + batsman_out_id INT NOT NULL, |
| 107 | + kind VARCHAR(50), -- Caught, Bowled, LBW, etc. |
| 108 | + FOREIGN KEY (delivery_id) REFERENCES DimDeliveries(delivery_id), |
| 109 | + FOREIGN KEY (batsman_out_id) REFERENCES DimPlayer(player_id) |
| 110 | +); |
| 111 | +GO |
| 112 | + |
| 113 | +-- Create FactMatch |
| 114 | +CREATE TABLE FactMatch ( |
| 115 | + match_id INT PRIMARY KEY, |
| 116 | + city_id INT NOT NULL, |
| 117 | + match_type NVARCHAR(50), |
| 118 | + result NVARCHAR(50), |
| 119 | + team1_id INT NOT NULL, |
| 120 | + team2_id INT NOT NULL, |
| 121 | + total_overs INT, |
| 122 | + total_runs INT, |
| 123 | + winning_team_id INT, |
| 124 | + win_type NVARCHAR(50), |
| 125 | + win_margin INT, |
| 126 | + event_name NVARCHAR(MAX), |
| 127 | + FOREIGN KEY (city_id) REFERENCES DimCity(city_id), |
| 128 | + FOREIGN KEY (winning_team_id) REFERENCES DimTeam(team_id) |
| 129 | +); |
| 130 | +GO |
| 131 | + |
| 132 | +-- Create MatchPlayerBridge |
| 133 | +CREATE TABLE MatchPlayerBridge ( |
| 134 | + player_id INT NOT NULL, |
| 135 | + match_id INT NOT NULL, |
| 136 | + team_id INT NOT NULL, |
| 137 | + PRIMARY KEY (match_id, player_id, team_id), |
| 138 | + FOREIGN KEY (match_id) REFERENCES FactMatch(match_id), |
| 139 | + FOREIGN KEY (team_id) REFERENCES DimTeam(team_id), |
| 140 | + FOREIGN KEY (player_id) REFERENCES DimPlayer(player_id) |
| 141 | +); |
| 142 | +GO |
| 143 | + |
| 144 | +-- Create MatchDateBridge |
| 145 | +CREATE TABLE MatchDateBridge ( |
| 146 | + match_id INT NOT NULL, |
| 147 | + date_id INT NOT NULL, |
| 148 | + PRIMARY KEY (match_id, date_id), |
| 149 | + FOREIGN KEY (match_id) REFERENCES FactMatch(match_id), |
| 150 | + FOREIGN KEY (date_id) REFERENCES DimDate(date_id) |
| 151 | +); |
| 152 | +GO |
| 153 | + |
| 154 | +-- Create MatchDateBridge |
| 155 | +CREATE TABLE MatchInningsBridge ( |
| 156 | + match_id INT NOT NULL, |
| 157 | + innings_id INT NOT NULL, |
| 158 | + PRIMARY KEY (match_id, innings_id), |
| 159 | + FOREIGN KEY (match_id) REFERENCES FactMatch(match_id), |
| 160 | + FOREIGN KEY (innings_id) REFERENCES DimInnings(innings_id) |
| 161 | +); |
| 162 | +GO |
| 163 | + |
0 commit comments