提交 490aed47 编写于 作者: M Mars Liu

add pre commit hook

上级 937b9450
repos:
- repo: git@codechina.csdn.net:csdn/skill_tree_hook.git
rev: 8cb76ab3b493582d3dab0021fb6fddfa28ec5a66
hooks:
- id: pre-commit
verbose: true
\ No newline at end of file
# {在此填写标题}
{在此填写题目描述}
## 答案
{在此填写答案}
## 选项
### A
{在此填写选项A}
### B
{在此填写选项B}
### C
{在此填写选项C}
{
"node_id": "",
"keywords": [
"关系型数据库",
"database",
"relational",
"rdbms"
],
"children": [],
"export": [
"relation.json"
],
"keywords_must": [
"关系型数据库",
"database",
"relational",
"rdbms"
],
"keywords_forbid": []
}
\ No newline at end of file
{ {
"type": "code_options", "type": "code_options",
"author": null, "author": "ccat",
"source": "helloworld.md", "source": "relation.md",
"notebook_enable": false, "notebook_enable": false,
"exercise_id": "c58c8c60118e4603b5255168de60d79e" "exercise_id": "e50c1cee116d4054b9794c57effa9f5a"
} }
\ No newline at end of file
# 关系型数据库
下列数据库产品中,哪一个通常不被视作关系型数据库?
## 答案
redis
## 选项
### A
PostgreSQL
### B
SQL Server
### C
Oracle
### D
H2
### E
MySQL
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "client.md",
"notebook_enable": false,
"exercise_id": "88b033cd97c642ef9900e5e091e335ff"
}
\ No newline at end of file
# 连接 PostgreSQL 数据库
下列选项中,可以用于连接到PostgreSQL数据库的有:
## 答案
全部都可以
## 选项
### A
命令行工具 psql
### B
GUI 工具 PgAdmin
### C
JDBC 库 `org.postgresql:postgresql`
### D
Python DBAPI 实现 psycopg2
### E
Go 语言驱动库 PQ。
{
"node_id": "",
"keywords": [
"server",
"client",
"数据库服务器",
"数据库客户端"
],
"children": [],
"export": [
"client.json",
"server.json"
],
"keywords_must": [
"服务器",
"客户端"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "server.md",
"notebook_enable": false,
"exercise_id": "55868ca50313434d855bbc1e997cf61b"
}
\ No newline at end of file
# PostgreSQL 服务器
关于 PostgreSQL 服务器,以下说法错误的是:
## 答案
PostgreSQL 的服务器和客户端必须运行在不同的计算机上。
## 选项
### A
PostgreSQL 的每个活跃连接对应一个 worker 进程。
### B
PostgreSQL 的服务器将数据存储在一个 owner 为 postgres 的目录下。
### C
PostgreSQL 可以设置监听端口、地址和最大连接数
### D
PostgreSQL 的超级用户默认为 postgres
### E
PostgreSQL 可以运行在 FreeBSD、Linux或Windows系统。
{ {
"node_id": "mysql-2cd5f68281704161820ee048e1f9de36", "node_id": "",
"keywords": [], "keywords": [],
"keywords_must": [], "keywords_must": [],
"keywords_forbid": [] "keywords_forbid": []
......
{
"node_id": "",
"keywords": [
"安装",
"PostgreSQL"
],
"children": [],
"export": [
"install.json"
],
"keywords_must": [
"安装"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "install.md",
"notebook_enable": false,
"exercise_id": "ca4669e5d66944df830390aec841cc7b"
}
\ No newline at end of file
# 安装
下列内容,不正确的是哪一项?
## 答案
安装过程需要 root 权限,也应该将 root 设置为超级用户。
## 选项
### A
在 Linux 上,可以用发行版自带的软件安装服务安装,用 systemctl 管理。
### B
在 FreeBSD 上,可以用 ports 安装。
### C
在 windows 上,可以下载安装包进行安装。
### D
PostgreSQL 默认的超级用户名为 postgres 。
### E
修改 postgres.conf 配置文件,可以修改 PG 的监听地址和端口。
{
"node_id": "",
"keywords": [
"登录",
"身份验证",
"授权"
],
"children": [],
"export": [
"login.json",
"rds.json"
],
"keywords_must": [
"登录"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "login.md",
"notebook_enable": false,
"exercise_id": "54917a4c69af41749d1c2a4430267f27"
}
\ No newline at end of file
# 登录
你的团队有一个公用的开发服务器,你要安装一个 PostgreSQL 服务公用,你们希望每位同事
使用一个独立的开发数据库,不互相混淆。同时这个服务器不向其他部门开放。数据也希望有足
够的安全保障。你的同事们都有这台开发服务器的 ssh 账号,平时远程登录到服务器上工作。
下面哪个组合最符合你们的需求?
1. 为每位同事建立一个数据库账号
2. 初始化口令为123456,告知他们登录后修改密码。
3. 为每位同事建立对应的开发数据库,将他们的用户设置为该数据库的 owner
4. 将每位同事的用户身份都设置为superuser,并禁止他们登录别人的数据库
5. 为每位同事建立一个与操作系统账号同名的数据库账号
6. 修改 pg_hba.conf ,设置本地用户可以用操作系统集成登录
7. 设置 postgres 账号口令为 123456,群发告知同事
## 答案
```
5, 3, 6
```
## 选项
### A
```
1, 2, 4
```
### B
```
1, 2, 4, 6
```
### C
```
5, 2, 4
```
### D
```
5, 2, 4, 6
```
### E
```
7, 6
```
### F
```
2, 3, 4, 5, 6, 7
```
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "rds.md",
"notebook_enable": false,
"exercise_id": "6b04760cd212469c925eb4a9c4a66757"
}
\ No newline at end of file
# RDS 管理
RShop 公司在云服务商处购买了一个 PostgreSQL RDS 服务,作为 DBA ,你会采取哪些措施来管理它?
1. 将云服务商提供的用户名口令交付开发团队
2. 要求开发团队交付数据库部署脚本
3. 部署并初始化数据库结构
4. 建立应用用户,采用口令登录,仅赋予数据查询和 DML 权限
5. 将应用用户的连接方式写入配置中心,供应用系统访问
6. 待开发团队初始化数据库后,修改用户名口令
7. 修改用户名口令后,将系统用户和口令保存在可信的存储中
## 答案
```
2, 3, 4, 5, 7
```
## 选项
### A
```
1, 3, 6, 4, 5, 7
```
### B
```
1, 3
```
### C
```
2, 4, 6
```
### D
```
1, 3, 5, 7
```
\ No newline at end of file
{
"node_id": "",
"keywords": [
"login",
"connect"
],
"children": [],
"export": [
"develop.json"
],
"keywords_must": [
"使用",
"基本用法",
"入门"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "develop.md",
"notebook_enable": false,
"exercise_id": "257414ca11f24a35a6270166b6bd2830"
}
\ No newline at end of file
# 开发环境中的 PostgreSQL
你是 SmartMarket 公司的一名开发人员,公司使用 Linux 运行 PostgreSQL 服务,而你的开发机使用 MacOS。下列说法正确的是:
1. 在 MacOS 上,使用 Homebrew 安装 PostgreSQL 后,在当前用户下执行 psql postgres 可以登入数据库
2. 在 Linux 上,使用 yum 或 apt 安装 PostgreSQL 后,sudo su postgres 切换到 postgres 用户,可以执行 psql 登入数据库
3. 对于自己安装的数据库服务器(非RDS),如果需要开放远程登录,需要编辑数据目录下的 postgresql.conf 和 pg_hua.conf
4. 安装 PostgreSQL 前,需要重装系统以获得一个干净的内核环境
5. 安装 PostgreSQL 前,需要手工建立一个名为 postgres 的超级用户
6. 安装 PostgreSQL 需要先安装 gcc 或 llvm clang
7. 对于使用 linux 的开发者,通常需要安装 postgresql 的开发包,这通常可以在主流的 linux 发行版的软件服务中找到,一般叫 postgresql-devel 或 postgresql-dev
## 答案
1, 2, 3, 7
## 选项
### A
1, 2, 3, 4, 5, 6, 7
### B
1, 2, 3
### C
2, 3, 4, 5
### D
5, 6, 7
### E
1, 3, 5, 7
### F
2, 4, 6
{ {
"node_id": "mysql-96403217971e4115835e31bba9366fcb", "node_id": "",
"keywords": [], "keywords": [],
"children": [],
"export": [
"helloworld.json"
],
"keywords_must": [], "keywords_must": [],
"keywords_forbid": [] "keywords_forbid": []
} }
\ No newline at end of file
{
"node_id": "",
"keywords": [
"sql",
"psql",
" 数据库客户端"
],
"children": [],
"export": [
"psql.json"
],
"keywords_must": [
"psql"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "psql.md",
"notebook_enable": false,
"exercise_id": "47faab6fba92431da6cbb2dacbf5bae4"
}
\ No newline at end of file
# PSQL 命令行
关于 PSQL 命令行,以下说法正确的是:
1. psql 命令行内置在 postgresql 安装包中
2. psql 命令行默认以系统当前用户身份登录本机,连接用户同名数据库
3. 进入 psql 命令行,可以使用 `\?` 查看内置的快捷指令
4. 在 psql 命令行中,可以通过 `\!` 在服务端执行shell命令
5. psql 命令行必须以 postgres 用户身份执行
6. psql 命令行必须以 root 用户身份执行
7. psql 只能用于 linux 和 bsd 系统
8. 在 psql 中执行 `\d` 可以查看 relations 列表,即用户表和视图
9. 在 psql 中执行 `\l` 可以查看数据库列表
10. 在 psql 中执行 `\d 表名` 可以查看指定表或视图的详细信息
11. 在 psql 中执行 `\timing` 可以开关查询计时
12. `analyze 表名;``explain 查询;` 以及 `explain analyze 查询;` 都是 psql 特有的命令
## 答案
1, 2, 3, 4, 8, 9, 10, 11
## 选项
### A
全部都对
### B
1, 2, 3, 4, 5, 6
### C
2, 3, 4, 5, 6, 7, 8, 9
### D
全部都错
### E
9, 10, 11, 12
### F
5, 6, 7, 8, 9, 10, 11, 12
### G
5, 6, 7, 12
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "basic.md",
"notebook_enable": false,
"exercise_id": "76fffa1781f140fd9abee9231a7186f4"
}
\ No newline at end of file
# 基本语法
下列 SQL 语句,哪一项不合法?
## 答案
```postgresql
from test select abc;
```
## 选项
### A
```postgresql
select 3.14;
```
### B
```postgresql
select * from employee;
```
### C
```postgresql
select * from employee where dept = 'hr';
```
### D
```postgresql
select id, name, dept, salary from employee where salary > 10000::money;
```
### E
```postgresql
select now();
```
{
"node_id": "",
"keywords": [
"语法",
"select"
],
"children": [],
"export": [
"basic.json"
],
"keywords_must": [
"语法",
"SQL"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "concept.md",
"notebook_enable": false,
"exercise_id": "ce2be9e86c6643a8a545115ae70ee8c1"
}
\ No newline at end of file
# 基本概念
下列叙述中,正确的是
1. 带有 select 关键字的查询不会修改数据
2. 查询表需要 select 权限
3. 修改一个数据库表或对象的结构,通常语句中会出现 create、add、drop、alter 等关键字
4. 修改数据内容,通常会出现update、insert、delete等关键字
5. update、insert、delete可以单独授权
## 答案
2, 3, 4, 5
## 选项
### A
1, 2, 3, 4, 5
### B
4, 5
### C
1, 2, 3
### D
3, 4, 5
\ No newline at end of file
{
"node_id": "",
"keywords": [
"DML",
"insert",
"update",
"delete"
],
"children": [],
"export": [
"insert.json",
"update.json",
"delete.json",
"concept.json"
],
"keywords_must": [
"DML",
"DDL",
"insert",
"update",
"delete"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "delete.md",
"notebook_enable": false,
"exercise_id": "78e6ca15ba794ef3b2092de4640308b9"
}
\ No newline at end of file
# 删除
SmartMarket 公司的业务数据库中,有一个 orders 表,其结构主要是以下形态:
```postgresql
create table orders
(
id serial primary key,
meta jsonb default '{}'::jsonb,
content jsonb default '{}'::jsonb,
created_at timestamp default now(),
deal boolean
)
```
有一个业务系统会实时的将已经成交(deal 字段为 true)的订单数据转储,现在我们仅需要一个清理 程序,将已经成 交的数据从 orders 表删除并记录被删除的数据id。下面哪个操作是对的?
## 答案
在一个独立的定时任务中执行
```postgresql
delete
from orders
where deal
returning id;
```
并记录id
## 选项
### A
在一个独立的定时任务中执行
```postgresql
truncate orders;
```
### B
在一个独立的定时任务中执行
```postgresql
delete
from orders;
```
### C
在一个独立的定时任务中执行
```postgresql
drop table orders;
create table orders
(
id serial primary key,
meta jsonb default '{}'::jsonb,
content jsonb default '{}'::jsonb,
created_at timestamp default now(),
deal boolean
);
```
### D
建立视图
```postgresql
create view order_view as
select id, meta, content, created_at
from orders
where not deal;
```
并要求业务系统只能访问这个视图。
### E
在一个独立的定时任务中执行
```postgresql
delete
from orders
where deal;
```
并记录操作前后表中的最大 id
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "insert.md",
"notebook_enable": false,
"exercise_id": "2d562e3ae4a84e648de31452a67ba71f"
}
\ No newline at end of file
# 插入
现有一个表:
```postgresql
create table book(
id serial primary key ,
title text not null ,
meta jsonb default '{}'::jsonb,
price money,
isbn text not null ,
publish_at date not null
);
create unique index on book(isbn);
create index on book using gin(meta);
```
那么下列哪个选项的代码可以执行成功?
## 答案
```postgresql
insert into book(title, price, isbn, publish_at) select 'a book title', 25.4, 'xx-xxxx-xxxx', '2019-12-1'::date;
insert into book(title, price, isbn, publish_at) select 'a other book title', 25.4, 'yy-yyyy-xxxx', '2019-12-1'::date;
```
## 选项
### 唯一键冲突
```postgresql
insert into book(title, price, isbn, publish_at) select 'a book title', 25.4, 'xx-xxxx-xxxx', '2019-12-1'::date;
insert into book(title, price, isbn, publish_at) select 'a other book title', 35.4, 'xx-xxxx-xxxx', '2019-12-1'::date;
```
### 缺少必要的列
```postgresql
insert into book(price, isbn, publish_at) select 25.4, 'xx-xxxx-xxxx', '2019-12-1'::date;
insert into book(price, isbn, publish_at) select 35.4, 'yy-yyyy-xxxx', '2019-12-1'::date;
```
### 类型错误
```postgresql
insert into book(title, price, isbn, publish_at) select 'a book title', 'unknown', 'xx-xxxx-xxxx', '2019-12-1'::date;
insert into book(title, price, isbn, publish_at) select 'a other book title', 'unknown', 'xx-xxxx-xxxx', '2019-12-1'::date;
```
### 违反非空约束
```postgresql
insert into book(title, price, isbn, publish_at) select null, 'unknown', 'xx-xxxx-xxxx', '2019-12-1'::date;
insert into book(title, price, isbn, publish_at) select null, 'unknown', 'xx-xxxx-xxxx', '2019-12-1'::date;
```
{
"type": "code_options",
"author": "ccat",
"source": "update.md",
"notebook_enable": false,
"exercise_id": "d6f2c270c3cd41a499715e55a2c565ba"
}
\ No newline at end of file
# 更新数据
现有 employee 表如下:
```postgresql
create table employee
(
id serial primary key,
name text,
dept text,
salary money
);
```
我们希望修改销售部(dept 字段为 sale)员工 Dora Muk 的工资,将其增加 1000,返回她的工号。正确的修改语句是:
## 答案
```postgresql
update employee set salary = salary + 1000 where dept = 'sale' and name = 'Dora Muk' returning id;
```
## 选项
### 过滤条件不严谨
```postgresql
update employee set salary = salary + 1000 where name = 'Dora Muk' returning id;
```
### 没有返回员工id
```postgresql
update employee set salary = salary + 1000 where dept = 'sale' and name = 'Dora Muk';
```
### 缺少过滤条件
```postgresql
update employee set salary = salary + 1000 returning id;
```
### 错误的赋值语句
```postgresql
update employee set salary += 1000 returning id;
```
{
"node_id": "",
"keywords": [],
"keywords_must": [],
"keywords_forbid": []
}
\ No newline at end of file
{
"node_id": "",
"keywords": [
"表",
"table"
],
"children": [],
"export": [
"table.json",
"create_table.json",
"serial.json"
],
"keywords_must": [
"表",
"table"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "create_table.md",
"notebook_enable": false,
"exercise_id": "14cc4d8de8c44415b80e9c94ea617464"
}
\ No newline at end of file
# 建表语句
现在我们尝试建立一个简化的交易流水表 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
{
"type": "code_options",
"author": "ccat",
"source": "serial.md",
"notebook_enable": false,
"exercise_id": "69bcc5f08a4e4328b54e389d74363e2a"
}
\ No newline at end of file
# 自增序列
PostgreSQL 表中的自增列 serial 的底层实现机制是:
## 答案
绑定 sequence 对象的表达式默认值
## 选项
### PG采用的是独立的序列器
auto increment 计数器
### B
触发器
### C
系统表
### D
Serial 文件
{
"type": "code_options",
"author": "ccat",
"source": "table.md",
"notebook_enable": false,
"exercise_id": "17b32ec3157e4b7aaa6948fa04db51e3"
}
\ No newline at end of file
# 表的基本结构
关于数据表的介绍,哪一句是对的?
## 答案
表中每一行记录的结构都要遵循表定义。
## 选项
### 虽然不推荐,但是可以没有主键
表一定要有主键
### 表的主键是任意可以建立唯一索引的字段组合
表的主键必须是自增的整数 id
### 索引可以不唯一
索引列的内容是唯一的
### 虽然不推荐,索引、唯一约束都可以不止一个
每个表只能有一个索引
{
"node_id": "",
"keywords": [
"函数",
"function"
],
"children": [],
"export": [
"function.json"
],
"keywords_must": [
"函数",
"function"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "function.md",
"notebook_enable": false,
"exercise_id": "7d0a6977f12a4d93bd684b56b2ad7003"
}
\ No newline at end of file
# 函数
关于 PostgreSQL 函数,错误的是:
## 答案
函数必须是无副作用的
## 选项
### A
函数可以用 SQL 写,也可以用 PLPGSQL,还可以用 Python、Perl、LUA等语言。
### B
函数的参数和返回值可以是简单变量,也可以是结果集或自定义类型
### C
函数可以递归引用
### D
函数之间可以互相引用
### E
函数的使用权限可以通过 grant/revoke/deny 管理
{
"node_id": "",
"keywords": [
"role",
"login",
"user",
"用户",
"角色",
"权限",
"privilege"
],
"children": [],
"export": [
"grant.json",
"revoke.json",
"role.json"
],
"keywords_must": [
"role",
"login",
"user",
"用户",
"角色",
"权限",
"privilege"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "grant.md",
"notebook_enable": false,
"exercise_id": "0766e022b61f4c83b4fe64cd8e4e5e9b"
}
\ No newline at end of file
# 授权
管理员要给用户 fred 授权,允许他查询 emplyee 表,应用哪一条语句?
## 答案
```postgresql
grant select on table employee to fred;
```
## 选项
### 权限名错误
```postgresql
grant query on table employee to fred;```
```
### 权限名错误
```postgresql
grant read on table employee to fred;```
```
### 操作关键词错误
```postgresql
grant select on table employee of fred;```
```
### 操作错误
```postgresql
grant select on table employee.* of fred;```
```
### 权限过高
```postgresql
grant all on table employee to fred;```
```
{
"type": "code_options",
"author": "ccat",
"source": "revoke.md",
"notebook_enable": false,
"exercise_id": "65de2267ccb7478db916d517d4b5bce7"
}
\ No newline at end of file
# 撤销权限
数据组的 Fred 调到了研发团队,不再参与分析生产数据,现在管理员要收回他对 trade 表的查询权限,假设这个权限是授予他本人的数据库
用户 fred ,下面哪个操作是对的?
## 答案
```postgresql
revoke select on trade from fred;
```
## 选项
### 操作错误
```postgresql
grant not select on trade to fred;
```
### 操作关键字错误
```postgresql
revoke select on trade to fred;
```
### 指定权限错误
```postgresql
revoke owned trade from fred;
```
{
"type": "code_options",
"author": "ccat",
"source": "role.md",
"notebook_enable": false,
"exercise_id": "f093e146721541139080bc01ffd39750"
}
\ No newline at end of file
# 角色
你是 rental dvd 公司的数据库管理员,公司数据分析组有 Fred、Alice、James、Jone 四位成员,现在你需要给数据分析组授权,允许他们
查询 trade 数据库的 public schema 中的所有表,规范的操作应该是
## 答案
```postgresql
create role analysis;
grant analysis to fred, alice, james, jone;
grant select on all tables in schema public to analysis;
```
## 选项
### 将来人员变动管理会很繁琐
```postgresql
grant select on all tables in schema public to fred, alice, james, jone;
```
### 过度授权
```postgresql
create role analysis;
grant analysis to fred, alice, james, jone;
grant all on all tables in schema public to analysis;
```
### 语句不完整
```postgresql
create role analysis;
grant analysis to fred, alice, james, jone;
grant select on all to analysis;
```
\ No newline at end of file
{
"node_id": "",
"keywords": [
"索引",
" 约束",
"constraints",
"index"
],
"children": [],
"export": [
"primary_key.json",
"unique.json",
"unique_2.json"
],
"keywords_must": [
"索引",
" 约束",
"constraints",
"index"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "primary_key.md",
"notebook_enable": false,
"exercise_id": "412c89270d24418ab35a1d919a5017e7"
}
\ No newline at end of file
# 主键
关于 PostgreSQL 的主键,哪一项是错误的?
## 答案
主键列只能是自增 id。
## 选项
### A
主键隐含了聚集索引和唯一约束
### B
主键可以是一个字段,也可以是多个字段的组合
### C
语法约束上,可以允许无主键的表,但是从工程实践上,应该保持每个表都有正确的主键。
### D
主键或唯一键可以被引用为外键约束
### E
主键应可以唯一的标识数据,并且主键的一部分不应该依赖另一部分。
{
"type": "code_options",
"author": "ccat",
"source": "unique.md",
"notebook_enable": false,
"exercise_id": "482896ba26ec43bc8ddedbf3448d47bb"
}
\ No newline at end of file
# 唯一约束
现有一个图书登记表:
```postgresql
create table book(
id serial primary key ,
title text,
publish_at date,
isbn text,
meta jsonb default '{}'::jsonb
)
```
我们发现有时候客户可能会重复输入同一本书的信息,怎样约束用户不会输入同一本书?
1. 删除id列,将isbn设置为主键
2. 在 isbn 列上加唯一约束
3. 执行 `create index on book(id, title, publish_at, isbn, meta)`
4. 在 id 键上加唯一约束
## 答案
1 或者 2
## 选项
### A
3
### B
4
### C
3 或 4
### D
3 和 4
{
"type": "code_options",
"author": "ccat",
"source": "unique_2.md",
"notebook_enable": false,
"exercise_id": "f1f5779378ef4684a210188e5730b6a1"
}
\ No newline at end of file
# 唯一约束
现有一个图书登记表:
```postgresql
create table book(
id serial primary key ,
title text,
publish_at date,
isbn text,
meta jsonb default '{}'::jsonb
)
```
我们发现有时候客户可能会重复输入同一本书的信息,*在不修改应用层程序的前提下*,怎样约束用户不会输入同一本书?
1. 删除id列,将isbn设置为主键
2. 在 isbn 列上加唯一约束
3. 执行 `create index on book(id, title, publish_at, isbn, meta)`
4. 在 id 键上加唯一约束
## 答案
2
## 选项
### A
3
### B
4
### C
3 或 4
### D
3 和 4
### E
1 或 2
\ No newline at end of file
{
"node_id": "",
"keywords": [
"trigger",
"触发器"
],
"children": [],
"export": [
"trigger.json"
],
"keywords_must": [
"trigger",
"触发器"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "trigger.md",
"notebook_enable": false,
"exercise_id": "2724dfb0a71c473b8a071e1d2aecc030"
}
\ No newline at end of file
# 触发器
SmartMarket 公司的OA数据库中包含以下结构:
```postgresql
create table employee(
id serial primary key ,
name text,
dept text,
salary money,
meta jsonb default '{}'::jsonb
);
create table budget(
id serial primary key ,
dept text,
amount money
)
```
我们省略了无关的内容。当某个员工的工资发生变动时,我们要修改他所在部门的预算。那么以下哪个选项可以解决问题?
## 答案
在 employee 表添加一个 after 触发器,当员工信息变动时,重算相关部门的预算。
## 选项
### A
在 budget 表添加触发器,当员工信息变动时,重算相关部门的预算。
### B
将预算总额字段变成计算列,通过统计员工工资生成。
### C
将员工信息表的工资字段设置为部门预算总额的外键引用字段,并设置级联更新。
\ No newline at end of file
{
"node_id": "",
"keywords": [
"数据库扩展",
"数据库插件",
"extension"
],
"children": [],
"export": [
"language.json",
"extension.json"
],
"keywords_must": [
"数据库扩展",
"extension"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "extension.md",
"notebook_enable": false,
"exercise_id": "fad124e7fe1a420dbfbe72de8d4b3f11"
}
\ No newline at end of file
# 外部扩展
下列哪个功能需要通过`create exension`语句安装扩展得到?
## 答案
postgis
## 选项
### A
全文检索
### B
JSONB
### C
GIST 和 GIN 索引
### D
JSON Path 支持
### E
XML 和 XSLT 支持
### F
面向对象语法
{
"type": "code_options",
"author": "ccat",
"source": "language.md",
"notebook_enable": false,
"exercise_id": "4657a4c34eec421a913625fb61af00d4"
}
\ No newline at end of file
# 过程语言
关于 PostgreSQL PL 语言,错误的是:
## 答案
要安装新的扩展语言,需要重新编译 PostgreSQL 内核。
## 选项
### A
PG 可以支持 Python、Perl、Lua 等多种 PL 语言编写函数。
### B
通过 create language 可以安装新的 pl 语言支持
### C
实现新的 PL 语言需要遵循 PostgreSQL 的语言扩展规范。
### D
用外部语言实现函数,要考虑跨边界传递数据的开销
\ No newline at end of file
{
"node_id": "",
"keywords": [],
"keywords_must": [],
"keywords_forbid": []
}
\ No newline at end of file
{
"node_id": "",
"keywords": [
"group by",
"分组"
],
"children": [],
"export": [
"salary.json"
],
"keywords_must": [
"group by",
"分组",
"聚合"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "salary.md",
"notebook_enable": false,
"exercise_id": "31deab40b4af44c38c363b2333bdd977"
}
\ No newline at end of file
# 工资最高的人
现有员工信息表如下:
```postgresql
create table employee
(
id serial primary key,
name text,
dept text,
salary money
);
```
下面哪条查询,可以给出每个部门工资最高的员工的 id, name, dept, salary 四项信息?
## 答案
```postgresql
select l.id, l.name, l.dept, l.salary
from employee as l
join (select max(salary) as salary, dept
from employee
group by dept) as r
on l.dept = r.dept and l.salary = r.salary
```
## 选项
### select 与 group by 不匹配
```postgresql
select id, name, dept, max(salary)
from employee
group by dept;
```
### group by 不对
```postgresql
select id, name, dept, max(salary)
from employee
group by dept, id, name;
```
### group by 不对
```postgresql
select id, name, dept, max(salary)
from employee
group by dept, id, name
having salary = max(salary);
```
### 结构错误
```postgresql
select id, name, dept, max(salary)
from employee
where salary = max(salary)
group by dept;
```
{
"node_id": "",
"keywords": [
"join",
"连接查询"
],
"children": [],
"export": [
"customer_order.json"
],
"keywords_must": [
"join",
"连接查询"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "customer_order.md",
"notebook_enable": false,
"exercise_id": "18d5a22c34874bb4b73aac6846674243"
}
\ No newline at end of file
# 客户和订单
我们现在看下面这个客户/订单系统
```postgresql
create table customers
(
id serial primary key,
company_name text,
address text,
city text,
state text
);
create table products
(
id serial primary key,
description text,
unit_price money
);
create table orders
(
id serial primary key,
product_id integer references products (id),
order_date timestamp,
quantity integer,
customer_id integer references customers(id)
);
```
我们希望这个数据库能够允许每个订单包含多种商品,那么应该如何改造?
## 答案
* 添加一个 order_detail 表,引用 order id、product id,增加 quantity 列
* order 表中删除 product id 和 quantity 列
## 选项
### A
将 order 表的修改为以 product id 和 customer id 作为联合主键
### B
删除 orders 表中的 product id 和 quantity 列
### C
在 order 表的主键上加唯一约束
\ No newline at end of file
{
"node_id": "",
"keywords": [
"子查询",
"subquery"
],
"children": [],
"export": [
"subquery.json"
],
"keywords_must": [
"子查询",
"subquery"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "subquery.md",
"notebook_enable": false,
"exercise_id": "82be2aa951304dc9869cd7177fcba99f"
}
\ No newline at end of file
# 子查询
现有员工表
```postgresql
create table employee
(
id serial primary key,
name text,
dept text,
salary money
)
```
我们希望找出比销售部(dept 为 sale)工资最高的员工工资更高的那部分人,查询出他们的完整信息,下面哪一项可以满足要求?
## 答案
```postgresql
select id, name, dept, salary
from employee
where salary > (select max(salary)
from employee
where dept = 'sale')
```
## 选项
### A
```postgresql
select id, name, dept, salary
from employee
where dept = 'sale'
group by dept
having salary > max(salary)
```
### B
```postgresql
select l.id, l.name, l.dept, l.salary
from employee as l
join employee as r on l.salary > max(r.salary)
where r.dept = 'sale'
group by r.dept
```
### C
```postgresql
select id, name, dept, salary
from employee
having salary > (select max(salary) from employee where dept = 'sale')
```
{
"node_id": "",
"keywords": [
"分页",
"limit",
"offset"
],
"children": [],
"export": [
"paged.json"
],
"keywords_must": [
"分页",
"limit",
"offset"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "paged.md",
"notebook_enable": false,
"exercise_id": "c92fab72b5c24e76b1032ca7a0c64c44"
}
\ No newline at end of file
# 分页
我们有如下订单表:
```postgresql
create table orders
(
id serial primary key,
product_id integer,
order_date date default now(),
quantity integer,
customer_id integer
);
```
现在开发人员希望查询指定的某一天内的数据,并按每一百条一页查询,那么正确的语句应该是:
## 答案
```postgresql
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1
offset $2 limit 100;
```
## 选项
### 缺少 limit
```postgresql
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1
offset $2;
```
### 缺少 offset
```postgresql
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1;
```
### 结构不对
```postgresql
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1 and
offset $2 and limit 100;
```
{
"node_id": "",
"keywords": [
"common table expression",
"cte",
"recursive"
],
"children": [],
"export": [
"to_root.json"
],
"keywords_must": [
"cte"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "to_root.md",
"notebook_enable": false,
"exercise_id": "45f0cef12fcf4f9e90276dff6a336657"
}
\ No newline at end of file
# 树结构溯根
现有一个表 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.content
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
{
"node_id": "",
"keywords": [
"plsql",
"过程化"
],
"children": [],
"export": [
"loop.json"
],
"keywords_must": [
"过程化"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "loop.md",
"notebook_enable": false,
"exercise_id": "8d25c0639365404ba3c61282e5ea32ba"
}
\ No newline at end of file
# 循环
下面哪一项定义的函数可以生成指定范围内的整数数列?
## 答案
```postgresql
create function gen(start integer, stop integer)
returns setof integer as
$$
begin
for idx in start .. stop
loop
return next idx;
end loop;
end;
$$ language plpgsql;
```
## 选项
### A
```postgresql
create function gen(start integer, stop integer) returns integer as
$$
begin
for idx in start .. stop
loop
return idx;
end loop;
end;
$$ language plpgsql;
```
### B
```postgresql
create function gen(start integer, stop integer)
returns integer as
$$
begin
for idx in start .. stop
loop
yield idx;
end loop;
end;
$$ language plpgsql;
```
### C
```postgresql
create function gen(start integer, stop integer)
returns setof integer as
$$
begin
for idx in start .. stop
loop
return idx;
idx += 1;
end loop;
end;
$$ language plpgsql;
```
### D
```postgresql
create function gen(start integer, stop integer)
returns setof integer as
$$
begin
for idx in start .. stop
loop
select idx;
end loop;
end;
$$ language plpgsql;
```
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "analyze.md",
"notebook_enable": false,
"exercise_id": "ee4d10ae488b4900835e52d184822962"
}
\ No newline at end of file
# 建表权限
SmartMarket 公司有一百名分析师,他们都属于 analyst 团队,现在他们希望能够在 market 仓库的 ana schema 下
自由的建表和删除表,以便进行模型试验。下列操作步骤中需要选择哪几项?(ana schema 和分析师的团队角色还不存在)
1. `create schema ana`
2. `create role analyst`
3. `grant create, usage on schema ana to analyst`
4. 将 analyst 角色授予每个分析师的账号
5. 删除 public schema
6. `grant superuser on schema ana to analyst`
7. `grant owner on schema ana to analyst;`
## 答案
1,2, 3, 4
## 选项
### A
所有全部
### B
1, 2, 5, 6, 7
### C
1, 2, 6, 7
### D
1, 2, 3, 6, 7
### E
2, 3, 4, 5
\ No newline at end of file
{
"node_id": "",
"keywords": [
"创建表",
"授权",
"ddl"
],
"children": [],
"export": [
"create_table.json",
"analyze.json"
],
"keywords_must": [
"创建表",
"授权",
"ddl"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "create_table.md",
"notebook_enable": false,
"exercise_id": "b9fc3c13d8df447fa9244f142b5c8c16"
}
\ No newline at end of file
# 创建表
下列方法中,可以创建表的有:
1. 使用 `create table xxx();`语句
2. 使用 `copy` 语句从 csv 导入数据
3. 使用 `create table as select...` 语句从查询中创建表
## 答案
1, 3
## 选项
### A
1, 2, 3
### B
1, 2
### C
2, 3
### D
只有 1
\ No newline at end of file
{
"node_id": "",
"keywords": [],
"keywords_must": [],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "backup.md",
"notebook_enable": false,
"exercise_id": "261517a14bca4cfd98ce23caf63c9cf5"
}
\ No newline at end of file
# 高可靠备份
你的团队有一个非常重要的业务数据库,希望在需要时,可以重建它在一周内任意某个时间点的状态。下列规划中不必要的是:
1. 对 WAL 日志建立备份,按时间保留所有的 WAL 文件作为增量备份
2. 每天做一次冷备
3. 需要重现时,用离目标时间点最近的前一次冷备恢复一个数据库节点,启动为 standby 状态,用对应的 wal 恢复。
4. 建立流备份节点
5. 每天非高峰期将数据库离线,完整复制一份数据区目录
## 答案
```
4, 5
```
## 选项
### A
```
1, 2
```
### B
```
3
```
### C
```
以上所有工作都需要,没有多余项
```
### D
```
2, 4
```
\ No newline at end of file
{
"node_id": "",
"keywords": [
"standby",
"热备份",
"高可用",
"流复制"
],
"children": [],
"export": [
"standby.json",
"backup.json"
],
"keywords_must": [
"standby"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "standby.md",
"notebook_enable": false,
"exercise_id": "491d9bdd19c04279ac607b73a63bb3e6"
}
\ No newline at end of file
# standby
关于 PG Standby,错误的是:
## 答案
复制节点需要有主节点的超级用户权限。
## 选项
### A
Hot standby 是通过同步 WAL 实现的。
### B
从节点处于制度状态
### C
可以设置从节点切换为主节点的超时阈值
### D
从节点会保持与主节点的数据和结构一致
### E
从节点不需要和主节点保持长连接
### F
从节点不需要和主节点硬件环境一致
\ No newline at end of file
{
"node_id": "",
"keywords": [
"standby",
"高可用",
"流复制",
"wal"
],
"children": [],
"export": [
"stream.json"
],
"keywords_must": [
"流式复制",
"流复制"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "stream.md",
"notebook_enable": false,
"exercise_id": "061bfb92b674438cb56a6cf72c406cbb"
}
\ No newline at end of file
# 流式复制
关于流式复制,错误的是:
## 答案
流式复制的主节点损坏,会导致订阅节点也损坏。
## 选项
### A
订阅数据流的需要特定的复制角色
### B
订阅节点可以作为只读的从节点,提供读写分离
### C
主节点崩溃,子节点会将自己切换为独立工作状态
### D
节点间传输的是 wal 数据流
### E
主从节点的硬件配置可以不一致
### F
可以配置多级订阅
### G
可以多个从节点订阅一个主节点
\ No newline at end of file
{
"node_id": "",
"keywords": [
"外部数据连接",
"fdw"
],
"children": [],
"export": [
"fdw.json"
],
"keywords_must": [
"外部数据连接",
"fdw"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "fdw.md",
"notebook_enable": false,
"exercise_id": "762a9c3f032b4ce884858c18e2bb580c"
}
\ No newline at end of file
# 外部数据源
RDVD 公司有一个 Oracle 数据库和一个PG数据库,现在需要每天汇总一次 PG 中的订单表和 oracle 中的客户信息表,从中生成一份
报表。现在你想在尽量不增加冗余数据的前提下,在PG服务上每天执行一次简单的查询任务来生成报表,那么应该:
## 答案
* 在 PG 数据库建立一个 fdw 数据源,连接 Oracle 数据库
* 在 PG 服务器中建立一个视图, 对客户信息表进行的查询变成一个 PG 查询
* 基于客户信息视图和订单表生成每日报表
## 选项
### 异构平台无法直接复制,且复制节点会影响功能
* 设置 PG 为 Oracle 的复制节点
* 订阅 Oracle 的变更流
### 不可靠的方法
* 用一个定时任务复制 Oracle 中的客户信息变更
* 写入 PG 数据库
### 低效且占用空间
* 在 PG 服务器上建一个同构的客户表
* 在 Oracle 服务器上写一个触发器
* 将客户信息写入 PostgreSQL 数据库
\ No newline at end of file
{
"node_id": "",
"keywords": [],
"keywords_must": [],
"keywords_forbid": []
}
\ No newline at end of file
{
"node_id": "",
"keywords": [
"gis",
"地理信息"
],
"children": [],
"export": [
"gis.json"
],
"keywords_must": [
"gis",
"几何"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "gis.md",
"notebook_enable": false,
"exercise_id": "617b947cb29b4a75b4b9367eaf7b8a7d"
}
\ No newline at end of file
# 邻近查找
我们有一个简化的表结构如下:
```postgresql
create table city
(
id serial8,
city text,
poi point
);
create index idx_tbl_point on city using gist (poi) with (buffering = on);
```
要查找给定的点位于哪个城市,或者离哪个城市最近,应该是下列哪一项?
## 答案
```postgresql
select city, poi <-> point($1, $2) dist
from city
where poi <-> point($1, $2) < 100
order by poi <-> point($1, $2)
limit 1;
```
## 选项
### A
```postgresql
select city, poi <-> point($1, $2) dist
from city
where poi <-> point($1, $2) < 100
order by poi <-> point($1, $2) desc
limit 1;
```
### B
```postgresql
select *, poi <-> point($1, $2) dist
from city
where poi <-> point($1, $2) < 100
limit 1;
```
### C
```postgresql
select *, poi - point($1, $2) dist
from city
where poi - point($1, $2) < 100
order by poi - point($1, $2)
limit 1;
```
### D
```postgresql
select *, abs(poi - point($1, $2)) dist
from city
where abs(poi - point($1, $2)) < 100
order by abs(poi - point($1, $2))
limit 1;
```
\ No newline at end of file
{
"node_id": "",
"keywords": [
"json",
"jsonb"
],
"children": [],
"export": [
"json.json",
"match.json",
"tags.json"
],
"keywords_must": [
"json",
"jsonb"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "json.md",
"notebook_enable": false,
"exercise_id": "99a960a0b46f4b1ba19bdb4bce6ac31d"
}
\ No newline at end of file
# JSON 和 JSONB 类型
关于 JSON 和 JSONB 类型,哪句话是错的?
## 答案
JSON 就是 文本类型的封装,JSONB 是 JSON 的二进制压缩格式,JSON 和 JSONB 的功能与 TEXT 类型一样。
## 选项
### A
JSONB 是推荐的 JSON 字段类型,更为高效。
### B
JSONB 可以通过 GIST 倒排索引优化对内部结构的查询。
### C
PostgreSQL JSON 和 JSONB 支持 JSON PATH表达式
### D
JSONB 和 文本可以有效兼容
### E
JSON 和 JSONB 与其它类型一样,不能部分修改,只能作为整体插入或更新
{
"type": "code_options",
"author": "ccat",
"source": "match.md",
"notebook_enable": false,
"exercise_id": "5b730f2a3c734ce780766ad23dcad033"
}
\ No newline at end of file
# 匹配
表 book 的 meta 有类似如下结构:
```json
{
"author": [
"Mars Liu",
"Milly Lee"
],
"ISBN": "xxxx-xxxx-xxxxxx",
"version": 1
}
```
该字段有 gist 索引。
如果我们想高效率的找到所有作者包含 Jim Gray 的书,应该如何查询?
## 答案
```postgresql
select * from book where meta @> '{"author": ["Jim Gray"]}'::jsonb;
```
## 选项
### A
```postgresql
select * from book where meta @> '["Jim Gray"]'::jsonb;
```
### A
```postgresql
select * from book where 'Jim Gray' in meta;
```
### B
```postgresql
select * from book where meta::text like '%Jim Gray%';
```
### C
```postgresql
select * from book having meta @> 'Jim Gray'
```
### D
```postgresql
select * from book where 'Jim Gray' in (meta #>> 'author');
```
{
"type": "code_options",
"author": "ccat",
"source": "tags.md",
"notebook_enable": false,
"exercise_id": "a415bb85ddb3400aae34318b9c142639"
}
\ No newline at end of file
# 标签
现有一个表 sku:
```postgresql
create table sku(
id serial primary key ,
name text,
meta jsonb default '{}'::jsonb,
tags jsonb default '[]'::jsonb
)
```
我们希望查找包含所有给定tag的商品,那么查询应该是:
## 答案
```postgresql
select id, name, meta, tags from sku where tags @> $1;
```
## 选项
### A
```postgresql
select id, name, meta, tags from sku where tags = $1;
```
### B
```postgresql
select id, name, meta, tags from sku where tags::text = $1;
```
### C
```postgresql
select id, name, meta, tags from sku where $1 in tags;
```
### D
```postgresql
select id, name, meta, tags from sku where tags in $1;
```
{
"node_id": "",
"keywords": [
"函数",
"过程",
"function",
"produce"
],
"children": [],
"export": [
"distinct.json"
],
"keywords_must": [
"聚合函数",
"json函数",
[
"数组",
"函数"
],
""
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "distinct.md",
"notebook_enable": false,
"exercise_id": "e5c4e314fcee480db8f4db8f9d76e0fd"
}
\ No newline at end of file
# 去重
下列函数中,有一个实现了合并两个 jsonb 对象并对其去重,是哪一项?
## 答案
```postgresql
create function jsonb_distinct_merge(a jsonb, b jsonb) returns jsonb as
$$
select jsonb_agg(distinct (value))
from jsonb_array_elements(a || b)
$$ language sql
```
## 选项
### A
```postgresql
create function jsonb_distinct_merge(a jsonb, b jsonb) returns jsonb as
$$
select a || b
$$ language sql
```
### B
```postgresql
create function jsonb_distinct_merge(a jsonb, b jsonb) returns jsonb as
$$ return jsonb_agg(distinct(value)) from jsonb_array_elements(a||b)
$$
language sql
```
### C
```postgresql
create function jsonb_distinct_merge(a jsonb, b jsonb) returns jsonb as
$$ return next jsonb_agg(distinct(value)) from jsonb_array_elements(a||b)
$$
language sql
```
### D
```postgresql
create function jsonb_distinct_merge(a jsonb, b jsonb) returns jsonb as
$$
select a + b;
$$ language sql
```
\ No newline at end of file
{
"node_id": "",
"keywords": [
"视图",
"view"
],
"children": [],
"export": [
"view.json"
],
"keywords_must": [
"视图",
"view"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "view.md",
"notebook_enable": false,
"exercise_id": "29e05c6b76c648f8bf9bcf08be3de364"
}
\ No newline at end of file
# 视图
SmartMarket 公司的数据分析师,每天要执行一个固定的复杂查询,生成每日报表。我们准备将其创建为视图,这能够解决:
1. 简化查询
2. 降低查询时死锁的风险
3. 大幅优化查询性能
4. 限制权限
## 答案
1, 4
## 选项
### A
1. 2, 3, 4
### B
1, 2, 3
### C
2, 3
### D
3, 4
\ No newline at end of file
{
"node_id": "",
"keywords": [],
"keywords_must": [],
"keywords_forbid": []
}
\ No newline at end of file
{
"node_id": "",
"keywords": [
"cte",
"递归查询",
"recursive"
],
"children": [],
"export": [
"continuous.json"
],
"keywords_must": [
"cte",
"递归查询",
"recursive"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "continuous.md",
"notebook_enable": false,
"exercise_id": "bd4568ed83594d12b510c6c69bf611f2"
}
\ No newline at end of file
# 获取连续区间
SmartMarket 交易所的系统中,所有订单在生成时,都从一个 PostgreSQL Sequence 中获取唯一的 序列号,完成交易计算后各个撮合程序将其插入如下的 orders 表中:
```postgresql
create table orders
(
id integer primary key,
meta jsonb default '{}'::jsonb,
content jsonb default '{}'::jsonb
-- ignore other definitions...
);
```
后续的结算系统需要连续的获取订单,以便处理一些顺序敏感的业务。但是撮合发生在很多个异步节点上,它们只能保证最终会将所有订单都保存到 orders 表,确保 id 列最终是连续的,但是最新插入的一段记录集有可能不连续。而我们希望结算系统成批
的读取数据,以优化性能,那么在结算系统有它最后处理的订单id的前提下,下面哪一个查询可以确保从这个id向前读取 id 连续 的一批订单?
## 答案
```postgresql
with recursive r(id) as (select id
from orders
where id = $1
union
select d.id
from orders as d
join r on d.id = r.id + 1)
select orders.id, meta, content
from orders
join r on orders.id = r.id;
```
## 选项
### A
没有办法构造一个简单查询实现这个功能,因为它的数据过滤条件递归的依赖查询结果。
### B
```postgresql
select data.id, meta, content
from orders
join orders as r on orders.id = r.id - 1
where id = $1;
```
### C
```postgresql
select id, meta, content
from orders
where id in (select id from orders where id = id + 1);
```
### D
```postgresql
with r as (select id
from orders
where id = $1
union
select d.id
from orders as d
join r on d.id = r.id + 1)
select orders.id, meta, content
from orders
join r on orders.id = r.id;
```
\ No newline at end of file
{
"keywords": [
"函数",
"function"
],
"children": [],
"node_id": "",
"export": [
"salary.json"
],
"keywords_must": [
"函数",
"function",
"window"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "salary.md",
"notebook_enable": false,
"exercise_id": "6dc9ce6d2a354a9986585a458e5476c9"
}
\ No newline at end of file
# 工资最高的人
现有员工信息表如下:
```postgresql
create table employee
(
id serial primary key,
name text,
dept text,
salary money
);
```
下面哪条查询,可以给出每个部门工资最高的前五个员工的 id, name, dept, salary 四项信息?
## 答案
```postgresql
select id, name, dept, salary
from (select id, name, dept, salary, rank() over (partition by dept order by salary desc) as r
from employee) as t
where r <= 5;
```
## 选项
### 结构错误
```postgresql
select id, name, dept, max(salary) as salary
from employee
group by dept
having id < 6;
```
### 结构错误
```postgresql
select l.id, l.name, l.dept, l.salary
from employee as l
join (select max(salary) as salary, dept
from employee
group by dept) as r
on l.dept = r.dept and l.salary = r.salary
where count(r.id) <= 5;
```
### 结构错误
```postgresql
select l.id, l.name, l.dept, l.salary
from employee as l
join (select max(salary, 5) as salary, dept
from employee
group by dept) as r
on l.dept = r.dept and l.salary = r.salary
```
### 结构错误
```postgresql
select id, name, dept, salary, rank() over (partition by dept order by salary desc) as r
from employee
where r <= 5;
```
### 结构错误
```postgresql
select id, name, dept, salary, rank() as r over (partition by dept order by salary desc)
from employee
where r <= 5;
```
{
"node_id": "",
"keywords": [
"透视表",
"交叉透视表",
"pivot"
],
"children": [],
"export": [
"pivot.json"
],
"keywords_must": [
"透视表",
"交叉透视表",
"pivot"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "pivot.md",
"notebook_enable": false,
"exercise_id": "e39489bf015448fb87c9722d05b357a4"
}
\ No newline at end of file
# 透视表
现有销售记录表
```postgresql
create table sales(
id serial primary key ,
sku_id integer not null ,
amount money,
meta jsonb default '{}'::jsonb,
created_at timestamp default now()
);
create index on sales(created_at);
```
现在我们希望对这个表做一个月度的透视汇总,得到2020年每个月每种商品(sku_id)的的销售总额,每个月一列,哪一项可以实现?
## 答案
```postgresql
select sku_id,
sum(case extract(month from created_at) when 1 then amount else 0::money end) as Jan,
sum(case extract(month from created_at) when 2 then amount else 0::money end) as Feb,
sum(case extract(month from created_at) when 3 then amount else 0::money end) as Mar,
sum(case extract(month from created_at) when 4 then amount else 0::money end) as Apr,
sum(case extract(month from created_at) when 5 then amount else 0::money end) as May,
sum(case extract(month from created_at) when 6 then amount else 0::money end) as June,
sum(case extract(month from created_at) when 7 then amount else 0::money end) as July,
sum(case extract(month from created_at) when 8 then amount else 0::money end) as Aug,
sum(case extract(month from created_at) when 9 then amount else 0::money end) as Sept,
sum(case extract(month from created_at) when 10 then amount else 0::money end) as Oct,
sum(case extract(month from created_at) when 11 then amount else 0::money end) as Nov,
sum(case extract(month from created_at) when 12 then amount else 0::money end) as Dec
from sales
where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp
group by sku_id;
```
## 选项
### 格式不相符
```postgresql
select sku_id, extract(month from created_at) as month, sum(amount)
from sales
where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp
group by 1, 2;
```
### 计算逻辑错误
```postgresql
select sku_id,
sum(amount) as Jan,
sum(amount) as Feb,
sum(amount) as Mar,
sum(amount) as Apr,
sum(amount) as May,
sum(amount) as June,
sum(amount) as July,
sum(amount) as Aug,
sum(amount) as Sept,
sum(amount) as Oct,
sum(amount) as Nov,
sum(amount) as Dec
from sales
where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp
group by sku_id, extract(month from created_at);
```
### 计算格式错误
```postgresql
select sku_id,
sum(amount having extract(month from created_at) = 1) as Jan,
sum(amount having extract(month from created_at) = 2) as Feb,
sum(amount having extract(month from created_at) = 3) as Mar,
sum(amount having extract(month from created_at) = 4) as Apr,
sum(amount having extract(month from created_at) = 5) as May,
sum(amount having extract(month from created_at) = 6) as June,
sum(amount having extract(month from created_at) = 7) as July,
sum(amount having extract(month from created_at) = 8) as Aug,
sum(amount having extract(month from created_at) = 9) as Sept,
sum(amount having extract(month from created_at) = 10) as Oct,
sum(amount having extract(month from created_at) = 11) as Nov,
sum(amount having extract(month from created_at) = 12) as Dec
from sales
where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp
group by sku_id, extract(month from created_at);
```
\ No newline at end of file
{
"node_id": "",
"keywords": [
"conflict",
"冲突",
"唯一约束"
],
"children": [],
"export": [
"score.json"
],
"keywords_must": [
"conflict",
"冲突"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "score.md",
"notebook_enable": false,
"exercise_id": "a4ef62fde91d4f558c6107172ba4f736"
}
\ No newline at end of file
# 积分累计
现有一个表
```postgresql
create table book_in(
login integer primary key ,
score integer default 0
);
```
用于记录用户行为积分,不同的客户端分布式的异步提交各个login的行为得分,如果一个用户不在表中,我们
需要插入一条新纪录,如果已经存在,则需要将新提交的score累加到表中,下列哪个查询可以实现此功能?
## 答案
```postgresql
insert into book_in(login, score) values ($1, $2) on conflict do update set score=$2;
```
## 选项
### A
```postgresql
try
insert into book_in(login, score) values ($1, $2);
catch
update book_in set score = $2 where login = $1
finally
commit;
```
### B
```postgresql
insert into book_in(login, score) select $1, $2;
```
### C
```postgresql
replace book_in(login, score) select $1, $2;
```
### D
```postgresql
upsert into book_in(login, score) select $1, $2;
```
{
"node_id": "",
"keywords": [
"事务",
"transaction"
],
"children": [],
"export": [
"transaction.json"
],
"keywords_must": [
"事务",
"transaction"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "transaction.md",
"notebook_enable": false,
"exercise_id": "bce8976b508942bdbe3bd5b6315e7583"
}
\ No newline at end of file
# 事务
现有 test1 表如下
```postgresql
create table test1(a integer primary key );
```
我们执行下面的语句
```postgresql
CREATE PROCEDURE transaction_test1()
LANGUAGE plpgsql
AS $$
BEGIN
FOR i IN 0..9 LOOP
INSERT INTO test1 (a) VALUES (i);
IF i % 2 = 0 THEN
COMMIT;
ELSE
ROLLBACK;
END IF;
END LOOP;
END;
$$;
CALL transaction_test1();
```
时,下面哪一项的解释是对的?
## 答案
每当循环迭代到偶数的时候提交插入,最终 test1 表中是`0,2,4,6,8`
## 选项
### A
以最后一次提交为准,最终 test1 表中是 0 到 9 共九个数字。
### B
所有插入都回滚了。test1 表中没有数据。
### C
这个过程执行会报错。
\ No newline at end of file
{
"node_id": "",
"keywords": [
"索引",
"index",
"performance",
"优化"
],
"children": [],
"export": [
"daily_payment.json",
"daily_payment_2.json"
],
"keywords_must": [
"索引",
"index",
"performance",
"优化"
],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "daily_payment.md",
"notebook_enable": false,
"exercise_id": "5e355cc9197a4d02a54f045301c88ace"
}
\ No newline at end of file
# 每日报表
数据分析组需要经常执行以下查询生成从昨天到之前某一天的交易量统计
```postgresql
select payment_date::date as day, sum(amount)
from payment
where (payment_date::date) between $1 and 'yesterday'::date
group by payment_date::date;
```
现在这个查询很慢,payment 表的信息如下:
```text
Table "public.payment"
Column | Type | Collation | Nullable | Default
--------------+-----------------------------+-----------+----------+---------------------------------------------
payment_id | integer | | not null | nextval('payment_payment_id_seq'::regclass)
customer_id | smallint | | not null |
staff_id | smallint | | not null |
rental_id | integer | | not null |
amount | numeric(5,2) | | not null |
payment_date | timestamp without time zone | | not null |
Indexes:
"payment_pkey" PRIMARY KEY, btree (payment_id)
```
应该如何优化这个查询?
## 答案
建立表达式索引
```postgresql
create index on payment((payment_date::date));
```
## 选项
### 对性能优化并无帮助
建立视图
```postgresql
create view view_daily_payment as select payment_date::date as day, amount from payment;
```
然后在视图 view_daily_payment 上执行
```postgresql
select day, sum(amount)
from view_daily_payment
where day between $1 and 'yesterday'::date
group by day
```
### 对该查询帮助不大
在 payment_date 列上建立索引
```postgresql
create index on payment(payment_date);
```
### 可以简化语句,但对优化没有帮助
建立计算列
```postgresql
alter table payment add day date generated always as ( payment_date::date ) stored
```
然后使用它改写查询
```postgresql
select day as day, sum(amount)
from payment
where day between $1 and 'yesterday'::date
group by day;
```
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "daily_payment_2.md",
"notebook_enable": false,
"exercise_id": "9295152720b74c65a0236cec175f6dce"
}
\ No newline at end of file
# 每日报表优化
# 每日报表
统计过去一段时间的数据,你发现 payment 表上每天的订单量很大,下面这个查询的统计过程占用了大多数数据库资源,查询不会
早止当天,总是在历史上某一个日期段内查询
```postgresql
select payment_date::date as day, sum(amount)
from payment
where (payment_date::date) between $1 and $2
group by payment_date::date;
```
payment 表的信息如下:
```text
Table "public.payment"
Column | Type | Collation | Nullable | Default
--------------+-----------------------------+-----------+----------+---------------------------------------------
payment_id | integer | | not null | nextval('payment_payment_id_seq'::regclass)
customer_id | smallint | | not null |
staff_id | smallint | | not null |
rental_id | integer | | not null |
amount | numeric(5,2) | | not null |
payment_date | timestamp without time zone | | not null |
Indexes:
"payment_pkey" PRIMARY KEY, btree (payment_id)
```
怎样做是最有效的?
## 答案
建立物化视图并建立索引。
```postgresql
create materialized view view_daily_payment as
select payment_date::date as day, sum(amount) as amount from payment group by day;
create index on view_daily_payment(day);
```
使用
```postgresql
select day, amount from view_daily_payment where day between $1 and $2;
```
进行查询。并且每天定时执行一次刷新命令
```postgresql
REFRESH MATERIALIZED VIEW view_daily_payment;
```
## 选项
### 不会优化 sum 和 group by
在 payment_date 列上建立索引
```postgresql
create index on payment(payment_date);
```
### 不会优化 sum
建立计算列
```postgresql
alter table payment add day date generated always as ( payment_date::date ) stored
```
然后使用它改写查询
```postgresql
select day as day, sum(amount)
from payment
where day between $1 and 'yesterday'::date
group by day;
```
### 优化了日期查询,但是不会优化统计过程
建立表达式索引
```postgresql
create index on payment((payment_date::date));
```
### 不做物化,对查询速度不会有显著改善
建立视图
```postgresql
create view view_daily_payment as select payment_date::date as day, amount from payment;
```
然后在视图 view_daily_payment 上执行
```postgresql
select day, sum(amount)
from view_daily_payment
where day between $1 and 'yesterday'::date
group by day
```
\ No newline at end of file
{
"node_id": "",
"keywords": [],
"keywords_must": [],
"keywords_forbid": []
}
\ No newline at end of file
{
"node_id": "",
"keywords": [
"数据库问答"
],
"keywords_must": [
"数据库问答"
],
"keywords_forbid": [],
"children": [],
"export": [
"qa.json"
]
}
\ No newline at end of file
{
"author": "huanhuilong",
"source": "qa.md",
"depends": [],
"type": "task_qa",
"task_link": "https://ask.csdn.net/channel/20/tag/300001",
"notebook_enable": false,
"exercise_id": "6798dddec5654cee9182e8aedfe70c2d"
}
\ No newline at end of file
# 请你在问答社区回答5个数据库问题并获得采纳,证明你的能力
数据库用户每天会产生很多实际使用的问题。
任务一:请你回答5个用户提出的 [数据库问题](https://ask.csdn.net/channel/20/tag/300001),并获得采纳,证明你的能力。
任务二:阅读以下数据库问题,请问对数据库问答描述错误的选项是?
* [数据库问题一](https://ask.csdn.net/questions/648654)
* [数据库问题二](https://ask.csdn.net/questions/675764)
* [数据库问题三](https://ask.csdn.net/questions/691021)
## 答案
```bash
【数据库问题二】里的问题,使用 PostgreSQL 无法解决
```
## 选项
### A
```bash
一个好的提问包含这5个步骤中的必要部分:
1. 问题遇到的现象和发生背景
2. 问题相关代码,请勿粘贴截图
3. 运行结果及报错内容
4. 我的解答思路和尝试过的方法
5. 我想要达到的结果
```
### B
```bash
【数据库问题一】里的问题,使用 PostgreSQL 也可以解决
```
### C
```bash
【数据库问题三】里的问题,使用 PostgreSQL 也可以解决
```
{
"node_id": "",
"keywords": [],
"keywords_must": [],
"keywords_forbid": []
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册