提交 6f9c9905 编写于 作者: M Mars Liu

fly on exercises

上级 a796fb05
{
"type": "code_options",
"author": "刘鑫",
"source": "server.md",
"notebook_enable": false,
"exercise_id": "88b033cd97c642ef9900e5e091e335ff"
}
\ No newline at end of file
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
"type": "code_options", "type": "code_options",
"author": "刘鑫", "author": "刘鑫",
"source": "develop.md", "source": "develop.md",
"notebook_enable": false "notebook_enable": false,
"exercise_id": "257414ca11f24a35a6270166b6bd2830"
} }
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "basic.md",
"notebook_enable": false
}
\ 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
# 基本概念
下列叙述中,正确的是
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;
```
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
"type": "code_options", "type": "code_options",
"author": "刘鑫", "author": "刘鑫",
"source": "subquery.md", "source": "subquery.md",
"notebook_enable": false "notebook_enable": false,
"exercise_id": "82be2aa951304dc9869cd7177fcba99f"
} }
\ No newline at end of file
{
"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
}
\ 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
{
"type": "code_options",
"author": "刘鑫",
"source": "pivot.md",
"notebook_enable": false,
"exercise_id": "88b033cd97c642ef9900e5e091e335ff"
}
\ No newline at end of file
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
"type": "code_options", "type": "code_options",
"author": "刘鑫", "author": "刘鑫",
"source": "score.md", "source": "score.md",
"notebook_enable": false "notebook_enable": false,
"exercise_id": "a4ef62fde91d4f558c6107172ba4f736"
} }
\ No newline at end of file
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
"type": "code_options", "type": "code_options",
"author": "刘鑫", "author": "刘鑫",
"source": "transaction.md", "source": "transaction.md",
"notebook_enable": false "notebook_enable": false,
"exercise_id": "bce8976b508942bdbe3bd5b6315e7583"
} }
\ No newline at end of file
{ {
"pg": { "pg": {
"node_id": "pg-4544619de2a54841930282c700ba92b8", "node_id": "pg-d8b0b9aded4c4cc0b2085d6a7c5611c7",
"keywords": [], "keywords": [],
"children": [ "children": [
{ {
"PostgreSQL初阶": { "PostgreSQL初阶": {
"node_id": "pg-6089aef54c4c4ce7bc85b4dfa14f44cd", "node_id": "pg-f1c160affc45417a9c5fff7dc4be448a",
"keywords": [], "keywords": [],
"children": [ "children": [
{ {
"PostgreSQL基本概念": { "PostgreSQL基本概念": {
"node_id": "pg-b710bd5c63fb4651bebfdd9b3fdb17a5", "node_id": "pg-1aad17890736469a8b2f61a36dfe41db",
"keywords": [], "keywords": [],
"children": [ "children": [
{ {
"关系型数据库": { "关系型数据库": {
"node_id": "pg-6920a4f2ba864459bd8883a7a48fa238", "node_id": "pg-11d131b2d01b40be90b23f096d4852cc",
"keywords": [], "keywords": [],
"children": [] "children": []
} }
}, },
{ {
"服务器和客户端": { "服务器和客户端": {
"node_id": "pg-40374311856c4ad3b76fff16ecbe9dce", "node_id": "pg-cf0e5cb243a247e2a0b812c5798cdba4",
"keywords": [], "keywords": [],
"children": [] "children": []
} }
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
}, },
{ {
"PostgreSQL的安装": { "PostgreSQL的安装": {
"node_id": "pg-3ac4a2bf31af423fb9e2ec9fe30024c5", "node_id": "pg-d95edf2d942b45d781e112be785ce260",
"keywords": [], "keywords": [],
"children": [ "children": [
{ {
...@@ -56,7 +56,10 @@ ...@@ -56,7 +56,10 @@
{ {
"使用PostgreSQL": { "使用PostgreSQL": {
"node_id": "pg-c252ff4b0e7f4163837c9b6f81678505", "node_id": "pg-c252ff4b0e7f4163837c9b6f81678505",
"keywords": [], "keywords": [
"login",
"connect"
],
"children": [] "children": []
} }
} }
...@@ -65,34 +68,35 @@ ...@@ -65,34 +68,35 @@
}, },
{ {
"查询数据": { "查询数据": {
"node_id": "pg-7e2e52dbddb44d5bad6deea6c5e57198", "node_id": "pg-30f905f73a574151994d6d7f58d8a20d",
"keywords": [], "keywords": [],
"children": [ "children": [
{ {
"SQL基本语法": { "psql": {
"node_id": "pg-dca8da7637f94ded91a1871daa51746e", "node_id": "pg-ee5c6d4a073b45a7995ba351ab9de46e",
"keywords": [], "keywords": [],
"children": [] "children": []
} }
}, },
{ {
"SQL、DML和DDL": { "SQL基本语法": {
"node_id": "pg-b0f366abe12e41b0a9672d317e556662", "node_id": "pg-dca8da7637f94ded91a1871daa51746e",
"keywords": [], "keywords": [
"children": [] "语法",
} "select"
}, ],
{
"PostgreSQL的数据类型": {
"node_id": "pg-1eae8a80615f436081e71b36d89fb0aa",
"keywords": [],
"children": [] "children": []
} }
}, },
{ {
"函数调用": { "SQL、DML和DDL": {
"node_id": "pg-ebef9fceb3684b119a9ba221a81f304b", "node_id": "pg-b0f366abe12e41b0a9672d317e556662",
"keywords": [], "keywords": [
"DML",
"insert",
"update",
"delete"
],
"children": [] "children": []
} }
} }
...@@ -104,7 +108,7 @@ ...@@ -104,7 +108,7 @@
}, },
{ {
"PostgreSQL中阶": { "PostgreSQL中阶": {
"node_id": "pg-c360138dad92431594130596c5c91394", "node_id": "pg-fc32585f4136497ea9be2d057d4aeada",
"keywords": [], "keywords": [],
"children": [ "children": [
{ {
...@@ -197,13 +201,6 @@ ...@@ -197,13 +201,6 @@
"children": [] "children": []
} }
}, },
{
"DML": {
"node_id": "pg-e1cd59819dd74a0bacc707f7863aad53",
"keywords": [],
"children": []
}
},
{ {
"过程化编程": { "过程化编程": {
"node_id": "pg-eb4048b7dfd3469f8049330ba78427b5", "node_id": "pg-eb4048b7dfd3469f8049330ba78427b5",
...@@ -226,7 +223,7 @@ ...@@ -226,7 +223,7 @@
}, },
{ {
"PostgreSQL高阶": { "PostgreSQL高阶": {
"node_id": "pg-2f46da8212cf41ad9c0d2a5bf1330adc", "node_id": "pg-c9c7629c52064d8780cbd4dc14dbe351",
"keywords": [], "keywords": [],
"children": [ "children": [
{ {
...@@ -263,13 +260,6 @@ ...@@ -263,13 +260,6 @@
"node_id": "pg-f80410c703044bfea61b2f3a4fbc5705", "node_id": "pg-f80410c703044bfea61b2f3a4fbc5705",
"keywords": [], "keywords": [],
"children": [ "children": [
{
"自定义类型": {
"node_id": "pg-ec4bbb39e76949b48b13f04cc8c16532",
"keywords": [],
"children": []
}
},
{ {
"几何类型和GIS": { "几何类型和GIS": {
"node_id": "pg-3bbe12c0a19d4cb7a61dc0a20624ed89", "node_id": "pg-3bbe12c0a19d4cb7a61dc0a20624ed89",
...@@ -340,20 +330,6 @@ ...@@ -340,20 +330,6 @@
"keywords": [], "keywords": [],
"children": [] "children": []
} }
},
{
"表函数": {
"node_id": "pg-e4b00736ab4c483785c022a0a464f30a",
"keywords": [],
"children": []
}
},
{
"结果集类型": {
"node_id": "pg-0e280b203b524a28ab8587aac125a865",
"keywords": [],
"children": []
}
} }
] ]
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册