-
Notifications
You must be signed in to change notification settings - Fork 0
/
CharityDatabaseSystems.sql
143 lines (130 loc) · 3.73 KB
/
CharityDatabaseSystems.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
USE [master];
GO
-- Create CharityDB Database
CREATE DATABASE [CharityDB]
CONTAINMENT = NONE
ON PRIMARY
(
NAME = N'CharityDB',
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.SQLEXPRESS03\MSSQL\DATA\CharityDB.mdf',
SIZE = 8192KB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 65536KB
)
LOG ON
(
NAME = N'CharityDB_log',
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.SQLEXPRESS03\MSSQL\DATA\CharityDB_log.ldf',
SIZE = 8192KB,
MAXSIZE = 2048GB,
FILEGROWTH = 65536KB
)
WITH CATALOG_COLLATION = DATABASE_DEFAULT;
GO
-- Set Compatibility Level
ALTER DATABASE [CharityDB] SET COMPATIBILITY_LEVEL = 150;
GO
-- Enable Full-Text Search if Installed
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
BEGIN
EXEC [CharityDB].[dbo].[sp_fulltext_database] @action = 'enable';
END
GO
-- Configure Database Settings
ALTER DATABASE [CharityDB] SET AUTO_CLOSE OFF;
ALTER DATABASE [CharityDB] SET AUTO_SHRINK OFF;
ALTER DATABASE [CharityDB] SET RECOVERY SIMPLE;
ALTER DATABASE [CharityDB] SET MULTI_USER;
ALTER DATABASE [CharityDB] SET PAGE_VERIFY CHECKSUM;
ALTER DATABASE [CharityDB] SET TARGET_RECOVERY_TIME = 60 SECONDS;
ALTER DATABASE [CharityDB] SET AUTO_UPDATE_STATISTICS ON;
ALTER DATABASE [CharityDB] SET QUERY_STORE = OFF;
GO
USE [CharityDB];
GO
-- Create Projects Table
CREATE TABLE [dbo].[Projects] (
[ProjectID] INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[Title] NVARCHAR(100) NOT NULL,
[Description] NVARCHAR(MAX) NULL,
[StartDate] DATE NULL,
[EndDate] DATE NULL
);
GO
-- Create Donations Table
CREATE TABLE [dbo].[Donations] (
[DonationID] INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[DonorID] INT NOT NULL,
[ProjectID] INT NOT NULL,
[Amount] DECIMAL(10, 2) NOT NULL,
[DonationDate] DATE NOT NULL
);
GO
-- Create Volunteers Table
CREATE TABLE [dbo].[Volunteers] (
[VolunteerID] INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[FirstName] NVARCHAR(50) NOT NULL,
[LastName] NVARCHAR(50) NOT NULL,
[Email] NVARCHAR(100) NOT NULL,
[PhoneNumber] NVARCHAR(15) NULL,
[JoinedDate] DATE NOT NULL
);
GO
-- Add Foreign Key Constraints
ALTER TABLE [dbo].[Donations]
ADD CONSTRAINT FK_Donations_Donors FOREIGN KEY ([DonorID])
REFERENCES [dbo].[Donors] ([DonorID]) ON DELETE CASCADE;
ALTER TABLE [dbo].[Donations]
ADD CONSTRAINT FK_Donations_Projects FOREIGN KEY ([ProjectID])
REFERENCES [dbo].[Projects] ([ProjectID]) ON DELETE CASCADE;
GO
-- Create Donation Summary View
CREATE VIEW [dbo].[DonationSummaryByProject] AS
SELECT
p.ProjectID,
p.Title AS ProjectTitle,
COUNT(d.DonationID) AS TotalDonations,
SUM(d.Amount) AS TotalAmountDonated
FROM
[dbo].[Projects] p
LEFT JOIN
[dbo].[Donations] d ON p.ProjectID = d.ProjectID
GROUP BY
p.ProjectID, p.Title;
GO
-- Create Stored Procedure to Insert Donation
CREATE PROCEDURE [dbo].[InsertDonation]
@DonorID INT,
@ProjectID INT,
@Amount DECIMAL(10, 2),
@DonationDate DATE
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
INSERT INTO [dbo].[Donations] ([DonorID], [ProjectID], [Amount], [DonationDate])
VALUES (@DonorID, @ProjectID, @Amount, @DonationDate);
END TRY
BEGIN CATCH
THROW;
END CATCH;
END;
GO
-- Create Stored Procedure to Get Donation Summary
CREATE PROCEDURE [dbo].[GetDonationSummary]
AS
BEGIN
SET NOCOUNT ON;
SELECT
p.ProjectID,
p.Title AS ProjectTitle,
COUNT(d.DonationID) AS TotalDonations,
SUM(d.Amount) AS TotalAmountDonated
FROM
[dbo].[Projects] p
LEFT JOIN
[dbo].[Donations] d ON p.ProjectID = d.ProjectID
GROUP BY
p.ProjectID, p.Title;
END;
GO