提交 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
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册