提交 3e4651af 编写于 作者: M Mars Liu

having

上级 1b5a3d67
# 授权
管理员要给用户 fred 授权,允许他查询 emplyee 表,应用哪一条语句?
管理员要给用户 joe 授权,允许他查询 emplyee 表,应用哪一条语句?
## 答案
```postgresql
grant select on table employee to fred;
```mysql
grant select on table employee to joe;
```
## 选项
### 权限名错误
```postgresql
grant query on table employee to fred;```
```mysql
grant query on table employee to joe;
```
### 权限名错误
```postgresql
grant read on table employee to fred;```
```mysql
grant read on table employee to joe;
```
### 操作关键词错误
```postgresql
grant select on table employee of fred;```
```mysql
grant select on table employee of joe;
```
### 操作错误
```postgresql
grant select on table employee.* of fred;```
```mysql
grant select on table employee.* of joe;
```
### 权限过高
```postgresql
grant all on table employee to fred;```
```mysql
grant all on table employee to joe;
```
......@@ -5,7 +5,7 @@
## 答案
```postgresql
```mysql
revoke select on trade from fred;
```
......@@ -13,19 +13,19 @@ revoke select on trade from fred;
### 操作错误
```postgresql
```mysql
grant not select on trade to fred;
```
### 操作关键字错误
```postgresql
```mysql
revoke select on trade to fred;
```
### 指定权限错误
```postgresql
```mysql
revoke owned trade from fred;
```
......
# 角色
你是 rental dvd 公司的数据库管理员,公司数据分析组有 Fred、Alice、James、Jone 四位成员,现在你需要给数据分析组授权,允许他们
查询 trade 数据库的 public schema 中的所有表,规范的操作应该是
Joe 现在是团队的 DBA,公司数据分析组有 Fred、Alice、James、Jone 四位成员,现在Joe需要给数据分析组授权,允许他们
查询 goods 数据库中的所有表,*规范*的操作应该是
## 答案
```postgresql
```mysql
create role analysis;
grant analysis to fred, alice, james, jone;
grant select on all tables in schema public to analysis;
grant select on goods.* to analysis;
flush privileges;
```
## 选项
### 将来人员变动管理会很繁琐
```postgresql
grant select on all tables in schema public to fred, alice, james, jone;
```mysql
grant select on goods.* to fred, alice, james, jone;
```
### 过度授权
### 错误的语法
```postgresql
```mysql
create role analysis;
grant analysis to fred, alice, james, jone;
grant all on all tables in schema public to analysis;
grant all on all tables in schema goods to analysis;
flush privileges;
```
### 语句不完整
### 过度授权
```postgresql
```mysql
create role analysis;
grant analysis to fred, alice, james, jone;
grant select on all to analysis;
grant select on *.* to analysis;
flush privileges ;
```
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "serial.md",
"source": "auto_increment.md",
"notebook_enable": false,
"exercise_id": "ac5111c4826b48659e6ba60aa614706d"
}
\ No newline at end of file
# 自增序列
关于 MySQL 的自增字段,错误的说法是
## 答案
自增字段必须名为 id。
## 选项
### 自增字段只能主键,每个表至多只能有一个
自增字段必须是主键。
### B
插入操作失败,自增计数仍然会被递增,下次操作使用下一个整数。
### C
自增字段默认从 1 开始。
......@@ -8,7 +8,7 @@
"export": [
"table.json",
"create_table.json",
"serial.json"
"auto_increment.json"
],
"keywords_must": [
"表",
......
# 建表语句
现在我们尝试建立一个简化的交易流水表 trade,需要一个自增主键,一个content字段保存订单详情,,
现在Joe 需要建立一个简化的交易流水表 trade,需要一个自增主键,一个content字段保存订单详情,,
需要有一个时间戳字段记录订单入库时间,那么哪一个语句是对的?
## 答案
```postgresql
```mysql
create table trade (
id serial primary key,
content text,
id int primary key auto_increment,
content varchar(8000),
created_at timestamp default now()
);
```
......@@ -17,30 +17,30 @@ create table trade (
### 主键没有设置自增,不符合题意
```postgresql
```mysql
create table trade (
id integer primary key,
content text,
content varchar(8000),
created_at timestamp default now()
);
```
### 时间戳没有设置默认值
```postgresql
```mysql
create table trade (
id serial primary key,
content text,
content varchar(8000),
created_at timestamp
);
```
### 没有主键,不符合题设
```postgresql
```mysql
create table trade (
id serial,
content text,
content varchar(8000),
created_at timestamp default now()
);
```
\ No newline at end of file
# 自增序列
PostgreSQL 表中的自增列 serial 的底层实现机制是:
## 答案
绑定 sequence 对象的表达式默认值
## 选项
### PG采用的是独立的序列器
auto increment 计数器
### B
触发器
### C
系统表
### D
Serial 文件
{
"node_id": "mysql-98e926730d844f238dc9cd8ac5f65126",
"keywords": ["view"],
"keywords": [
"view"
],
"children": [],
"export": [],
"export": [
"view.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
......
{
"type": "code_options",
"author": "ccat",
"source": "function.md",
"source": "view.md",
"notebook_enable": false,
"exercise_id": "65ba04e19bad4538a4af9b3da275fe68"
"exercise_id": "0d485e3ba6e448edbe727da0f1cf8685"
}
\ No newline at end of file
# 视图
Joe 需要给 goods 表
```mysql
create table goods(
id int primary key auto_increment,
category_id int,
category varchar(64),
name varchar(256),
price decimal(12, 4),
stock int,
upper_time timestamp
)
```
添加一个视图,仅展示价格超过 1000 的商品价格和名称,下列选项中正确的是:
## 答案
```mysql
CREATE VIEW view_name_price
AS
SELECT name, price
FROM goods
WHERE price > 1000;
```
## 选项
### A
```mysql
CREATE VIEW view_name_price
AS
SELECT name, price
FROM goods;
```
### B
```mysql
CREATE VIEW view_name_price
AS
SELECT *
FROM goods
WHERE price > 1000;
```
### C
```mysql
CREATE VIEW view_name_price
AS
BEGIN
SELECT name, price
FROM goods
WHERE price > 1000;
END;
```
### D
```mysql
CREATE VIEW view_name_price
AS
BEGIN
SELECT name, price
FROM goods
WHERE price > 1000;
END;
```
{
"node_id": "mysql-a6b27f219f3c47c981ed1dceffa8a1a6",
"node_id": "mysql-98e926730d844f238dc9cd8ac5f65126",
"keywords": [
"函数",
"function"
"produce", "function"
],
"children": [],
"export": [
"function.json"
],
"keywords_must": [
"函数",
"function"
],
"keywords_forbid": []
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ No newline at end of file
# 函数
关于 PostgreSQL 函数,错误的是:
## 答案
函数必须是无副作用的
## 选项
### A
函数可以用 SQL 写,也可以用 PLPGSQL,还可以用 Python、Perl、LUA等语言。
### B
函数的参数和返回值可以是简单变量,也可以是结果集或自定义类型
### C
函数可以递归引用
### D
函数之间可以互相引用
### E
函数的使用权限可以通过 grant/revoke/deny 管理
# 主键
关于 PostgreSQL 的主键,哪一项是错误的?
关于 MySQL 的主键,哪一项是错误的?
## 答案
......
......@@ -2,13 +2,12 @@
现有一个图书登记表:
```postgresql
```mysql
create table book(
id serial primary key ,
id int primary key auto_increment,
title text,
publish_at date,
isbn text,
meta jsonb default '{}'::jsonb
isbn text
)
```
......@@ -16,7 +15,7 @@ create table book(
1. 删除id列,将isbn设置为主键
2. 在 isbn 列上加唯一约束
3. 执行 `create index on book(id, title, publish_at, isbn, meta)`
3. 执行 `create index on book(id, title, publish_at, isbn)`
4. 在 id 键上加唯一约束
## 答案
......
......@@ -2,13 +2,12 @@
现有一个图书登记表:
```postgresql
```mysql
create table book(
id serial primary key ,
id int primary key auto_increment,
title text,
publish_at date,
isbn text,
meta jsonb default '{}'::jsonb
isbn text
)
```
......
{
"type": "code_options",
"author": "ccat",
"source": "aliases.md",
"notebook_enable": false,
"exercise_id": "7a6d06703d1448209e9d82074d1887fb"
}
\ No newline at end of file
# 别名
关于别名,以下说法中正确的是:
1. 查询集(表或子查询)可以指定别名
2. 查询集的列可以指定别名
3. 别名只能用合法的变量名,即字母开头,只由英文数字、字母和下划线组成
4. 可以用双引号或反引号将别名包围起来
## 答案
```
1, 2, 4
```
## 选项
### A
全部都对
### B
全都不对
### C
```
2, 4
```
### D
```
2, 3, 4
```
### E
````
3, 4
````
\ No newline at end of file
......@@ -2,7 +2,9 @@
"node_id": "mysql-1d350c6226d443bdb76b5058d8ee23e7",
"keywords": [],
"children": [],
"export": [],
"export": [
"aliases.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
......
{
"type": "code_options",
"author": "ccat",
"source": "between.md",
"notebook_enable": false,
"exercise_id": "6ad1d6bd4fa7421890b7498b5648b4e0"
}
\ No newline at end of file
# Between
Joe 要查询 goods 表
```mysql
create table goods(
id int primary key auto_increment,
category_id int,
category varchar(64),
name varchar(256),
price decimal(12, 4),
stock int,
upper_time timestamp
)
```
中价格在 1000 到 2000 之间(包含1000和2000)的数据,以下查询中错误的是:
## 答案
```mysql
SELECT * FROM goods HAVING price BETWEEN 1000 AND 2000;
```
## 选项
### A
```mysql
SELECT * FROM goods WHERE price BETWEEN 1000 AND 2000;
```
### B
```mysql
SELECT * FROM goods WHERE price >= 1000 AND price <= 2000;
```
### C
```mysql
SELECT * FROM goods WHERE not (price < 1000 or price > 2000);
```
......@@ -2,7 +2,9 @@
"node_id": "mysql-2296dbe96d584a52bd28a3ad5f655518",
"keywords": [],
"children": [],
"export": [],
"export": [
"between.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
......
{
"node_id": "mysql-8e6cd4d5f4b446a2bc3f5402de9bd49c",
"keywords": [],
"keywords": ["case", "pivot", "透视表"],
"children": [],
"export": [],
"export": [
"pivot.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
......
{
"type": "code_options",
"author": "ccat",
"source": "pivot.md",
"notebook_enable": false,
"exercise_id": "d61198987832488ab53e8a18f7337946"
}
\ No newline at end of file
# 透视表
现有销售记录表
```mysql
create table sales(
id serial primary key ,
sku_id integer not null ,
amount decimal(12, 4),
created_at timestamp default now()
);
create index idx_created_at on sales(created_at);
```
现在我们希望对这个表做一个月度的透视汇总,得到2021年每个月每种商品(sku_id)的的销售总额,每个月一列,哪一项可以实现?
## 答案
```mysql
select sku_id,
sum(case extract(month from created_at) when 1 then amount else 0 end) as Jan,
sum(case extract(month from created_at) when 2 then amount else 0 end) as Feb,
sum(case extract(month from created_at) when 3 then amount else 0 end) as Mar,
sum(case extract(month from created_at) when 4 then amount else 0 end) as Apr,
sum(case extract(month from created_at) when 5 then amount else 0 end) as May,
sum(case extract(month from created_at) when 6 then amount else 0 end) as June,
sum(case extract(month from created_at) when 7 then amount else 0 end) as July,
sum(case extract(month from created_at) when 8 then amount else 0 end) as Aug,
sum(case extract(month from created_at) when 9 then amount else 0 end) as Sept,
sum(case extract(month from created_at) when 10 then amount else 0 end) as Oct,
sum(case extract(month from created_at) when 11 then amount else 0 end) as Nov,
sum(case extract(month from created_at) when 12 then amount else 0 end) as Dec
from sales
where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp
group by sku_id;
```
## 选项
### 格式不相符
```mysql
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;
```
### 计算逻辑错误
```mysql
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);
```
### 计算格式错误
```mysql
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": "mysql-153ce0dadc824af98de199f193c8c75c",
"keywords": ["distinct"],
"keywords": [
"distinct"
],
"children": [],
"export": [],
"export": [
"distinct.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
......
{
"type": "code_options",
"author": "ccat",
"source": "distinct.md",
"notebook_enable": false,
"exercise_id": "331f369b150340f9989ccea8abfcd42f"
}
\ No newline at end of file
# Distinct
Joe 想统计以下 goods 表
```mysql
create table goods(
id int primary key auto_increment,
category_id int,
category varchar(64),
name varchar(256),
price decimal(12, 4),
stock int,
upper_time timestamp
)
```
中的商品有多少种不同的价格,他应该如何写这条查询?
## 答案
```mysql
select count(distinct price) from goods;
```
## 选项
### A
```mysql
select count(distinct *) from goods;
```
### B
```mysql
select distinct count(price) from goods;
```
### C
```mysql
select count(price) from goods;
```
### D
```mysql
select distinct price from goods;
```
......@@ -2,7 +2,9 @@
"node_id": "mysql-fa20a81805b44975aed265dd058c542a",
"keywords": [],
"children": [],
"export": [],
"export": [
"order_by.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
......
{
"type": "code_options",
"author": "ccat",
"source": "order_by.md",
"notebook_enable": false,
"exercise_id": "68e526b5717246a190e6bb06ad3e58ac"
}
\ No newline at end of file
# 排序
Joe 需要根据员工表
```mysql
create table employee
(
id serial primary key,
name varchar(256),
dept varchar(256),
salary decimal(12, 4)
);
```
生成一份报表,首先按部门名称的字典序排序,部门内部按员工工资从高到低排列,那么这个查询应该是:
## 答案
```mysql
select id, name, dept, salary from employee order by dept, salary desc;
```
## 选项
### A
```mysql
select id, name, dept, salary from employee order by dept, salary;
```
### B
```mysql
select id, name, dept, salary from employee order by dept desc, salary desc;
```
### C
```mysql
select id, name, dept, salary from employee order by dept and salary desc;
```
### D
```mysql
select id, name, dept, salary from employee order by dept, salary, id, name;
```
### E
```mysql
select id, name, dept, salary from employee order by dept, salary desc;
```
......@@ -3,5 +3,5 @@
"author": "ccat",
"source": "paged.md",
"notebook_enable": false,
"exercise_id": "68e526b5717246a190e6bb06ad3e58ac"
"exercise_id": "b80577df526846bb8eccf638480a0170"
}
\ No newline at end of file
......@@ -2,7 +2,7 @@
我们有如下订单表:
```postgresql
```mysql
create table orders
(
id serial primary key,
......@@ -17,7 +17,7 @@ create table orders
## 答案
```postgresql
```mysql
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1
......@@ -28,7 +28,7 @@ offset $2 limit 100;
### 缺少 limit
```postgresql
```mysql
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1
......@@ -37,7 +37,7 @@ offset $2;
### 缺少 offset
```postgresql
```mysql
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1;
......@@ -45,7 +45,7 @@ where date = $1;
### 结构不对
```postgresql
```mysql
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1 and
......
......@@ -2,7 +2,9 @@
"node_id": "mysql-3b85e53dd88146798d21b7254ad85cae",
"keywords": [],
"children": [],
"export": [],
"export": [
"having.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
......
{
"type": "code_options",
"author": "ccat",
"source": "having.md",
"notebook_enable": false,
"exercise_id": "2e74213549474ee680f4b72cde1a5b45"
}
\ No newline at end of file
# Having
Joe 要从 employee 表
```mysql
create table employee
(
id serial primary key,
name varchar(256),
dept varchar(256),
salary decimal(12, 4)
);
```
中得到每月工资开支超过十万的部门,这个查询应该怎么写?
## 答案
```mysql
select dept from employee group by dept having sum(salary) > 100000;
```
## 选项
### A
```mysql
select dept from employee group by dept where sum(salary) > 100000;
```
### B
```mysql
select dept from employee where sum(salary) > 100000 group by dept;
```
### C
```mysql
select dept from employee where sum(salary) > 100000 order by dept;
```
### D
```mysql
select dept from employee group by dept where sum(salary) > 100000;
```
......@@ -2,7 +2,9 @@
"node_id": "mysql-a4773004e0cf432aa7ccdf6b9490838f",
"keywords": [],
"children": [],
"export": [],
"export": [
"salary.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
......
......@@ -296,7 +296,10 @@
{
"mysqladmin": {
"node_id": "mysql-daca74b56aca48cea2da14078b518051",
"keywords": [],
"keywords": [
"mysqladmin",
"shell"
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
......@@ -305,7 +308,11 @@
{
"myisampack": {
"node_id": "mysql-b34e3244a8ba4166bd22bc4fe5f7d8db",
"keywords": [],
"keywords": [
"myisampack",
"myisam",
"pack"
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
......@@ -314,7 +321,11 @@
{
"mysqlbinlog": {
"node_id": "mysql-fe9caf0e07a94efc8b7c8f2a4c683efe",
"keywords": [],
"keywords": [
"mysqlbinlog",
"binlog",
"二进制日志"
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
......@@ -359,7 +370,12 @@
{
"mysqlimport": {
"node_id": "mysql-e782e10839d843c0ad36a05ae7d0366f",
"keywords": [],
"keywords": [
"mysqlimport",
"import",
"restore",
"导入"
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
......@@ -438,16 +454,13 @@
},
{
"存储过程和函数": {
"node_id": "mysql-a6b27f219f3c47c981ed1dceffa8a1a6",
"node_id": "mysql-98e926730d844f238dc9cd8ac5f65126",
"keywords": [
"函数",
"produce",
"function"
],
"children": [],
"keywords_must": [
"函数",
"function"
],
"keywords_must": [],
"keywords_forbid": []
}
},
......
create table employee
(
id serial primary key,
name varchar(256),
dept varchar(256),
salary decimal(12, 4)
);
\ No newline at end of file
create table sales(
id serial primary key ,
sku_id integer not null ,
amount decimal(12, 4),
created_at timestamp default now()
);
create index idx_created_at on sales(created_at);
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册