From d2ed176e4843c06ccb4d51d6387cc8fc794f9a04 Mon Sep 17 00:00:00 2001 From: Mars Liu Date: Sun, 14 Nov 2021 23:15:11 +0800 Subject: [PATCH] add six exercises --- .../1.\350\241\250/config.json" | 6 +- .../1.\350\241\250/create_table.json" | 6 ++ .../1.\350\241\250/create_table.md" | 46 +++++++++++++ .../1.\350\241\250/serial.json" | 6 ++ .../1.\350\241\250/serial.md" | 22 +++++++ .../1.\350\241\250/table.json" | 6 ++ .../1.\350\241\250/table.md" | 26 ++++++++ .../2.\345\207\275\346\225\260/config.json" | 2 +- .../2.\345\207\275\346\225\260/function.json" | 6 ++ .../2.\345\207\275\346\225\260/function.md" | 29 +++++++++ .../5.CTE/to_root.json" | 6 ++ .../5.CTE/to_root.md" | 65 +++++++++++++++++++ 12 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 "data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/create_table.json" create mode 100644 "data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/create_table.md" create mode 100644 "data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/serial.json" create mode 100644 "data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/serial.md" create mode 100644 "data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/table.json" create mode 100644 "data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/table.md" create mode 100644 "data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/2.\345\207\275\346\225\260/function.json" create mode 100644 "data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/2.\345\207\275\346\225\260/function.md" create mode 100644 "data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/5.CTE/to_root.json" create mode 100644 "data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/5.CTE/to_root.md" diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/config.json" "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/config.json" index 1093ea2..42f84d1 100644 --- "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/config.json" +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/config.json" @@ -2,5 +2,9 @@ "node_id": "pg-806d3976e8764d4f8e26c197c53d45a8", "keywords": [], "children": [], - "export": [] + "export": [ + "table.json", + "create_table.json", + "serial.json" + ] } \ No newline at end of file diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/create_table.json" "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/create_table.json" new file mode 100644 index 0000000..eeacdfe --- /dev/null +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/create_table.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "create_table.md", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/create_table.md" "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/create_table.md" new file mode 100644 index 0000000..570e2c5 --- /dev/null +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/create_table.md" @@ -0,0 +1,46 @@ +# 建表语句 + +现在我们尝试建立一个简化的交易流水表 trade,需要一个自增主键,一个content字段保存订单详情,, +需要有一个时间戳字段记录订单入库时间,那么哪一个语句是对的? + +## 答案 + +```postgresql +create table trade ( + id serial primary key, + content text, + created_at timestamp default now() +); +``` + +## 选项 + +### 主键没有设置自增,不符合题意 + +```postgresql +create table trade ( + id integer primary key, + content text, + created_at timestamp default now() +); +``` + +### 时间戳没有设置默认值 + +```postgresql +create table trade ( + id serial primary key, + content text, + created_at timestamp +); +``` + +### 没有主键,不符合题设 + +```postgresql +create table trade ( + id serial, + content text, + created_at timestamp default now() +); +``` \ No newline at end of file diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/serial.json" "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/serial.json" new file mode 100644 index 0000000..28fcf77 --- /dev/null +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/serial.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "serial.md", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/serial.md" "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/serial.md" new file mode 100644 index 0000000..63034dd --- /dev/null +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/serial.md" @@ -0,0 +1,22 @@ +# 自增序列 + +PostgreSQL 表中的自增列 serial 的底层实现机制是: + +## 答案 + +绑定 sequence 对象的表达式默认值 + +### 选项 + +### PG 采用的是独立的序列器,不依赖表 + +auto increment 计数器 + +### B + +触发器 + +### C + +系统表 + diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/table.json" "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/table.json" new file mode 100644 index 0000000..4163770 --- /dev/null +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/table.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "table.md", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/table.md" "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/table.md" new file mode 100644 index 0000000..37182a5 --- /dev/null +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/1.\350\241\250/table.md" @@ -0,0 +1,26 @@ +# 表的基本结构 + +关于数据表的介绍,哪一句是对的? + +## 答案 + +表中每一行记录的结构都要遵循表定义。 + +## 选项 + +### 虽然不推荐,但是可以没有主键 + +表一定要有主键 + +### 表的主键是任意可以建立唯一索引的字段组合 + +表的主键必须是自增的整数 id + +### 索引可以不唯一 + +索引列的内容是唯一的 + +### 虽然不推荐,索引、唯一约束都可以不止一个 + +每个表只能有一个索引 + diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/2.\345\207\275\346\225\260/config.json" "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/2.\345\207\275\346\225\260/config.json" index de3eaee..46ce3b6 100644 --- "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/2.\345\207\275\346\225\260/config.json" +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/2.\345\207\275\346\225\260/config.json" @@ -2,5 +2,5 @@ "node_id": "pg-f371b91ef63a4ce08f80b92bc8580196", "keywords": [], "children": [], - "export": [] + "export": ["function.json"] } \ No newline at end of file diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/2.\345\207\275\346\225\260/function.json" "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/2.\345\207\275\346\225\260/function.json" new file mode 100644 index 0000000..5eb79a9 --- /dev/null +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/2.\345\207\275\346\225\260/function.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "function.md", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/2.\345\207\275\346\225\260/function.md" "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/2.\345\207\275\346\225\260/function.md" new file mode 100644 index 0000000..7f0e049 --- /dev/null +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/1.PostgreSQL\346\225\260\346\215\256\345\272\223\347\232\204\345\237\272\346\234\254\347\273\223\346\236\204/2.\345\207\275\346\225\260/function.md" @@ -0,0 +1,29 @@ +# 函数 + +关于 PostgreSQL 函数,错误的是: + +## 答案 + +函数必须是无副作用的 + +## 选项 + +### A + +函数可以用 SQL 写,也可以用 PLPGSQL,还可以用 Python、Perl、LUA等语言。 + +### B + +函数的参数和返回值可以是简单变量,也可以是结果集或自定义类型 + +### C + +函数可以递归引用 + +### D + +函数之间可以互相引用 + +### E + +函数的使用权限可以通过 grant/revoke/deny 管理 diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/5.CTE/to_root.json" "b/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/5.CTE/to_root.json" new file mode 100644 index 0000000..4df2186 --- /dev/null +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/5.CTE/to_root.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "to_root.md", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/5.CTE/to_root.md" "b/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/5.CTE/to_root.md" new file mode 100644 index 0000000..8c33ca8 --- /dev/null +++ "b/data/2.PostgreSQL\344\270\255\351\230\266/2.\346\234\215\345\212\241\347\253\257\347\274\226\347\250\213/5.CTE/to_root.md" @@ -0,0 +1,65 @@ +# 树结构溯根 + +现有一个表 node + +```postgresql +create table node +( + id serial primary key, + pid integer, + content text +); +``` + +其 pid 列引用 id 列,形成一个树结构,根节点的 pid 为 0。 + +现在我们希望写一个查询,找到某一个给定id的记录,其父节点、父节点的父节点,直至根节点的路径。那么这个查询应该是: + +## 答案 + +```postgresql +with recursive t(id, pid, content) as (select id, pid, content + from node + where id = $1 + union all + select node.id, node.pid, node.level + from node + join t on node.id = t.pid) +select node.id, node.pid, content +from node + join t on node.id = t.id; +``` + +## 选项 + +### 没有递归定义 + +```postgresql +with t as (select id, pid, content + from node + where id = $1 + union all + select node.id, node.pid, node.level + from node + join t on node.id = t.pid) +select node.id, node.pid, content +from node + join t on node.id = t.id; +``` + +### 平凡的连接查询无法处理递归问题 + +```postgresql +select node.id, node.pid, node.content +from node + join node as p on node.pid = p.id +where id=$1; +``` + +### 子查询无法处理递归问题 + +```postgresql +select node.id, node.pid, node content +from node as t +where t.pid = (select id from t where id=t.pid) +``` \ No newline at end of file -- GitLab