-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtiDinHisab_DDL.sql
More file actions
163 lines (139 loc) · 4.24 KB
/
ProtiDinHisab_DDL.sql
File metadata and controls
163 lines (139 loc) · 4.24 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
-- DDL: Database Structure for Darul Uloom Madrasah
----- Creating Database--
-- =============================================
-- 31. Create ProtiDinHisab: Define database with growth and file size control for inventory
-- =============================================
CREATE DATABASE ProtiDinHisab
ON PRIMARY
(
NAME = ProtiDinHisab_Data,
FILENAME = 'D:\\ProtiDinHisab.mdf',
SIZE = 10MB,
MAXSIZE = 100MB,
FILEGROWTH = 5%
)
LOG ON
(
NAME = ProtiDinHisab_Log,
FILENAME = 'D:\\ProtiDinHisab_log.ldf',
SIZE = 5MB,
MAXSIZE = 50MB,
FILEGROWTH = 5MB
);
GO
-----Creating Tables------
use ProtiDinHisab;
CREATE TABLE Students (
student_id INT PRIMARY KEY IDENTITY(1,1),
full_name VARCHAR(100) NOT NULL,
class_level VARCHAR(50),
guardian_name VARCHAR(100),
phone_number VARCHAR(20),
is_residential BIT,
enrollment_date DATE
);
CREATE TABLE StudentDues (
due_id INT PRIMARY KEY IDENTITY(1001,1),
student_id INT,
billing_month DATE,
amount_due DECIMAL(10, 2),
due_date DATE,
amount_paid DECIMAL(10, 2) DEFAULT 0,
payment_status AS
CASE
WHEN amount_paid >= amount_due THEN 'Paid'
WHEN amount_paid > 0 THEN 'Partially Paid'
ELSE 'Unpaid'
END,
FOREIGN KEY (student_id) REFERENCES Students(student_id)
);
CREATE TABLE FeePayments (
payment_id INT PRIMARY KEY IDENTITY(2001,1),
due_id INT,
payment_date DATE,
amount_paid DECIMAL(10, 2),
payment_method VARCHAR(50),
transaction_ref VARCHAR(100),
FOREIGN KEY (due_id) REFERENCES StudentDues(due_id)
);
CREATE TABLE Donors (
donor_id INT PRIMARY KEY IDENTITY(3001,1),
full_name VARCHAR(100),
phone_number VARCHAR(20),
email VARCHAR(100),
[address] VARCHAR(200)
);
CREATE TABLE Donations (
donation_id INT PRIMARY KEY IDENTITY(5001,1),
donor_id INT,
amount DECIMAL(10, 2),
donation_type VARCHAR(20) CHECK (donation_type IN ('Zakat', 'Sadaqa', 'Lillah', 'Other')),
purpose_tag VARCHAR(100),
donation_date DATE,
transaction_ref VARCHAR(100),
FOREIGN KEY (donor_id) REFERENCES Donors(donor_id)
);
CREATE TABLE Expenses (
expense_id INT PRIMARY KEY IDENTITY(6001,1),
expense_name VARCHAR(100),
category VARCHAR(50),
amount_spent DECIMAL(10, 2),
funded_by VARCHAR(20) CHECK(funded_by IN ('Donation', 'Fee', 'Other')),
source_id INT,
expense_date DATE,
approved_by VARCHAR(100),
remarks VARCHAR(200)
);
CREATE TABLE Staff (
staff_id INT PRIMARY KEY IDENTITY(7001,1),
full_name VARCHAR(100),
position VARCHAR(50),
salary DECIMAL(10, 2),
bank_account VARCHAR(50),
join_date DATE,
payment_status VARCHAR(20) CHECK (payment_status IN ('Active', 'Inactive', 'Resigned'))
);
-- =============================================
-- SOLUTIONS TO CASE STUDY PROBLEMS USING SQL
-- =============================================
-- 3. Real-Time Report Generation
-- View: Monthly donation summary
CREATE VIEW vw_MonthlyDonationSummary AS
SELECT CONVERT(CHAR(7), donation_date, 120) AS Month,
SUM(amount) AS TotalAmount
FROM Donations
GROUP BY CONVERT(CHAR(7), donation_date, 120);
GO
-- =============================================
-- ALTER TABLE
-- =============================================
ALTER TABLE Donors
ADD national_id VARCHAR(20);
-- =============================================
-- DROP COMUMN
-- =============================================
ALTER TABLE Donors
DROP COLUMN national_id;
-- =============================================
-- VIEWS
-- =============================================
-- View: Students with Dues Summary
CREATE VIEW vw_StudentDueStatus AS
SELECT s.full_name, s.class_level, s.phone_number,
d.billing_month, d.amount_due, d.amount_paid, d.payment_status
FROM Students s
JOIN StudentDues d ON s.student_id = d.student_id;
GO
-- View: Donation Summary by Type
CREATE VIEW vw_DonationSummary AS
SELECT donation_type, COUNT(*) AS donation_count, SUM(amount) AS total_amount
FROM Donations
GROUP BY donation_type;
GO
-- =============================================
-- INDEXES
-- =============================================
-- Index: Speed up lookups on Students by phone number
CREATE NONCLUSTERED INDEX idx_Students_PhoneNumber
ON Students (phone_number);
GO