提交 4d43a73a 编写于 作者: M Mars Liu

fly on exercises

上级 24fc16f5
......@@ -2,5 +2,6 @@
"type": "code_options",
"author": "刘鑫",
"source": "rds.md",
"notebook_enable": false
"notebook_enable": false,
"exercise_id": "6b04760cd212469c925eb4a9c4a66757"
}
\ No newline at end of file
......@@ -2,5 +2,5 @@
"node_id": "pg-eeb777ca54434480a06bfe7fc5e7d3ca",
"keywords": [],
"children": [],
"export": []
"export": ["primary_key.json","unique.json", "unique_2.json"]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"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": "刘鑫",
"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": "刘鑫",
"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
......@@ -2,5 +2,5 @@
"node_id": "pg-a0cb78d68d814f5e935b41922b88e085",
"keywords": [],
"children": [],
"export": []
"export": ["trigger.json"]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"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
{
"type": "code_options",
"author": "刘鑫",
"source": "extension.md",
"notebook_enable": false
}
\ 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": "刘鑫",
"source": "language.md",
"notebook_enable": false
}
\ 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
{
"type": "code_options",
"author": "刘鑫",
"source": "salary.md",
"notebook_enable": false
}
\ 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);
```
### 结构错误
### group by 不对
```postgresql
select id, name, dept, max(salary) from employee where salary=max(salary) group by dept;
```
......@@ -2,5 +2,5 @@
"node_id": "pg-55fd213f919d411c9b572241c4bb7807",
"keywords": [],
"children": [],
"export": []
"export": ["subquery.json"]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "subquery.md",
"notebook_enable": false
}
\ 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')
```
......@@ -2,5 +2,5 @@
"node_id": "pg-e1cd59819dd74a0bacc707f7863aad53",
"keywords": [],
"children": [],
"export": []
"export": ["insert.json", "update.json", "delete.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,5 @@
"node_id": "pg-eb4048b7dfd3469f8049330ba78427b5",
"keywords": [],
"children": [],
"export": []
"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-693a81467f444886a199bac681f238b8",
"keywords": [],
"children": [],
"export": ["pivot.json"]
}
\ 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
......@@ -2,5 +2,5 @@
"node_id": "pg-6ce3520f7a67494a90378e7b194f8720",
"keywords": [],
"children": [],
"export": []
"export": ["score.json"]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "score.md",
"notebook_enable": false
}
\ 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;
```
......@@ -2,5 +2,5 @@
"node_id": "pg-2d547e778fc4453b84feb0a0c6341348",
"keywords": [],
"children": [],
"export": []
"export": ["transaction.json"]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "transaction.md",
"notebook_enable": false
}
\ 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
......@@ -313,6 +313,13 @@
"children": []
}
},
{
"透视表 ": {
"node_id": "pg-693a81467f444886a199bac681f238b8",
"keywords": [],
"children": []
}
},
{
"写入和冲突": {
"node_id": "pg-6ce3520f7a67494a90378e7b194f8720",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册