--
-- URLs
--
CREATE TABLE urls (
id INTEGER PRIMARY KEY,
url TEXT NOT NULL
);
--
-- Tags
--
CREATE TABLE tags (
id INTEGER PRIMARY KEY,
tag TEXT NOT NULL
);
--
-- UrlTags
--
CREATE TABLE UrlTags (
id INTEGER PRIMARY KEY,
urls_id INTEGER,
tags_id INTEGER,
FOREIGN KEY(urls_id) REFERENCES url(id),
FOREIGN KEY(tags_id) REFERENCES tags(id)
);
--
-- URLS sample data
--
INSERT INTO urls VALUES (null, 'http://www.google.com/');
INSERT INTO urls VALUES (null, 'http://maps.google.com/');
--
-- Tags sample data
--
INSERT INTO tags VALUES (null, 'google');
INSERT INTO tags VALUES (null, 'map');
INSERT INTO tags VALUES (null, 'search');
--
-- URLtags sample data
-- Let's say http://www.google.com/ has all 3 tags
INSERT INTO urltags VALUES (null, 1, 1);
INSERT INTO urltags VALUES (null, 1, 2);
INSERT INTO urltags VALUES (null, 1, 3);
-- http://maps.google.com/ has only the 'map' tag
INSERT INTO urltags VALUES (null, 2, 2);
--- The following retrieves all urls associated with the 'search' tag
--- which I assume will be only http://www.google.com/
SELECT url
FROM urls u INNER JOIN urltags r ON u.id = r.urls_id
INNER JOIN tags t ON t.id = r.tags_id
WHERE t.tag = 'search';