-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.txt
124 lines (102 loc) · 3.64 KB
/
help.txt
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
-- Node: {
-- _id: 0,
-- dbid: 0,
-- title: "",
-- body: "",
-- tags: [],
-- version: 1,
-- created: "",
-- updated: ""
-- }
CREATE TABLE auto_increment (
dbid INT,
count INT
);
CREATE TABLE notes (
_id INTEGER,
dbid INTEGER NOT NULL,
title TEXT,
body TEXT,
PRIMARY KEY (_id, dbid)
);
INSERT INTO auto_increment VALUES (0, 0);
CREATE TABLE tags(
_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE NOT NULL
);
CREATE TABLE tag_notes(
note_id INTEGER,
dbid INTEGER,
tag_id INTEGER,
FOREIGN KEY(note_id) REFERENCES notes(_id),
FOREIGN KEY(dbid) REFERENCES notes(dbid),
FOREIGN KEY(tag_id) REFERENCES tags(_id)
);
INSERT INTO notes(dbid, title, body) VALUES(0, 'Note 1', "Note 1 description");
INSERT INTO notes(dbid, title, body) VALUES(0, 'Note 2', "Note 2 description");
INSERT INTO notes(dbid, title, body) VALUES(0, 'Note 3', "Note 3 description");
INSERT INTO notes(dbid, title, body) VALUES(0, 'Note 4', "Note 4 description");
INSERT INTO notes(dbid, title, body) VALUES(0, 'Note 5', "Note 5 description");
INSERT INTO notes(dbid, title, body) VALUES(0, 'Note 6', "Note 6 description");
INSERT INTO notes(dbid, title, body) VALUES(0, 'Note 7', "Note 7 description");
SELECT * FROM notes;
INSERT INTO tags(name) VALUES ('tag');
INSERT INTO tags(name) VALUES ('inbox');
INSERT INTO tags(name) VALUES ('notes');
INSERT INTO tags(name) VALUES ('project');
INSERT INTO tags(name) VALUES ('android');
INSERT INTO tags(name) VALUES ('study');
INSERT INTO tags(name) VALUES ('do');
INSERT INTO tags(name) VALUES ('done');
SELECT * FROM tags;
INSERT INTO tag_notes(note_id, dbid, tag_id) VALUES(1, 0, 1);
INSERT INTO tag_notes(note_id, dbid, tag_id) VALUES(1, 0, 2);
INSERT INTO tag_notes(note_id, dbid, tag_id) VALUES(1, 0, 3);
INSERT INTO tag_notes(note_id, dbid, tag_id) VALUES(2, 0, 3);
SELECT * FROM tag_notes;
-- List all tags
SELECT * FROM tags;
-- List all tags that exists in notes
SELECT tags.*, notes.*
FROM tag_notes
INNER JOIN tags ON tag_notes.tag_id = tags._id
INNER JOIN notes ON tag_notes.note_id = notes._id AND tag_notes.dbid = notes.dbid;
SELECT tags.*, notes.*
FROM tag_notes
INNER JOIN tags ON tag_notes.tag_id = tags._id
INNER JOIN notes ON tag_notes.note_id = notes._id AND tag_notes.dbid = notes.dbid;
-- List tags of a note
SELECT tags.*
FROM tag_notes
INNER JOIN tags ON tag_notes.tag_id = tags._id
INNER JOIN notes ON tag_notes.note_id = notes._id AND tag_notes.dbid = notes.dbid
WHERE notes._id IN (2);
--
CREATE VIEW tagsByNotes AS
SELECT tags.*
FROM tag_notes
INNER JOIN tags ON tag_notes.tag_id = tags._id
INNER JOIN notes ON tag_notes.note_id = notes._id AND tag_notes.dbid = notes.dbid;
SELECT * FROM tagsByNotes;
-- List tags with associated notes
SELECT tags.*, notes.*
FROM tag_notes
INNER JOIN tags ON tag_notes.tag_id = tags._id
INNER JOIN notes ON tag_notes.note_id = notes._id AND tag_notes.dbid = notes.dbid;
-- List Notes by tag name
SELECT tags.*, notes.*
FROM tag_notes
INNER JOIN tags ON tag_notes.tag_id = tags._id
INNER JOIN notes ON tag_notes.note_id = notes._id AND tag_notes.dbid = notes.dbid
WHERE tags.name NOT IN ("inbox", "tag");
-- Only list notes filting by tag name
SELECT DISTINCT notes.*
FROM tag_notes
INNER JOIN tags ON tag_notes.tag_id = tags._id
INNER JOIN notes ON tag_notes.note_id = notes._id AND tag_notes.dbid = notes.dbid
WHERE tags.name IN ("notes");
-- list tags per all notes
SELECT DISTINCT tags.*
FROM tag_notes
INNER JOIN tags ON tag_notes.tag_id = tags._id
INNER JOIN notes ON tag_notes.note_id = notes._id AND tag_notes.dbid = notes.dbid;