-
Notifications
You must be signed in to change notification settings - Fork 0
/
eav.sql
71 lines (64 loc) · 2.53 KB
/
eav.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
-- # EAV Style Architecture
-- A flexible data storage method using the Entity-Attribute-Value model.
-- It has some performance trade-offs due to less normalized data and more joins.
-- Indexes are implemented for performance optimization.
-- ## Templates Table
-- Defines the data structure with a title and
-- optional short description and detailed content.
CREATE TABLE "Templates" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT,
"Title" TEXT,
"Description" TEXT CHECK (LENGTH("Description") <= 280),
"Content" TEXT CHECK (LENGTH("Content") <= 10000)
);
-- ## Entities Table
-- Stores records, each linked to a template with a unique ID.
CREATE TABLE "Entities" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT,
"TemplateID" INTEGER,
FOREIGN KEY ("TemplateID") REFERENCES "Templates"("ID")
);
-- ## Attributes Table
-- Associates data types with each template, including title,
-- type, and optional short description and detailed content.
CREATE TABLE "Attributes" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT,
"TemplateID" INTEGER,
"Title" TEXT,
"Type" TEXT CHECK ("Type" IN ('integer', 'text', 'blob')),
"Required" INTEGER CHECK ("Required" IN (0, 1)),1).
"Description" TEXT CHECK (LENGTH("Description") <= 280),
"Content" TEXT CHECK (LENGTH("Content") <= 10000),
FOREIGN KEY ("TemplateID") REFERENCES "Templates"("ID")
);
-- ## Values Tables (Integer, Text, Blob)
-- Stores actual data, linking values to entities and attributes.
CREATE TABLE "IntegerValues" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT,
"EntityID" INTEGER,
"AttributeID" INTEGER,
"Value" INTEGER,
FOREIGN KEY ("EntityID") REFERENCES "Entities"("ID"),
FOREIGN KEY ("AttributeID") REFERENCES "Attributes"("ID")
);
CREATE TABLE "TextValues" (
"ID" INTEGER PRIMARY KEY AUTOINCREMENT,
"EntityID" INTEGER,
"AttributeID" INTEGER,
"Value" TEXT,
FOREIGN KEY ("EntityID") REFERENCES "Entities"("ID"),
FOREIGN KEY ("AttributeID") REFERENCES "Attributes"("ID")
);
CREATE TABLE "BlobValues" (
"EntityID" INTEGER,
"AttributeID" INTEGER,
"Value" BLOB,
PRIMARY KEY ("EntityID", "AttributeID"),
FOREIGN KEY ("EntityID") REFERENCES "Entities"("ID"),
FOREIGN KEY ("AttributeID") REFERENCES "Attributes"("ID")
);
-- ## Indexes
-- Enhances query performance; optional but recommended.
CREATE INDEX "idx_integer_values" ON "IntegerValues" ("EntityID", "AttributeID");
CREATE INDEX "idx_text_values" ON "TextValues" ("EntityID", "AttributeID");
CREATE INDEX "idx_blob_values" ON "BlobValues" ("EntityID", "AttributeID");