sql 演示数据库和查询简单的待办事项/购物清单应用程序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 演示数据库和查询简单的待办事项/购物清单应用程序相关的知识,希望对你有一定的参考价值。
DROP TABLE IF EXISTS items_tags;
DROP TABLE IF EXISTS tags;
DROP TABLE IF EXISTS items;
DROP TABLE IF EXISTS users;
CREATE TABLE users (
id serial PRIMARY KEY,
email text NOT NULL,
username text NOT NULL
);
CREATE TABLE items (
id serial PRIMARY KEY,
name text NOT NULL,
description text,
completed boolean DEFAULT false,
created timestamp DEFAULT now(),
user_id integer REFERENCES users(id) ON DELETE RESTRICT
);
CREATE TABLE tags (
id serial PRIMARY KEY,
tag text NOT NULL
);
CREATE TABLE items_tags (
item_id integer REFERENCES items(id) ON DELETE CASCADE,
tag_id integer REFERENCES tags(id) ON DELETE RESTRICT,
PRIMARY KEY (item_id, tag_id)
);
INSERT INTO users
(id, username, email)
VALUES
(31, 'Foo', 'foo@example.com'),
(42, 'Bar', 'bar@example.com'),
(53, 'Qux', 'qux@example.com'),
(64, 'Baz', 'baz@example.com');
INSERT INTO items
(id, name, description, user_id)
VALUES
(1001, 'Avocados', 'avacado mash', 31),
(1002, 'Orange juice', 'not just for breakfast', 42),
(1003, 'Ice Cream', 'Ben & Jerry''s', 53),
(1004, 'Oil Change', 'stop at dealership', 31),
(1005, 'Cat Food', null, 42),
(1006, 'Sun Glasses', 'for vacation', 53),
(1007, 'Dark roast', 'nectar of the gods', 31),
(1008, 'Running Shoes', null, 42),
(1009, 'Espresso', 'nectar of the gods', 53),
(1010, 'Merlot', 'for dinner', null);
INSERT INTO tags
(id, tag)
VALUES
(171, 'Grocery'),
(172, 'Coffee'),
(173, 'Pet');
INSERT INTO items_tags (item_id, tag_id) VALUES
(1001, 171),
(1002, 171),
(1003, 171),
(1005, 171), (1005, 173),
(1006, 171),
(1007, 171), (1007, 172),
(1009, 171), (1009, 172);
SELECT users.id, email, username, items.id,
name, description, completed, user_id
FROM users JOIN items
ON users.id = items.user_id;
SELECT users.id, email, username, items.id,
name, description, completed, user_id
FROM users LEFT JOIN items
ON users.id = items.user_id;
SELECT users.id, email, username, items.id,
name, description, completed, user_id
FROM users RIGHT JOIN items
ON users.id = items.user_id;
SELECT users.id, email, username, items.id,
name, completed, user_id, tag
FROM users
JOIN items ON users.id = items.user_id
JOIN items_tags ON items.id = items_tags.item_id
JOIN tags ON items_tags.tag_id = tags.id;
以上是关于sql 演示数据库和查询简单的待办事项/购物清单应用程序的主要内容,如果未能解决你的问题,请参考以下文章
text 仪式网站2018年待办事项清单
jQuery模仿ToDoList实现简单的待办事项列表
使用单元测试测试简单的首页
8月13日iOSiPadOSmacOS限免及免费应用推荐
当我将 OnResume 函数用于带有数据库的简单待办事项列表应用程序时,OnCreate 函数被删除
通量存储中的异步数据加载