-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrontEd.sql
195 lines (194 loc) · 7.31 KB
/
FrontEd.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/****** Object: Table [dbo].[School] Script Date: 16.3.2021. 23:50:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[School](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](200) NOT NULL,
[TownId] [int] NOT NULL,
CONSTRAINT [PK_School] PRIMARY KEY CLUSTERED
(
[Id] 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].[Town] Script Date: 16.3.2021. 23:50:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Town](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Postcode] [int] NOT NULL,
[Name] [nvarchar](100) NOT NULL,
CONSTRAINT [PK_Town] PRIMARY KEY CLUSTERED
(
[Id] 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].[Workshop] Script Date: 16.3.2021. 23:50:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Workshop](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Title] [nvarchar](100) NOT NULL,
[Description] [nvarchar](max) NULL,
[Time] [datetime2](7) NOT NULL,
[SchoolId] [int] NOT NULL,
[Capacity] [int] NOT NULL,
CONSTRAINT [PK_Workshop] PRIMARY KEY CLUSTERED
(
[Id] 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] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object: Table [dbo].[WorkshopParticipant] Script Date: 16.3.2021. 23:50:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[WorkshopParticipant](
[Id] [int] IDENTITY(1,1) NOT NULL,
[ParticipantId] [int] NOT NULL,
[WorkshopId] [int] NOT NULL,
[ApplicationTime] [datetime2](7) NOT NULL,
CONSTRAINT [PK_WorkshopParticipant] PRIMARY KEY CLUSTERED
(
[Id] 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: View [dbo].[view_Workshops] Script Date: 16.3.2021. 23:50:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[view_Workshops]
AS
SELECT *, ISNULL((SELECT MAX(FreePlaces) FROM (VALUES (Capacity - NoOfParticipants), (0)) AS T(FreePlaces)), 0) FreePlaces FROM
(
SELECT W.Id, W.Title, W.Description, W.Time, W.SchoolId,
(S.Name + ' ' + T.Name) AS School,
W.Capacity,
ISNULL( (SELECT COUNT(*) FROM WorkshopParticipant WHERE WorkshopId = W.Id), 0) AS NoOfParticipants
FROM Workshop W
INNER JOIN School S ON S.Id = W.SchoolId
INNER JOIN Town T ON S.TownId = T.Id
) V
GO
/****** Object: Table [dbo].[RefreshToken] Script Date: 16.3.2021. 23:50:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[RefreshToken](
[RefreshTokenId] [int] IDENTITY(1,1) NOT NULL,
[UserId] [int] NOT NULL,
[Token] [nvarchar](256) NOT NULL,
[Expires] [datetime2](7) NOT NULL,
CONSTRAINT [PK_RefreshToken] PRIMARY KEY CLUSTERED
(
[RefreshTokenId] 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].[Student] Script Date: 16.3.2021. 23:50:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Student](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[Surname] [nvarchar](100) NOT NULL,
[Email] [nvarchar](100) NOT NULL,
[SchoolId] [int] NOT NULL,
CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED
(
[Id] 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].[User] Script Date: 16.3.2021. 23:50:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[User](
[UserId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](200) NOT NULL,
[FirstName] [nvarchar](200) NULL,
[LastName] [nvarchar](200) NULL,
[Password] [nvarchar](200) NULL,
CONSTRAINT [PK_User] 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
SET ANSI_PADDING ON
GO
/****** Object: Index [IX_School] Script Date: 16.3.2021. 23:50:20 ******/
CREATE UNIQUE NONCLUSTERED INDEX [IX_School] ON [dbo].[School]
(
[Name] ASC,
[TownId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object: Index [UIX_Student_Email] Script Date: 16.3.2021. 23:50:20 ******/
CREATE UNIQUE NONCLUSTERED INDEX [UIX_Student_Email] ON [dbo].[Student]
(
[Email] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
/****** Object: Index [IX_WorkshopParticipant] Script Date: 16.3.2021. 23:50:20 ******/
CREATE UNIQUE NONCLUSTERED INDEX [IX_WorkshopParticipant] ON [dbo].[WorkshopParticipant]
(
[ParticipantId] ASC,
[WorkshopId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
ALTER TABLE [dbo].[RefreshToken] WITH CHECK ADD CONSTRAINT [FK_RefreshToken_User] FOREIGN KEY([UserId])
REFERENCES [dbo].[User] ([UserId])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[RefreshToken] CHECK CONSTRAINT [FK_RefreshToken_User]
GO
ALTER TABLE [dbo].[School] WITH CHECK ADD CONSTRAINT [FK_School_Town] FOREIGN KEY([TownId])
REFERENCES [dbo].[Town] ([Id])
GO
ALTER TABLE [dbo].[School] CHECK CONSTRAINT [FK_School_Town]
GO
ALTER TABLE [dbo].[Student] WITH CHECK ADD CONSTRAINT [FK_Student_School] FOREIGN KEY([SchoolId])
REFERENCES [dbo].[School] ([Id])
GO
ALTER TABLE [dbo].[Student] CHECK CONSTRAINT [FK_Student_School]
GO
ALTER TABLE [dbo].[Workshop] WITH CHECK ADD CONSTRAINT [FK_Workshop_School] FOREIGN KEY([SchoolId])
REFERENCES [dbo].[School] ([Id])
GO
ALTER TABLE [dbo].[Workshop] CHECK CONSTRAINT [FK_Workshop_School]
GO
ALTER TABLE [dbo].[WorkshopParticipant] WITH CHECK ADD CONSTRAINT [FK_WorkshopParticipant_Student] FOREIGN KEY([ParticipantId])
REFERENCES [dbo].[Student] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[WorkshopParticipant] CHECK CONSTRAINT [FK_WorkshopParticipant_Student]
GO
ALTER TABLE [dbo].[WorkshopParticipant] WITH CHECK ADD CONSTRAINT [FK_WorkshopParticipant_Workshop] FOREIGN KEY([WorkshopId])
REFERENCES [dbo].[Workshop] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[WorkshopParticipant] CHECK CONSTRAINT [FK_WorkshopParticipant_Workshop]
GO
INSERT INTO [dbo].[User]
([Name], [FirstName], [LastName], [Password])
VALUES
('rade', 'Rade', 'Adminić', 'AQAAAAEAACcQAAAAEAKp4nJM5HSQYwIcTe2ZaxUela7xJNEAEhni00xr/BgGQNtxKMC8qIc2zeQeC6cghg==') --admin
GO