提交 20c1d264 编写于 作者: M Mars Liu

fly on exercises

上级 6f9c9905
{
"node_id": "pg-ee5c6d4a073b45a7995ba351ab9de46e",
"keywords": [],
"children": [],
"export": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"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": "pg-dca8da7637f94ded91a1871daa51746e",
"keywords": ["语法", "select"],
"children": [],
"export": ["basic.json"]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "concept.md",
"notebook_enable": false,
"exercise_id": "88b033cd97c642ef9900e5e091e335ff"
}
\ 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": "pg-b0f366abe12e41b0a9672d317e556662",
"keywords": ["DML", "insert", "update", "delete"],
"children": [],
"export": [
"insert.json",
"update.json",
"delete.json",
"concept.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"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": "刘鑫",
"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": "刘鑫",
"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": "pg-eb4048b7dfd3469f8049330ba78427b5",
"keywords": [],
"children": [],
"export": ["loop.json"]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"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
{
"node_id": "pg-0378bcd60ccd4dfebcfead92abbdd673",
"keywords": [],
"children": [],
"export": []
}
\ No newline at end of file
{
"node_id": "pg-3bbe12c0a19d4cb7a61dc0a20624ed89",
"keywords": [],
"children": [],
"export": []
}
\ No newline at end of file
{
"node_id": "pg-69ad784e39c044d7a115cfd124e49ef1",
"keywords": [],
"children": [],
"export": []
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "json.md",
"notebook_enable": false
}
\ 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 与其它类型一样,不能部分修改,只能作为整体插入或更新
# 匹配
表 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');
```
# 标签
现有一个表 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": "pg-6dbbb44b41d347b58075a9155dfb8356",
"keywords": [],
"children": [],
"export": []
}
\ 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": "pg-532f8f84ea74493a9581e6bf720ebf78",
"keywords": [],
"children": [],
"export": []
}
\ 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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册