-
Notifications
You must be signed in to change notification settings - Fork 0
/
LetThereBeADatabase.sql
403 lines (338 loc) · 9.88 KB
/
LetThereBeADatabase.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
--
-- File generated with SQLiteStudio v3.2.1 on Mon Sep 11 22:49:12 2023
--
-- Text encoding used: System
--
PRAGMA foreign_keys = off;
BEGIN TRANSACTION;
-- Table: AuthorBooks
CREATE TABLE AuthorBooks (
BookId INTEGER NOT NULL
REFERENCES Books (Id),
AuthorId INTEGER NOT NULL
REFERENCES Authors (Id),
Id INTEGER PRIMARY KEY AUTOINCREMENT
NOT NULL
);
-- Table: Authors
CREATE TABLE Authors (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Name VARCHAR (100) NOT NULL
);
-- Table: Books
CREATE TABLE Books (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Title VARCHAR (100) NOT NULL,
SeriesId INTEGER REFERENCES Series (Id),
IsRead BOOL DEFAULT (false),
NumberInSeries REAL,
IsFavorite BOOLEAN NOT NULL
DEFAULT (false),
CoverId INTEGER REFERENCES Covers (Id),
Format VARCHAR (10) NOT NULL,
Signature VARCHAR (64) NOT NULL
UNIQUE,
Path VARCHAR (32767) NOT NULL,
Description VARCHAR (3000),
ISBN VARCHAR (13)
);
-- Table: Covers
CREATE TABLE Covers (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Image BLOB NOT NULL
);
-- Table: Quotes
CREATE TABLE Quotes (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Text VARCHAR,
BookId INTEGER REFERENCES Books (Id),
Note VARCHAR
);
-- Table: Series
CREATE TABLE Series (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Name VARCHAR (100) NOT NULL
);
-- Table: UserCollectionBooks
CREATE TABLE UserCollectionBooks (
BookId INTEGER NOT NULL
REFERENCES Books (Id),
UserCollectionId INTEGER NOT NULL
REFERENCES UserCollections (Id),
Id INTEGER PRIMARY KEY AUTOINCREMENT
NOT NULL
);
-- Table: UserCollections
CREATE TABLE UserCollections (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Tag VARCHAR (50) NOT NULL
UNIQUE
);
-- Index: Author_Book_BookId_Index
CREATE INDEX Author_Book_BookId_Index ON AuthorBooks (
BookId
);
-- Index: IdIndex
CREATE UNIQUE INDEX IdIndex ON Books (
Id
);
-- Index: NameIndex
CREATE INDEX NameIndex ON Authors (
Name
);
-- Index: SeriesNameIndex
CREATE INDEX SeriesNameIndex ON Series (
Name
);
-- Index: TagIndex
CREATE INDEX TagIndex ON UserCollections (
Tag
);
-- Index: TitleIndex
CREATE INDEX TitleIndex ON Books (
Title
);
-- Index: User_Collection_BookId_Index
CREATE INDEX User_Collection_BookId_Index ON UserCollectionBooks (
BookId
);
-- Trigger: BeforeBookDelete
CREATE TRIGGER BeforeBookDelete
BEFORE DELETE
ON Books
BEGIN
DELETE FROM AuthorBooks
WHERE BookId = OLD.Id;
DELETE FROM UserCollectionBooks
WHERE BookId = OLD.Id;
UPDATE Quotes
SET BookId = NULL
WHERE BookId = OLD.Id;
END;
-- Trigger: CleanAuthorsAfterBookDelete
CREATE TRIGGER CleanAuthorsAfterBookDelete
AFTER DELETE
ON AuthorBooks
WHEN NOT EXISTS (
SELECT 1
FROM AuthorBooks
WHERE AuthorId = OLD.AuthorId
)
BEGIN
DELETE FROM Authors
WHERE Id = OLD.AuthorId;
END;
-- Trigger: CleanCollectionsAfterBookDelete
CREATE TRIGGER CleanCollectionsAfterBookDelete
AFTER DELETE
ON UserCollectionBooks
WHEN NOT EXISTS (
SELECT 1
FROM UserCollectionBooks
WHERE UserCollectionId = OLD.UserCollectionId
)
BEGIN
DELETE FROM UserCollections
WHERE Id = OLD.UserCollectionId;
END;
-- Trigger: CleanSeriesAfterBookDelete
CREATE TRIGGER CleanSeriesAfterBookDelete
AFTER DELETE
ON Books
WHEN NOT EXISTS (
SELECT 1
FROM Books
WHERE SeriesId = OLD.SeriesId
)
BEGIN
DELETE FROM Series
WHERE Id = OLD.SeriesId;
END;
-- Trigger: DeleteCoverAfterBookDelete
CREATE TRIGGER DeleteCoverAfterBookDelete
AFTER DELETE
ON Books
BEGIN
DELETE FROM Covers
WHERE Id = OLD.CoverId;
END;
-- View: AuthorId_Book_View
CREATE VIEW AuthorId_Book_View AS
SELECT AuthorBooks.AuthorId,
Books.Id,
Books.Title,
Books.Description,
Books.IsFavorite,
Books.IsRead,
Books.NumberInSeries,
Books.SeriesId,
Books.Signature,
Books.Format,
Books.Path,
Books.ISBN
FROM Books
INNER JOIN
AuthorBooks ON Books.Id = AuthorBooks.BookId;
-- View: Authors_BookCount_Collections_View
CREATE VIEW Authors_BookCount_Collections_View AS
SELECT DISTINCT a.Id,
a.Name,
a.BookCount,
c.Id AS CollectionId,
c.Tag
FROM Authors_BookCount_View a
JOIN
AuthorBooks b ON a.Id = b.AuthorId
JOIN
BookId_Collection_View c ON b.BookId = c.BookId;
-- View: Authors_BookCount_View
CREATE VIEW Authors_BookCount_View AS
SELECT a.Id,
a.Name,
COUNT( * ) AS BookCount
FROM Authors a
JOIN
AuthorBooks ba ON a.Id = ba.AuthorId
GROUP BY a.Name;
-- View: Book_Author_Join
CREATE VIEW Book_Author_Join AS
SELECT AuthorId_Book_View.Id,
AuthorId_Book_View.Title,
AuthorId_Book_View.Description,
AuthorId_Book_View.SeriesId,
AuthorId_Book_View.IsFavorite,
AuthorId_Book_View.IsRead,
AuthorId_Book_View.NumberInSeries,
AuthorId_Book_View.Signature,
AuthorId_Book_View.Format,
AuthorId_Book_View.Path,
AuthorId_Book_View.ISBN,
Authors.Name AS AuthorName
FROM AuthorId_Book_View
INNER JOIN
Authors ON Authors.Id = AuthorId_Book_View.AuthorId;
-- View: Book_Series_Join
CREATE VIEW Book_Series_Join AS
SELECT Books.Id,
Books.Title,
Books.Description,
Books.SeriesId,
Books.IsFavorite,
Books.IsRead,
Books.CoverId,
Books.NumberInSeries,
Books.Signature,
Books.Format,
Books.Path,
Books.ISBN,
Series.Name AS SeriesName
FROM Books
LEFT JOIN
Series ON Series.Id = Books.SeriesId;
-- View: BookId_Author_View
CREATE VIEW BookId_Author_View AS
SELECT AuthorBooks.BookId,
Authors.Id,
Authors.Name
FROM Authors
INNER JOIN
AuthorBooks ON Authors.Id = AuthorBooks.AuthorId;
-- View: BookId_Collection_View
CREATE VIEW BookId_Collection_View AS
SELECT UserCollectionBooks.BookId,
UserCollections.Id,
UserCollections.Tag
FROM UserCollections
INNER JOIN
UserCollectionBooks ON UserCollections.Id = UserCollectionBooks.UserCollectionId;
-- View: CollectionId_Book_View
CREATE VIEW CollectionId_Book_View AS
SELECT UserCollectionBooks.UserCollectionId AS CollectionId,
Books.Id,
Books.Title,
Books.Description,
Books.IsFavorite,
Books.IsRead,
Books.NumberInSeries,
Books.SeriesId,
Books.Signature,
Books.Format,
Books.Path,
Books.ISBN
FROM Books
INNER JOIN
UserCollectionBooks ON Books.Id = UserCollectionBooks.BookId;
-- View: Collections_BookCount_View
CREATE VIEW Collections_BookCount_View AS
SELECT c.Id,
c.Tag,
COUNT( * ) AS BookCount
FROM UserCollections c
JOIN
UserCollectionBooks cb ON c.Id = cb.UserCollectionId
GROUP BY c.Tag;
-- View: Full_Join
CREATE VIEW Full_Join AS
SELECT X.Id,
X.Title,
X.Description,
X.SeriesId,
X.IsFavorite,
X.IsRead,
X.NumberInSeries,
X.SeriesName,
X.AuthorName,
X.AuthorId,
X.CoverId,
X.Signature,
X.Format,
X.Path,
X.ISBN,
Tag,
BookId_Collection_View.Id AS CollectionId
FROM (
SELECT BookId AS Id,
BookId_Author_View.Id AS AuthorId,
Title,
Description,
SeriesId,
CoverId,
IsFavorite,
IsRead,
NumberInSeries,
SeriesName,
Name AS AuthorName,
Signature,
Format,
Path,
ISBN
FROM Book_Series_Join
INNER JOIN
BookId_Author_View ON Book_Series_Join.Id = BookId_Author_View.BookId
)
AS X
LEFT JOIN
BookId_Collection_View ON BookId_Collection_View.BookId = X.Id;
-- View: Series_BookCount_Collections_View
CREATE VIEW Series_BookCount_Collections_View AS
SELECT DISTINCT a.Id,
a.Name,
a.BookCount,
c.Id AS CollectionId,
c.Tag
FROM Series_BookCount_View a
JOIN
Books b ON a.Id = b.SeriesId
JOIN
BookId_Collection_View c ON b.Id = c.BookId;
-- View: Series_BookCount_View
CREATE VIEW Series_BookCount_View AS
SELECT c.Id,
c.Name,
COUNT( * ) AS BookCount
FROM Series c
JOIN
Books b ON c.Id = b.SeriesId
GROUP BY c.Name;
COMMIT TRANSACTION;
PRAGMA foreign_keys = on;