0% found this document useful (0 votes)
8 views18 pages

Print DB

The document outlines a project assignment for a Database Systems course, detailing the creation of an ER diagram and SQL schema for a sports-related database. It includes table definitions for entities such as Injuries, Matches, Players, PlayerStatistics, PointsTable, Teams, Tickets, and Users, along with foreign key relationships. Additionally, it presents various SQL commands to analyze player and team statistics based on specific problem statements.

Uploaded by

anasfaizsahi6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views18 pages

Print DB

The document outlines a project assignment for a Database Systems course, detailing the creation of an ER diagram and SQL schema for a sports-related database. It includes table definitions for entities such as Injuries, Matches, Players, PlayerStatistics, PointsTable, Teams, Tickets, and Users, along with foreign key relationships. Additionally, it presents various SQL commands to analyze player and team statistics based on specific problem statements.

Uploaded by

anasfaizsahi6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Session 2023 – 2027

Project Assignment

Submitted by:

Anas Saahi 2023-CS-26

Supervised by:

Dr. Amjad Farooq

Course:

Database Systems

Department of Computer Science

University of Engineering and Technology


Lahore Pakistan
2

ER Diagram:
3

Schema for creating tables:

USE [PSL ]

GO

/****** Object: Table [dbo].[Injuries] Script Date: 24/03/2024 2:54:43 pm ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[Injuries](

[PlayerID] [int] NULL,

[InjuryType] [varchar](255) NULL,

[DateOfInjury] [date] NULL,

[Status] [varchar](50) NULL

) ON [PRIMARY]

GO

/****** Object: Table [dbo].[Matches] Script Date: 24/03/2024 2:54:44 pm ******/


4

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[Matches](

[MatchID] [int] NOT NULL,

[Date] [date] NULL,

[Venue] [varchar](255) NULL,

[Team1ID] [int] NULL,

[Team2ID] [int] NULL,

[Winner] [int] NULL,

[ManOfTheMatch] [int] NULL,

PRIMARY KEY CLUSTERED

[MatchID] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =


OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON,
OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

GO
5

/****** Object: Table [dbo].[Players] Script Date: 24/03/2024 2:54:44 pm ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[Players](

[PlayerID] [int] NOT NULL,

[TeamID] [int] NULL,

[PlayerName] [varchar](255) NULL,

[Role] [varchar](50) NULL,

[BattingAverage] [float] NULL,

[BowlingAverage] [float] NULL,

PRIMARY KEY CLUSTERED

[PlayerID] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =


OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON,
OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

GO
6

/****** Object: Table [dbo].[PlayerStatistics] Script Date: 24/03/2024 2:54:44 pm ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[PlayerStatistics](

[PlayerID] [int] NOT NULL,

[MatchesPlayed] [int] NULL,

[RunsScored] [int] NULL,

[WicketsTaken] [int] NULL,

[BattingStrikeRate] [float] NULL,

[BowlingEconomy] [float] NULL,

PRIMARY KEY CLUSTERED

[PlayerID] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =


OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON,
OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

GO
7

/****** Object: Table [dbo].[PointsTable] Script Date: 24/03/2024 2:54:44 pm ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[PointsTable](

[TeamID] [int] NOT NULL,

[MatchesPlayed] [int] NULL,

[MatchesWon] [int] NULL,

[MatchesLost] [int] NULL,

[Points] [int] NULL,

PRIMARY KEY CLUSTERED

[TeamID] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =


OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON,
OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

GO

/****** Object: Table [dbo].[Teams] Script Date: 24/03/2024 2:54:44 pm ******/


8

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[Teams](

[TeamID] [int] NOT NULL,

[TeamName] [varchar](255) NULL,

[Owner] [varchar](255) NULL,

[Coach] [varchar](255) NULL,

[HomeGround] [varchar](255) NULL,

PRIMARY KEY CLUSTERED

[TeamID] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =


OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON,
OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

GO

/****** Object: Table [dbo].[Tickets] Script Date: 24/03/2024 2:54:44 pm ******/

SET ANSI_NULLS ON
9

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[Tickets](

[TicketID] [int] NOT NULL,

[MatchID] [int] NULL,

[SeatNo] [varchar](50) NULL,

[Price] [decimal](10, 2) NULL,

[Status] [varchar](50) NULL,

PRIMARY KEY CLUSTERED

[TicketID] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =


OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON,
OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

GO

/****** Object: Table [dbo].[Users] Script Date: 24/03/2024 2:54:44 pm ******/

SET ANSI_NULLS ON

GO
10

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[Users](

[UserID] [int] NOT NULL,

[Username] [varchar](255) NULL,

[Password] [varchar](255) NULL,

[Role] [varchar](50) NULL,

PRIMARY KEY CLUSTERED

[UserID] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =


OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON,
OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Injuries] WITH CHECK ADD FOREIGN KEY([PlayerID])

REFERENCES [dbo].[Players] ([PlayerID])

GO

ALTER TABLE [dbo].[Matches] WITH CHECK ADD FOREIGN KEY([ManOfTheMatch])


11

REFERENCES [dbo].[Players] ([PlayerID])

GO

ALTER TABLE [dbo].[Matches] WITH CHECK ADD FOREIGN KEY([Team1ID])

REFERENCES [dbo].[Teams] ([TeamID])

GO

ALTER TABLE [dbo].[Matches] WITH CHECK ADD FOREIGN KEY([Team2ID])

REFERENCES [dbo].[Teams] ([TeamID])

GO

ALTER TABLE [dbo].[Matches] WITH CHECK ADD FOREIGN KEY([Winner])

REFERENCES [dbo].[Teams] ([TeamID])

GO

ALTER TABLE [dbo].[Players] WITH CHECK ADD FOREIGN KEY([TeamID])

REFERENCES [dbo].[Teams] ([TeamID])

GO

ALTER TABLE [dbo].[PlayerStatistics] WITH CHECK ADD FOREIGN KEY([PlayerID])

REFERENCES [dbo].[Players] ([PlayerID])


12

GO

ALTER TABLE [dbo].[PointsTable] WITH CHECK ADD FOREIGN KEY([TeamID])

REFERENCES [dbo].[Teams] ([TeamID])

GO

ALTER TABLE [dbo].[Tickets] WITH CHECK ADD FOREIGN KEY([MatchID])

REFERENCES [dbo].[Matches] ([MatchID])

GO
13

Problem Statements:

1. Determine the number of matches played by each player and the number of matches
drawn by each player.
2. Identify the players who have scored the most runs in a particular year.
3. Calculate the total number of runs scored by each player and the number of wickets
taken by each player.
4. Determine the total number of matches played by each player and the number of
matches won by each player.
5. Identify the players who have taken the most wickets in a particular season.
6. Calculate the total number of wickets taken by each player and the number of runs
conceded by each player.
7. Identify the players who have scored the most runs in a particular match.
8. Determine the total number of matches played by each player and the number of
matches won by each player.
9. Identify the players who have taken the most wickets in a particular season.
10. Calculate the total number of wickets taken by each player and the number of runs
conceded by each player.
11. Identify the players who have scored the most runs in a particular year.
12. Determine the number of matches played by each player and the number of matches
lost by each player.
13. Identify the players who have scored the most centuries in a particular season.
14. Calculate the total number of runs scored by each team and the number of wickets
taken by each team.
15. Determine the total number of matches played by each team and the number of
matches won by each team.
16. Identify the players who have taken the most wickets in a particular match.
17. Calculate the total number of wickets taken by each team and the number of runs
conceded by each team.
18. Identify the players who have scored the most runs in a particular match.
19. Determine the total number of matches played by each player and the number of
matches won by each player.
20. Iden
14

SQL Commands:

-- 1. Determine the number of matches played by each player and the number of matches
drawn by each player.
SELECT Players.PlayerID, COUNT(*) AS TotalMatchesPlayed,
SUM(CASE WHEN Matches.Winner IS NULL THEN 1 ELSE 0 END) AS
MatchesDrawn
FROM Players
LEFT JOIN Matches ON Players.TeamID = Matches.TeamID
GROUP BY Players.PlayerID;

-- 2. Identify the players who have scored the most runs in a particular year.
SELECT PlayerID, SUM(RunsScored) AS TotalRuns
FROM PlayerStatistics
WHERE YEAR(Date) = <year>
GROUP BY PlayerID
ORDER BY TotalRuns DESC
LIMIT 1;

-- 3. Calculate the total number of runs scored by each player and the number of wickets
taken by each player.
SELECT PlayerID, SUM(RunsScored) AS TotalRuns, SUM(WicketsTaken) AS TotalWickets
FROM PlayerStatistics
GROUP BY PlayerID;

-- 4. Determine the total number of matches played by each player and the number of
matches won by each player.
SELECT Players.PlayerID, COUNT(*) AS TotalMatchesPlayed,
SUM(CASE WHEN Matches.Winner = Players.TeamID THEN 1 ELSE 0 END) AS
MatchesWon
FROM Players
LEFT JOIN Matches ON Players.TeamID = Matches.TeamID
15

GROUP BY Players.PlayerID;

-- 5. Identify the players who have taken the most wickets in a particular season.
SELECT PlayerID, SUM(WicketsTaken) AS TotalWickets
FROM PlayerStatistics
WHERE Season = '<season>'
GROUP BY PlayerID
ORDER BY TotalWickets DESC
LIMIT 1;

-- 6. Calculate the total number of wickets taken by each player and the number of runs
conceded by each player.
SELECT PlayerID, SUM(WicketsTaken) AS TotalWickets, SUM(RunsConceded) AS
TotalRunsConceded
FROM PlayerStatistics
GROUP BY PlayerID;

-- 7. Identify the players who have scored the most runs in a particular match.
SELECT PlayerID, SUM(RunsScored) AS TotalRuns
FROM PlayerStatistics
WHERE MatchID = <match_id>
GROUP BY PlayerID
ORDER BY TotalRuns DESC
LIMIT 1;

-- 8. Determine the total number of matches played by each player and the number of
matches won by each player.
SELECT Players.PlayerID, COUNT(*) AS TotalMatchesPlayed,
SUM(CASE WHEN Matches.Winner = Players.TeamID THEN 1 ELSE 0 END) AS
MatchesWon
FROM Players
LEFT JOIN Matches ON Players.TeamID = Matches.TeamID
16

GROUP BY Players.PlayerID;

-- 9. Identify the players who have taken the most wickets in a particular season.
SELECT PlayerID, SUM(WicketsTaken) AS TotalWickets
FROM PlayerStatistics
WHERE Season = '<season>'
GROUP BY PlayerID
ORDER BY TotalWickets DESC
LIMIT 1;

-- 10. Calculate the total number of wickets taken by each player and the number of runs
conceded by each player.
SELECT PlayerID, SUM(WicketsTaken) AS TotalWickets, SUM(RunsConceded) AS
TotalRunsConceded
FROM PlayerStatistics
GROUP BY PlayerID;

-- 11. Identify the players who have scored the most runs in a particular year.
SELECT PlayerID, SUM(RunsScored) AS TotalRuns
FROM PlayerStatistics
WHERE YEAR(Date) = <year>
GROUP BY PlayerID
ORDER BY TotalRuns DESC
LIMIT 1;

-- 12. Determine the number of matches played by each player and the number of matches
lost by each player.
SELECT Players.PlayerID, COUNT(*) AS TotalMatchesPlayed,
SUM(CASE WHEN Matches.Winner != Players.TeamID THEN 1 ELSE 0 END) AS
MatchesLost
FROM Players
LEFT JOIN Matches ON Players.TeamID = Matches.TeamID
17

GROUP BY Players.PlayerID;

-- 13. Identify the players who have scored the most centuries in a particular season.
SELECT PlayerID, COUNT(*) AS Centuries
FROM PlayerStatistics
WHERE RunsScored >= 100 AND Season = '<season>'
GROUP BY PlayerID
ORDER BY Centuries DESC
LIMIT 1;

-- 14. Calculate the total number of runs scored by each team and the number of wickets
taken by each team.
SELECT TeamID, SUM(RunsScored) AS TotalRuns, SUM(WicketsTaken) AS TotalWickets
FROM PlayerStatistics
GROUP BY TeamID;

-- 15. Determine the total number of matches played by each team and the number of matches
won by each team.
SELECT Matches.TeamID, COUNT(*) AS TotalMatchesPlayed,
SUM(CASE WHEN Matches.Winner = Matches.TeamID THEN 1 ELSE 0 END) AS
MatchesWon
FROM Matches
GROUP BY Matches.TeamID;

-- 16. Identify the players who have taken the most wickets in a particular match.
SELECT PlayerID, WicketsTaken
FROM PlayerStatistics
WHERE MatchID = <match_id>
ORDER BY WicketsTaken DESC
LIMIT 1;
18

-- 17. Calculate the total number of wickets taken by each team and the number of runs
conceded by each team.
SELECT Matches.TeamID, SUM(WicketsTaken) AS TotalWickets, SUM(RunsConceded)
AS TotalRunsConceded
FROM Matches
INNER JOIN PlayerStatistics ON Matches.MatchID = PlayerStatistics.MatchID
GROUP BY Matches.TeamID;

-- 18. Identify the players who have scored the most runs in a particular match.
SELECT PlayerID, SUM(RunsScored) AS TotalRuns
FROM PlayerStatistics
WHERE MatchID = <match_id>
GROUP BY PlayerID
ORDER BY TotalRuns DESC
LIMIT 1;

-- 19. Determine the total number of matches played by each player and the number of
matches won by each player.
SELECT Players.PlayerID, COUNT(*) AS TotalMatchesPlayed,
SUM(CASE WHEN Matches.Winner = Players.TeamID THEN 1 ELSE 0 END) AS
MatchesWon
FROM Players
LEFT JOIN Matches ON Players.TeamID = Matches.TeamID
GROUP BY Players.PlayerID;

-- 20. Identify the players who have taken the most wickets in a particular season.
SELECT PlayerID, SUM(WicketsTaken) AS TotalWickets
FROM PlayerStatistics
WHERE Season = '<season>'
GROUP BY PlayerID
ORDER BY TotalWickets DESC
LIMIT 1;

You might also like