-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab5.sql
110 lines (96 loc) · 2.88 KB
/
lab5.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
-- Active: 1713084570414@@127.0.0.1@5432@hdu
CREATE TABLE IF NOT EXISTS Publishers (
PublisherID SERIAL PRIMARY KEY,
PublisherName TEXT CHECK(length(PublisherName) <= 200) NOT NULL
);
CREATE TABLE IF NOT EXISTS Themes (
ThemeID SERIAL PRIMARY KEY,
ThemeDescription TEXT CHECK(length(ThemeDescription) <= 100) NOT NULL
);
CREATE TABLE IF NOT EXISTS Categories (
CategoryID SERIAL PRIMARY KEY,
CategoryDescription TEXT CHECK(length(CategoryDescription) <= 100) NOT NULL
);
CREATE TABLE IF NOT EXISTS Sizes (
SizeID SERIAL PRIMARY KEY,
SizeDescriptions TEXT CHECK(length(SizeDescriptions) <= 100) NOT NULL
);
CREATE TABLE IF NOT EXISTS Books (
BookID SERIAL PRIMARY KEY,
Code INTEGER,
Novelty BOOLEAN DEFAULT FALSE,
Title TEXT CHECK(length(Title) <= 200) NOT NULL,
Price_Currency DECIMAL(10, 2),
Pages INTEGER NOT NULL,
EventDate DATE,
Circulation INTEGER DEFAULT 5000,
ThemeID INTEGER,
CategoryID INTEGER,
PublisherID INTEGER,
SizeID INTEGER,
FOREIGN KEY (ThemeID) REFERENCES Themes(ThemeID),
FOREIGN KEY (CategoryID) REFERENCES Categories(CategoryID),
FOREIGN KEY (PublisherID) REFERENCES Publishers(PublisherID),
FOREIGN KEY (SizeID) REFERENCES Sizes(SizeID)
);
-- Міграція даних
INSERT INTO Publishers (PublisherName)
SELECT DISTINCT publisher FROM Lab1
WHERE publisher IS NOT NULL;
INSERT INTO Themes (ThemeDescription)
SELECT DISTINCT theme FROM Lab1
WHERE theme IS NOT NULL;
INSERT INTO Categories (CategoryDescription)
SELECT DISTINCT category FROM Lab1
WHERE category IS NOT NULL;
INSERT INTO Sizes (SizeDescriptions)
SELECT DISTINCT size FROM Lab1
WHERE size IS NOT NULL;
INSERT INTO Books (Code, Novelty, Title, Price_Currency, Pages, EventDate, Circulation, ThemeID, CategoryID, PublisherID, SizeID)
SELECT
Lab1.code,
Lab1.novelty,
Lab1.title,
Lab1.price_currency,
Lab1.pages,
Lab1.eventDate,
Lab1.circulation,
Themes.ThemeID,
Categories.CategoryID,
Publishers.PublisherID,
Sizes.SizeID
FROM
Lab1
LEFT JOIN
Publishers ON Lab1.publisher = Publishers.PublisherName
LEFT JOIN
Themes ON Lab1.theme = Themes.ThemeDescription
LEFT JOIN
Categories ON Lab1.category = Categories.CategoryDescription
LEFT JOIN
Sizes ON Lab1.size = Sizes.SizeDescriptions;
-- Створення універсального представлення
CREATE VIEW UniversalView AS
SELECT
b.BookID,
b.Title,
b.Pages,
b.SizeId,
b.EventDate,
b.Circulation,
b.Price_Currency,
p.PublisherName,
t.ThemeDescription,
c.CategoryDescription,
s.SizeDescriptions,
b.Novelty
FROM
Books b
LEFT JOIN
Publishers p ON b.PublisherID = p.PublisherID
LEFT JOIN
Themes t ON b.ThemeID = t.ThemeID
LEFT JOIN
Categories c ON b.CategoryID = c.CategoryID
LEFT JOIN
Sizes s ON b.SizeID = s.SizeID;