提交 ba97f6ea 编写于 作者: M Mars Liu

修正左连接题目错误;代码排版

上级 2778a741
...@@ -25,8 +25,13 @@ mysql mysql ...@@ -25,8 +25,13 @@ mysql mysql
``` ```
创建数据库用户 joe 并授权: 创建数据库用户 joe 并授权:
```sql ```sql
create user 'joe'@'localhost' identified by 'joe'; create user
grant all privileges on *.* to `joe`@`localhost`; 'joe'@'localhost'
identified by 'joe';
grant all privileges
on *.* to `joe`@`localhost`;
flush privileges ; flush privileges ;
``` ```
...@@ -41,7 +46,9 @@ mysql mysql ...@@ -41,7 +46,9 @@ mysql mysql
``` ```
为 joe 授权 为 joe 授权
```sql ```sql
grant all privileges on *.* to joe; grant all
privileges on *.* to joe;
flush privileges ; flush privileges ;
``` ```
...@@ -55,7 +62,9 @@ mysql mysql ...@@ -55,7 +62,9 @@ mysql mysql
为 joe 授权 为 joe 授权
```sql ```sql
grant all privileges on *.* to joe; grant all
privileges on *.* to joe;
flush privileges ; flush privileges ;
``` ```
...@@ -68,7 +77,10 @@ mysql mysql ...@@ -68,7 +77,10 @@ mysql mysql
创建数据库用户 joe 创建数据库用户 joe
```sql ```sql
create user 'joe'@'localhost' identified by 'joe'; create user
'joe'@'localhost'
identified by 'joe';
flush privileges ; flush privileges ;
``` ```
...@@ -81,7 +93,12 @@ mysql mysql ...@@ -81,7 +93,12 @@ mysql mysql
``` ```
创建数据库用户 joe 并授权: 创建数据库用户 joe 并授权:
```sql ```sql
create user 'joe'@'%' identified by 'joe'; create user
grant all privileges on *.* to joe; 'joe'@'%'
identified by 'joe';
grant all
privileges on *.* to joe;
flush privileges ; flush privileges ;
``` ```
{ {
"node_id": "mysql-fcca525ab0f04f16834ded9b2b3f38a4", "node_id": "mysql-fcca525ab0f04f16834ded9b2b3f38a4",
"keywords": ["insert", "插入"], "keywords": [
"insert",
"插入"
],
"children": [], "children": [],
"export": [ "export": [
"insert.json" "insert.json"
], ],
"keywords_must": [ "keywords_must": [
["mysql", "插入"] [
"mysql",
"插入"
]
], ],
"keywords_forbid": [], "keywords_forbid": [],
"group": 1 "group": 1
......
...@@ -26,8 +26,24 @@ create unique index idx_book_isbn on book(isbn); ...@@ -26,8 +26,24 @@ create unique index idx_book_isbn on book(isbn);
## 答案 ## 答案
```sql ```sql
insert into book(title, price, isbn, publish_at) select 'a book title', 25.4, 'xx-xxxx-xxxx', '2019-12-1'; insert into book(
insert into book(title, price, isbn, publish_at) select 'a other book title', 25.4, 'yy-yyyy-xxxx', '2019-12-1'; title,
price,
isbn,
publish_at)
select 'a book title',
25.4,
'xx-xxxx-xxxx',
'2019-12-1';
insert into book(title,
price,
isbn,
publish_at)
select 'a other book title',
25.4,
'yy-yyyy-xxxx',
'2019-12-1';
``` ```
## 选项 ## 选项
...@@ -35,27 +51,83 @@ insert into book(title, price, isbn, publish_at) select 'a other book title', 25 ...@@ -35,27 +51,83 @@ insert into book(title, price, isbn, publish_at) select 'a other book title', 25
### 唯一键冲突 ### 唯一键冲突
```sql ```sql
insert into book(title, price, isbn, publish_at) select 'a book title', 25.4, 'xx-xxxx-xxxx', '2019-12-1'; insert into book(title,
insert into book(title, price, isbn, publish_at) select 'a other book title', 35.4, 'xx-xxxx-xxxx', '2019-12-1'; price,
isbn,
publish_at)
select 'a book title',
25.4,
'xx-xxxx-xxxx',
'2019-12-1';
insert into book(title,
price,
isbn,
publish_at)
select 'a other book title',
35.4,
'xx-xxxx-xxxx',
'2019-12-1';
``` ```
### 缺少必要的列 ### 缺少必要的列
```sql ```sql
insert into book(price, isbn, publish_at) select 25.4, 'xx-xxxx-xxxx', '2019-12-1'; insert into book(price,
insert into book(price, isbn, publish_at) select 35.4, 'yy-yyyy-xxxx', '2019-12-1'; isbn,
publish_at)
select 25.4,
'xx-xxxx-xxxx',
'2019-12-1';
insert into book(price,
isbn,
publish_at)
select 35.4,
'yy-yyyy-xxxx',
'2019-12-1';
``` ```
### 类型错误 ### 类型错误
```sql ```sql
insert into book(title, price, isbn, publish_at) select 'a book title', 'unknown', 'xx-xxxx-xxxx', '2019-12-1'; insert into book(title,
insert into book(title, price, isbn, publish_at) select 'a other book title', 'unknown', 'xx-xxxx-xxxx', '2019-12-1'; price,
isbn,
publish_at)
select 'a book title',
'unknown',
'xx-xxxx-xxxx',
'2019-12-1';
insert into book(title,
price,
isbn,
publish_at)
select 'a other book title',
'unknown',
'xx-xxxx-xxxx',
'2019-12-1';
``` ```
### 违反非空约束 ### 违反非空约束
```sql ```sql
insert into book(title, price, isbn, publish_at) select null, 'unknown', 'xx-xxxx-xxxx', '2019-12-1'; insert into book(title,
insert into book(title, price, isbn, publish_at) select null, 'unknown', 'xx-xxxx-xxxx', '2019-12-1'; price,
isbn,
publish_at)
select null,
'unknown',
'xx-xxxx-xxxx',
'2019-12-1';
insert into book(title,
price,
isbn,
publish_at)
select null,
'unknown',
'xx-xxxx-xxxx',
'2019-12-1';
``` ```
...@@ -24,7 +24,9 @@ Joe 希望修改销售部(dept 字段为 sale)员工 Dora Muk 的工资,将其 ...@@ -24,7 +24,9 @@ Joe 希望修改销售部(dept 字段为 sale)员工 Dora Muk 的工资,将其
## 答案 ## 答案
```sql ```sql
update employee set salary = salary + 1000 where dept = 'sale' and name = 'Dora Muk'; update employee set
salary = salary + 1000
where dept = 'sale' and name = 'Dora Muk';
``` ```
## 选项 ## 选项
...@@ -32,19 +34,23 @@ update employee set salary = salary + 1000 where dept = 'sale' and name = 'Dora ...@@ -32,19 +34,23 @@ update employee set salary = salary + 1000 where dept = 'sale' and name = 'Dora
### 过滤条件不严谨 ### 过滤条件不严谨
```sql ```sql
update employee set salary = salary + 1000 where name = 'Dora Muk'; update employee set
salary = salary + 1000
where name = 'Dora Muk';
``` ```
### 缺少过滤条件 ### 缺少过滤条件
```sql ```sql
update employee set salary = salary + 1000; update employee set
salary = salary + 1000;
``` ```
### 错误的赋值语句 ### 错误的赋值语句
```sql ```sql
update employee set salary += 1000; update employee set
salary += 1000;
``` ```
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
```sql ```sql
create table orders ( create table orders (
id int primary key auto_increment, id int primary key auto_increment,
item_id int, item_id int,
amount int, amount int,
unit_price decimal(12, 4), unit_price decimal(12, 4),
price decimal(12, 4), price decimal(12, 4),
description varchar(2000), description varchar(2000),
ts timestamp default now(), ts timestamp default now(),
deal bool default false deal bool default false
); );
``` ```
...@@ -60,14 +60,14 @@ from orders; ...@@ -60,14 +60,14 @@ from orders;
```sql ```sql
drop table orders; drop table orders;
create table if not exists orders ( create table if not exists orders (
id int primary key auto_increment, id int primary key auto_increment,
item_id int, item_id int,
amount int, amount int,
unit_price decimal(12, 4), unit_price decimal(12, 4),
price decimal(12, 4), price decimal(12, 4),
description varchar(2000), description varchar(2000),
ts timestamp default now(), ts timestamp default now(),
deal bool default false deal bool default false
); );
``` ```
......
{ {
"node_id": "mysql-f5527eae7f3148108c92ff99a6d4ed4a", "node_id": "mysql-f5527eae7f3148108c92ff99a6d4ed4a",
"keywords": ["查询", "select"], "keywords": [
"查询",
"select"
],
"children": [], "children": [],
"export": [ "export": [
"select.json" "select.json"
], ],
"keywords_must": [ "keywords_must": [
["mysql", "select"] [
"mysql",
"select"
]
], ],
"keywords_forbid": [], "keywords_forbid": [],
"group": 1 "group": 1
......
...@@ -39,7 +39,9 @@ select * from employee where dept = 'hr'; ...@@ -39,7 +39,9 @@ select * from employee where dept = 'hr';
### D ### D
```sql ```sql
select id, name, dept, salary from employee where salary > 10000; select id, name, dept, salary
from employee
where salary > 10000;
``` ```
### E ### E
......
{ {
"node_id": "mysql-91cc9c73e58945d3ba654370a057a1c7", "node_id": "mysql-91cc9c73e58945d3ba654370a057a1c7",
"keywords": ["查询", "select", "where"], "keywords": [
"查询",
"select",
"where"
],
"children": [], "children": [],
"export": [ "export": [
"where.json" "where.json"
], ],
"keywords_must": [ "keywords_must": [
["mysql", "where"] [
"mysql",
"where"
]
], ],
"keywords_forbid": [], "keywords_forbid": [],
"group": 1 "group": 1
......
...@@ -40,7 +40,9 @@ where date(ts) = '2022-05-25' ...@@ -40,7 +40,9 @@ where date(ts) = '2022-05-25'
```sql ```sql
select id select id
from (select * from orders where date(ts) = '2022-05-25') as o from (select *
from orders
where date(ts) = '2022-05-25') as o
where unit_prise < 20; where unit_prise < 20;
``` ```
...@@ -58,7 +60,8 @@ where date(ts) = '2022-05-25' ...@@ -58,7 +60,8 @@ where date(ts) = '2022-05-25'
```sql ```sql
select id select id
from orders from orders
if date(ts) = '2022-05-25' or unit_prise < 20; if date(ts) = '2022-05-25'
or unit_prise < 20;
``` ```
### D ### D
......
...@@ -9,7 +9,10 @@ ...@@ -9,7 +9,10 @@
"geo.json" "geo.json"
], ],
"keywords_must": [ "keywords_must": [
["mysql", "运算符"] [
"mysql",
"运算符"
]
], ],
"keywords_forbid": [], "keywords_forbid": [],
"group": 1 "group": 1
......
...@@ -22,7 +22,8 @@ create table points( ...@@ -22,7 +22,8 @@ create table points(
## 答案 ## 答案
```sql ```sql
select id, sqrt(x^2 + y^2) from points; select id, sqrt(x^2 + y^2)
from points;
``` ```
## 选项 ## 选项
...@@ -30,23 +31,30 @@ select id, sqrt(x^2 + y^2) from points; ...@@ -30,23 +31,30 @@ select id, sqrt(x^2 + y^2) from points;
### A ### A
```sql ```sql
select sqrt(vx+vy) from (select x^2 as vx, y^2 as vy from points) as t; select sqrt(vx+vy)
from (
select x^2 as vx, y^2 as vy
from points) as t;
``` ```
### B ### B
```sql ```sql
select sqrt(vx + vy) from points where x^2 as vx, y^2 as vy ; select sqrt(vx + vy)
from points
where x^2 as vx, y^2 as vy ;
``` ```
### C ### C
```sql ```sql
select id + sqrt(x^2 + y^2) from points; select id + sqrt(x^2 + y^2)
from points;
``` ```
### D ### D
```sql ```sql
select id || sqrt(x^2 + y^2) from points; select id || sqrt(x^2 + y^2)
from points;
``` ```
\ No newline at end of file
...@@ -14,7 +14,10 @@ Joe 希望这个计算更紧凑一些,在已经有 individual_income_tax 的 ...@@ -14,7 +14,10 @@ Joe 希望这个计算更紧凑一些,在已经有 individual_income_tax 的
## 答案 ## 答案
```sql ```sql
create procedure sp_idt(in salary decimal(12, 4), out tax decimal(12, 4), out take_home decimal(12, 4)) create procedure sp_idt(
in salary decimal(12, 4),
out tax decimal(12, 4),
out take_home decimal(12, 4))
begin begin
set tax = individual_income_tax(salary); set tax = individual_income_tax(salary);
set take_home = salary - tax; set take_home = salary - tax;
...@@ -26,7 +29,9 @@ end; ...@@ -26,7 +29,9 @@ end;
### A ### A
```sql ```sql
create procedure sp_idt(salary decimal(12, 4)) returns (tax decimal(12, 4), take_home decimal(12, 4)) create procedure sp_idt(salary decimal(12, 4))
returns (tax decimal(12, 4),
take_home decimal(12, 4))
begin begin
set tax = individual_income_tax(salary); set tax = individual_income_tax(salary);
set take_home = salary - tax; set take_home = salary - tax;
...@@ -37,7 +42,8 @@ end; ...@@ -37,7 +42,8 @@ end;
```sql ```sql
create procedure sp_idt(salary decimal(12, 4)) returns (decimal(12, 4), decimal(12, 4)) create procedure sp_idt(salary decimal(12, 4))
returns (decimal(12, 4), decimal(12, 4))
begin begin
declare tax, take_home decimal(12, 4); declare tax, take_home decimal(12, 4);
set tax = individual_income_tax(salary); set tax = individual_income_tax(salary);
......
...@@ -45,13 +45,44 @@ create table orders_log ...@@ -45,13 +45,44 @@ create table orders_log
## 答案 ## 答案
```sql ```sql
create trigger in_orders after insert on orders create trigger in_orders
for each row insert into orders_log(id, item_id, amount, unit_price, total, description, ts, direction) after insert on orders
values(NEW.id, NEW.item_id, NEW.amount, NEW.unit_price, NEW.total, NEW.description, NEW.ts, 'in'); for each row insert into orders_log(
id,
create trigger out_orders after delete on orders item_id,
for each row insert into orders_log(id, item_id, amount, unit_price, total, description, ts, direction) amount,
values(OLD.id, OLD.item_id, OLD.amount, OLD.unit_price, OLD.total, OLD.description, OLD.ts, 'out'); unit_price,
total,
description,
ts,
direction)
values(NEW.id,
NEW.item_id,
NEW.amount,
NEW.unit_price,
NEW.total,
NEW.description,
NEW.ts, 'in');
create trigger out_orders
after delete on orders
for each row insert into orders_log(
id,
item_id,
amount,
unit_price,
total,
description,
ts,
direction)
values(OLD.id,
OLD.item_id,
OLD.amount,
OLD.unit_price,
OLD.total,
OLD.description,
OLD.ts,
'out');
``` ```
## 选项 ## 选项
...@@ -60,8 +91,23 @@ create trigger out_orders after delete on orders ...@@ -60,8 +91,23 @@ create trigger out_orders after delete on orders
```sql ```sql
create trigger in_orders after insert on orders create trigger in_orders after insert on orders
for each row insert into orders_log(id, item_id, amount, unit_price, total, description, ts, direction) for each row insert into orders_log(
values(NEW.id, NEW.item_id, NEW.amount, NEW.unit_price, NEW.total, NEW.description, NEW.ts, 'in'); id,
item_id,
amount,
unit_price,
total,
description,
ts,
direction)
values(NEW.id,
NEW.item_id,
NEW.amount,
NEW.unit_price,
NEW.total,
NEW.description,
NEW.ts,
'in');
``` ```
...@@ -70,8 +116,23 @@ create trigger in_orders after insert on orders ...@@ -70,8 +116,23 @@ create trigger in_orders after insert on orders
```sql ```sql
create trigger out_orders after delete on orders create trigger out_orders after delete on orders
for each row insert into orders_log(id, item_id, amount, unit_price, total, description, ts, direction) for each row insert into orders_log(
values(OLD.id, OLD.item_id, OLD.amount, OLD.unit_price, OLD.total, OLD.description, OLD.ts, 'out'); id,
item_id,
amount,
unit_price,
total,
description,
ts,
direction)
values(OLD.id,
OLD.item_id,
OLD.amount,
OLD.unit_price,
OLD.total,
OLD.description,
OLD.ts,
'out');
``` ```
...@@ -79,17 +140,63 @@ create trigger out_orders after delete on orders ...@@ -79,17 +140,63 @@ create trigger out_orders after delete on orders
```sql ```sql
create trigger in_orders after insert on orders create trigger in_orders after insert on orders
for each row insert into orders_log(id, item_id, amount, unit_price, total, description, ts, direction) for each row insert into orders_log(
values(NEW.id, NEW.item_id, NEW.amount, NEW.unit_price, NEW.total, NEW.description, NEW.ts, 'in'); id,
item_id,
amount,
unit_price,
total,
description,
ts,
direction)
values(NEW.id,
NEW.item_id,
NEW.amount,
NEW.unit_price,
NEW.total,
NEW.description,
NEW.ts,
'in');
after delete on orders after delete on orders
for each row insert into orders_log(id, item_id, amount, unit_price, total, description, ts, direction) for each row insert into orders_log(
values(OLD.id, OLD.item_id, OLD.amount, OLD.unit_price, OLD.total, OLD.description, OLD.ts, 'out'); id,
item_id,
amount,
unit_price,
total,
description,
ts,
direction)
values(OLD.id,
OLD.item_id,
OLD.amount,
OLD.unit_price,
OLD.total,
OLD.description,
OLD.ts,
'out');
``` ```
### D ### D
```sql ```sql
create trigger in_orders after insert, after delete on orders create trigger in_orders after insert, after delete on orders
for each row insert into orders_log(id, item_id, amount, unit_price, total, description, ts, direction) for each row insert into orders_log(
values(NEW.id, NEW.item_id, NEW.amount, NEW.unit_price, NEW.total, NEW.description, NEW.ts, 'in'); id,
item_id,
amount,
unit_price,
total,
description,
ts,
direction)
values(NEW.id,
NEW.item_id,
NEW.amount,
NEW.unit_price,
NEW.total,
NEW.description,
NEW.ts,
'in');
``` ```
\ No newline at end of file
...@@ -13,8 +13,11 @@ Joe 要给数据组的 John 创建一个用户,他希望John 能够从 `192.16 ...@@ -13,8 +13,11 @@ Joe 要给数据组的 John 创建一个用户,他希望John 能够从 `192.16
## 答案 ## 答案
```sql ```sql
create user 'john'@'192.168.7.42' identified by 'goods123' password expire; create user 'john'@'192.168.7.42'
identified by 'goods123' password expire;
grant select on goods.* to 'john'@'192.168.7.42'; grant select on goods.* to 'john'@'192.168.7.42';
flush privileges; flush privileges;
``` ```
...@@ -23,22 +26,31 @@ flush privileges; ...@@ -23,22 +26,31 @@ flush privileges;
### A ### A
```sql ```sql
create user 'john'@'192.168.7.42' identified by 'goods123'; create user 'john'@'192.168.7.42'
identified by 'goods123';
grant select on goods.* to 'john'@'192.168.7.42'; grant select on goods.* to 'john'@'192.168.7.42';
flush privileges; flush privileges;
``` ```
### B ### B
```sql ```sql
create user 'john'@'192.168.7.42' identified by 'goods123'; create user 'john'@'192.168.7.42'
grant usage on goods.* to 'john'@'192.168.7.42'; identified by 'goods123';
grant usage on
goods.* to 'john'@'192.168.7.42';
flush privileges; flush privileges;
``` ```
### C ### C
```sql ```sql
create user 'john'@'192.168.7.42' identified by 'goods123' on database goods; create user 'john'@'192.168.7.42'
identified by 'goods123' on database goods;
flush privileges; flush privileges;
``` ```
\ No newline at end of file
...@@ -22,31 +22,21 @@ IDENTIFIED BY '@Binghe123456'; ...@@ -22,31 +22,21 @@ IDENTIFIED BY '@Binghe123456';
### A ### A
```
可以使用GRANT来创建用户 可以使用GRANT来创建用户
```
### B ### B
```
可以使用CREATE USER语句来创建用户 可以使用CREATE USER语句来创建用户
```
### C ### C
```
CREATE USER 'binghe'@'localhost'; CREATE USER 'binghe'@'localhost';
创建用户名为binghe的MySQL用户,其主机名为localhost。 创建用户名为binghe的MySQL用户,其主机名为localhost。
```
### D ### D
```
使用GRANT语句创建用户,不仅可以添加用户,而且还能为用户赋予相应的权限。 使用GRANT语句创建用户,不仅可以添加用户,而且还能为用户赋予相应的权限。
```
### E ### E
```
使用CREATE USER语句创建用户时,只是在mysql数据库下的user数据表中添加了一条记录,并没有为用户授权。 使用CREATE USER语句创建用户时,只是在mysql数据库下的user数据表中添加了一条记录,并没有为用户授权。
```
\ No newline at end of file
...@@ -12,7 +12,8 @@ Joe 需要限制数据分析组(role analysis)的用户, 每小时查询次 ...@@ -12,7 +12,8 @@ Joe 需要限制数据分析组(role analysis)的用户, 每小时查询次
## 答案 ## 答案
```sql ```sql
alter user analysis with MAX_QUERIES_PER_HOUR 10000; alter user analysis
with MAX_QUERIES_PER_HOUR 10000;
``` ```
## 选项 ## 选项
...@@ -20,24 +21,28 @@ alter user analysis with MAX_QUERIES_PER_HOUR 10000; ...@@ -20,24 +21,28 @@ alter user analysis with MAX_QUERIES_PER_HOUR 10000;
### A ### A
```sql ```sql
set user analysis with MAX_QUERIES_PER_HOUR 10000; set user analysis
with MAX_QUERIES_PER_HOUR 10000;
``` ```
### B ### B
```sql ```sql
alter role analysis with MAX_QUERIES_PER_HOUR 10000; alter role analysis
with MAX_QUERIES_PER_HOUR 10000;
``` ```
### C ### C
```sql ```sql
alter role analysis with MAX_QUERIES_PER_HOUR=10000; alter role analysis
with MAX_QUERIES_PER_HOUR=10000;
``` ```
### D ### D
```sql ```sql
alter user analysis set MAX_QUERIES_PER_HOUR 10000; alter user analysis
set MAX_QUERIES_PER_HOUR 10000;
``` ```
...@@ -24,7 +24,10 @@ create table goods( ...@@ -24,7 +24,10 @@ create table goods(
## 答案 ## 答案
```sql ```sql
SELECT * FROM goods HAVING price BETWEEN 1000 AND 2000; SELECT *
FROM goods
HAVING price
BETWEEN 1000 AND 2000;
``` ```
...@@ -33,18 +36,25 @@ SELECT * FROM goods HAVING price BETWEEN 1000 AND 2000; ...@@ -33,18 +36,25 @@ SELECT * FROM goods HAVING price BETWEEN 1000 AND 2000;
### A ### A
```sql ```sql
SELECT * FROM goods WHERE price BETWEEN 1000 AND 2000; SELECT *
FROM goods
WHERE price
BETWEEN 1000 AND 2000;
``` ```
### B ### B
```sql ```sql
SELECT * FROM goods WHERE price >= 1000 AND price <= 2000; SELECT *
FROM goods
WHERE price >= 1000 AND price <= 2000;
``` ```
### C ### C
```sql ```sql
SELECT * FROM goods WHERE not (price < 1000 or price > 2000); SELECT *
FROM goods
WHERE not (price < 1000 or price > 2000);
``` ```
{ {
"node_id": "mysql-2296dbe96d584a52bd28a3ad5f655518", "node_id": "mysql-2296dbe96d584a52bd28a3ad5f655518",
"keywords": ["where"], "keywords": [
"where"
],
"children": [], "children": [],
"export": [ "export": [
"between.json" "between.json"
], ],
"keywords_must": [ "keywords_must": [
["mysql", "between"] [
"mysql",
"between"
]
], ],
"keywords_forbid": [], "keywords_forbid": [],
"group": 1 "group": 1
......
...@@ -26,7 +26,8 @@ create table goods( ...@@ -26,7 +26,8 @@ create table goods(
## 答案 ## 答案
```sql ```sql
select count(distinct price) from goods; select count(distinct price)
from goods;
``` ```
## 选项 ## 选项
...@@ -34,23 +35,27 @@ select count(distinct price) from goods; ...@@ -34,23 +35,27 @@ select count(distinct price) from goods;
### A ### A
```sql ```sql
select count(distinct *) from goods; select count(distinct *)
from goods;
``` ```
### B ### B
```sql ```sql
select distinct count(price) from goods; select distinct count(price)
from goods;
``` ```
### C ### C
```sql ```sql
select count(price) from goods; select count(price)
from goods;
``` ```
### D ### D
```sql ```sql
select distinct price from goods; select distinct price
from goods;
``` ```
...@@ -24,7 +24,9 @@ create table employee ...@@ -24,7 +24,9 @@ create table employee
## 答案 ## 答案
```sql ```sql
select id, name, dept, salary from employee order by dept, salary desc; select id, name, dept, salary
from employee
order by dept, salary desc;
``` ```
## 选项 ## 选项
...@@ -32,28 +34,38 @@ select id, name, dept, salary from employee order by dept, salary desc; ...@@ -32,28 +34,38 @@ select id, name, dept, salary from employee order by dept, salary desc;
### A ### A
```sql ```sql
select id, name, dept, salary from employee order by dept, salary; select id, name, dept, salary
from employee
order by dept, salary;
``` ```
### B ### B
```sql ```sql
select id, name, dept, salary from employee order by dept desc, salary desc; select id, name, dept, salary
from employee
order by dept desc, salary desc;
``` ```
### C ### C
```sql ```sql
select id, name, dept, salary from employee order by dept and salary desc; select id, name, dept, salary
from employee
order by dept and salary desc;
``` ```
### D ### D
```sql ```sql
select id, name, dept, salary from employee order by dept, salary, id, name; select id, name, dept, salary
from employee
order by dept, salary, id, name;
``` ```
### E ### E
```sql ```sql
select id, name, dept, salary from employee order by dept, salary desc; select id, name, dept, salary
from employee
order by dept, salary desc;
``` ```
{ {
"node_id": "mysql-b57b6c08f5f240c6a997284e4448f088", "node_id": "mysql-b57b6c08f5f240c6a997284e4448f088",
"keywords": ["union"], "keywords": [
"union"
],
"children": [], "children": [],
"export": [ "export": [
"union.json" "union.json"
], ],
"keywords_must": [ "keywords_must": [
["mysql", "union"] [
"mysql",
"union"
]
], ],
"keywords_forbid": [], "keywords_forbid": [],
"group": 1 "group": 1
......
...@@ -12,10 +12,10 @@ create table employee( ...@@ -12,10 +12,10 @@ create table employee(
); );
create table customer( create table customer(
id int primary key auto_increment, id int primary key auto_increment,
name varchar(256), name varchar(256),
address varchar(1024), address varchar(1024),
level int level int
-- ignore more -- ignore more
) )
......
...@@ -25,7 +25,11 @@ create table orders ...@@ -25,7 +25,11 @@ create table orders
## 答案 ## 答案
```sql ```sql
select id, product_id, order_date, quantity, customer_id select id,
product_id,
order_date,
quantity,
customer_id
from orders from orders
where date = $1 where date = $1
offset $2 limit 100; offset $2 limit 100;
...@@ -36,7 +40,11 @@ offset $2 limit 100; ...@@ -36,7 +40,11 @@ offset $2 limit 100;
### 缺少 limit ### 缺少 limit
```sql ```sql
select id, product_id, order_date, quantity, customer_id select id,
product_id,
order_date,
quantity,
customer_id
from orders from orders
where date = $1 where date = $1
offset $2; offset $2;
...@@ -45,7 +53,11 @@ offset $2; ...@@ -45,7 +53,11 @@ offset $2;
### 缺少 offset ### 缺少 offset
```sql ```sql
select id, product_id, order_date, quantity, customer_id select id,
product_id,
order_date,
quantity,
customer_id
from orders from orders
where date = $1; where date = $1;
``` ```
...@@ -53,7 +65,11 @@ where date = $1; ...@@ -53,7 +65,11 @@ where date = $1;
### 结构不对 ### 结构不对
```sql ```sql
select id, product_id, order_date, quantity, customer_id select id,
product_id,
order_date,
quantity,
customer_id
from orders from orders
where date = $1 and where date = $1 and
offset $2 and limit 100; offset $2 and limit 100;
......
...@@ -18,7 +18,8 @@ select name from goods; ...@@ -18,7 +18,8 @@ select name from goods;
## 答案 ## 答案
```sql ```sql
select convert(name using 'gb18030') from goods; select convert(name using 'gb18030')
from goods;
``` ```
## 选项 ## 选项
...@@ -27,27 +28,31 @@ select convert(name using 'gb18030') from goods; ...@@ -27,27 +28,31 @@ select convert(name using 'gb18030') from goods;
### A ### A
```sql ```sql
select str(name, 'gb18303') from goods; select str(name, 'gb18303')
from goods;
``` ```
### B ### B
```sql ```sql
select encode(decode(name, 'utf8mb4'), 'gb18303') from goods; select encode(decode(name, 'utf8mb4'), 'gb18303')
from goods;
``` ```
### C ### C
```sql ```sql
select convert(name from 'utf8mb4' to 'gb18303') from goods; select convert(name from 'utf8mb4' to 'gb18303')
from goods;
``` ```
### D ### D
```sql ```sql
select convert(name to 'gb18303') from goods; select convert(name to 'gb18303')
from goods;
``` ```
### E ### E
......
...@@ -17,7 +17,8 @@ mysql> select * from items; ...@@ -17,7 +17,8 @@ mysql> select * from items;
当他执行 当他执行
```sql ```sql
select count(*), count(item) from items; select count(*), count(item)
from items;
``` ```
会得到什么结果? 会得到什么结果?
......
...@@ -4,14 +4,14 @@ Joe 想要得到 orders 表 ...@@ -4,14 +4,14 @@ Joe 想要得到 orders 表
```sql ```sql
create table orders ( create table orders (
id int primary key auto_increment, id int primary key auto_increment,
item_id int, item_id int,
amount int, amount int,
unit_price decimal(12, 4), unit_price decimal(12, 4),
total decimal(12, 4), total decimal(12, 4),
description varchar(2000), description varchar(2000),
ts timestamp default now(), ts timestamp default now(),
deal bool default false deal bool default false
); );
``` ```
...@@ -27,7 +27,9 @@ create table orders ( ...@@ -27,7 +27,9 @@ create table orders (
## 答案 ## 答案
```sql ```sql
select sum(total) from orders where deal and unit_price > 1000; select sum(total)
from orders
where deal and unit_price > 1000;
``` ```
## 选项 ## 选项
...@@ -35,17 +37,24 @@ select sum(total) from orders where deal and unit_price > 1000; ...@@ -35,17 +37,24 @@ select sum(total) from orders where deal and unit_price > 1000;
### A ### A
```sql ```sql
select sum(total) from orders having deal and unit_price > 1000; select sum(total)
from orders
having deal and unit_price > 1000;
``` ```
### B ### B
```sql ```sql
select sum(total) from orders group by deal having unit_price > 1000; select sum(total)
from orders
group by deal
having unit_price > 1000;
``` ```
### C ### C
```sql ```sql
select sum(total) from orders order by deal and unit_price > 1000; select sum(total)
from orders
order by deal and unit_price > 1000;
``` ```
\ No newline at end of file
...@@ -24,7 +24,9 @@ create table employee ...@@ -24,7 +24,9 @@ create table employee
## 答案 ## 答案
```sql ```sql
select dept, min(salary) from employee group by dept; select dept, min(salary)
from employee
group by dept;
``` ```
## 选项 ## 选项
...@@ -32,17 +34,20 @@ select dept, min(salary) from employee group by dept; ...@@ -32,17 +34,20 @@ select dept, min(salary) from employee group by dept;
### A ### A
```sql ```sql
select dept, min(salary) from employee; select dept, min(salary)
from employee;
``` ```
### B ### B
```sql ```sql
select dept, min(salary) from employee order by dept; select dept, min(salary)
from employee order by dept;
``` ```
### C ### C
```sql ```sql
select dept, min(total) from employee; select dept, min(total)
from employee;
``` ```
\ No newline at end of file
...@@ -24,7 +24,9 @@ create table employee ...@@ -24,7 +24,9 @@ create table employee
## 答案 ## 答案
```sql ```sql
select dept, avg(salary) from employee group by dept; select dept, avg(salary)
from employee
group by dept;
``` ```
## 选项 ## 选项
...@@ -32,17 +34,22 @@ select dept, avg(salary) from employee group by dept; ...@@ -32,17 +34,22 @@ select dept, avg(salary) from employee group by dept;
### A ### A
```sql ```sql
select dept, avg(salary) from employee; select dept, avg(salary)
from employee;
``` ```
### B ### B
```sql ```sql
select dept, avg(dept) from employee group by dept; select dept, avg(dept)
from employee
group by dept;
``` ```
### C ### C
```sql ```sql
select dept, avg(salary) from employee group by salary; select dept, avg(salary)
from employee
group by salary;
``` ```
{ {
"node_id": "mysql-11462fccf9d24d17a372d5c60af90f54", "node_id": "mysql-11462fccf9d24d17a372d5c60af90f54",
"keywords": ["avg", "平均值"], "keywords": [
"avg",
"平均值"
],
"children": [], "children": [],
"export": [ "export": [
"avg.json" "avg.json"
......
...@@ -6,7 +6,10 @@ ...@@ -6,7 +6,10 @@
"having.json" "having.json"
], ],
"keywords_must": [ "keywords_must": [
["mysql", "having"] [
"mysql",
"having"
]
], ],
"keywords_forbid": [], "keywords_forbid": [],
"group": 1 "group": 1
......
...@@ -24,7 +24,10 @@ create table employee ...@@ -24,7 +24,10 @@ create table employee
## 答案 ## 答案
```sql ```sql
select dept from employee group by dept having sum(salary) > 100000; select dept
from employee
group by dept
having sum(salary) > 100000;
``` ```
## 选项 ## 选项
...@@ -32,25 +35,36 @@ select dept from employee group by dept having sum(salary) > 100000; ...@@ -32,25 +35,36 @@ select dept from employee group by dept having sum(salary) > 100000;
### A ### A
```sql ```sql
select dept from employee group by dept where sum(salary) > 100000; select dept
from employee
group by dept where sum(salary) > 100000;
``` ```
### B ### B
```sql ```sql
select dept from employee where sum(salary) > 100000 group by dept; select dept
from employee
where sum(salary) > 100000
group by dept;
``` ```
### C ### C
```sql ```sql
select dept from employee where sum(salary) > 100000 order by dept; select dept
from employee
where sum(salary) > 100000
order by dept;
``` ```
### D ### D
```sql ```sql
select dept from employee group by dept where sum(salary) > 100000; select dept
from employee
group by dept
where sum(salary) > 100000;
``` ```
...@@ -48,7 +48,8 @@ having salary > max(salary) ...@@ -48,7 +48,8 @@ having salary > max(salary)
```sql ```sql
select l.id, l.name, l.dept, l.salary select l.id, l.name, l.dept, l.salary
from employee as l from employee as l
join employee as r on l.salary > max(r.salary) join employee as r
on l.salary > max(r.salary)
where r.dept = 'sale' where r.dept = 'sale'
group by r.dept group by r.dept
``` ```
...@@ -58,7 +59,9 @@ group by r.dept ...@@ -58,7 +59,9 @@ group by r.dept
```sql ```sql
select id, name, dept, salary select id, name, dept, salary
from employee from employee
having salary > (select max(salary) from employee where dept = 'sale') having salary > (select max(salary)
from employee
where dept = 'sale')
``` ```
......
...@@ -25,7 +25,9 @@ create table employee( ...@@ -25,7 +25,9 @@ create table employee(
```sql ```sql
select id, name, dept, salary select id, name, dept, salary
from employee as o from employee as o
where o.salary < any(select salary from employee as i where i.dept=o.dept) where o.salary < any(select salary
from employee as i
where i.dept=o.dept)
``` ```
## 选项 ## 选项
...@@ -35,7 +37,8 @@ where o.salary < any(select salary from employee as i where i.dept=o.dept) ...@@ -35,7 +37,8 @@ where o.salary < any(select salary from employee as i where i.dept=o.dept)
```sql ```sql
select id, name, dept, salary select id, name, dept, salary
from employee as o from employee as o
join employee as i on o.dept = i.dept and o.salary < i.salary join employee as i on
o.dept = i.dept and o.salary < i.salary
``` ```
### B ### B
...@@ -43,7 +46,8 @@ join employee as i on o.dept = i.dept and o.salary < i.salary ...@@ -43,7 +46,8 @@ join employee as i on o.dept = i.dept and o.salary < i.salary
```sql ```sql
select o.id, o.name, o.dept, o.salary select o.id, o.name, o.dept, o.salary
from employee as o from employee as o
left join employee as i on o.dept = i.dept and o.salary < i.salary left join employee as i on
o.dept = i.dept and o.salary < i.salary
where i.id is null; where i.id is null;
``` ```
...@@ -52,6 +56,7 @@ where i.id is null; ...@@ -52,6 +56,7 @@ where i.id is null;
```sql ```sql
select o.id, o.name, o.dept, o.salary select o.id, o.name, o.dept, o.salary
from employee as o from employee as o
left join employee as i on o.dept = i.dept and o.salary < i.salary left join employee as i
on o.dept = i.dept and o.salary < i.salary
where i.id is not null; where i.id is not null;
``` ```
\ No newline at end of file
...@@ -25,7 +25,10 @@ create table employee( ...@@ -25,7 +25,10 @@ create table employee(
```sql ```sql
select id, name, dept select id, name, dept
from employee as o from employee as o
where 'assistant' != all(select post from employee as i where o.dept = i.dept); where 'assistant' !=
all(select post
from employee as i
where o.dept = i.dept);
``` ```
## 选项 ## 选项
...@@ -35,7 +38,10 @@ where 'assistant' != all(select post from employee as i where o.dept = i.dept); ...@@ -35,7 +38,10 @@ where 'assistant' != all(select post from employee as i where o.dept = i.dept);
```sql ```sql
select id, name, dept select id, name, dept
from employee as o from employee as o
where 'assistant' = all(select post from employee as i where o.dept = i.dept); where 'assistant' =
all(select post
from employee as i
where o.dept = i.dept);
``` ```
### B ### B
...@@ -43,14 +49,19 @@ where 'assistant' = all(select post from employee as i where o.dept = i.dept); ...@@ -43,14 +49,19 @@ where 'assistant' = all(select post from employee as i where o.dept = i.dept);
```sql ```sql
select id, name, dept select id, name, dept
from employee as o from employee as o
where 'assistant' != all(select post from employee as i where o.dept = i.dept); where 'assistant' !=
all(select post
from employee as i
where o.dept = i.dept);
``` ```
### C ### C
```sql ```sql
select id, name, dept select id, name, dept
from employee as o from employee as o
where 'assistant' != all(select post from employee as i); where 'assistant' !=
all(select post
from employee as i);
``` ```
### D ### D
...@@ -58,5 +69,8 @@ where 'assistant' != all(select post from employee as i); ...@@ -58,5 +69,8 @@ where 'assistant' != all(select post from employee as i);
```sql ```sql
select id, name, dept select id, name, dept
from employee as o from employee as o
where 'assistant' != ANY(select post from employee as i where o.dept = i.dept); where 'assistant' !=
ANY(select post
from employee as i
where o.dept = i.dept);
``` ```
\ No newline at end of file
...@@ -26,7 +26,10 @@ create table employee( ...@@ -26,7 +26,10 @@ create table employee(
```sql ```sql
select id, name, dept select id, name, dept
from employee as o from employee as o
where not exists(select * from employee as i where o.dept = i.dept and post='assistant'); where not exists(
select *
from employee as i
where o.dept = i.dept and post='assistant');
``` ```
## 选项 ## 选项
...@@ -36,14 +39,19 @@ where not exists(select * from employee as i where o.dept = i.dept and post='ass ...@@ -36,14 +39,19 @@ where not exists(select * from employee as i where o.dept = i.dept and post='ass
```sql ```sql
select id, name, dept select id, name, dept
from employee as o from employee as o
where exists(select * from employee as i where o.dept = i.dept and post='assistant'); where exists(
select *
from employee as i
where o.dept = i.dept and post='assistant');
``` ```
### B ### B
```sql ```sql
select id, name, dept select id, name, dept
from employee as o from employee as o
where 'assistant' != exists(select post from employee as i); where 'assistant' != exists(
select post
from employee as i);
``` ```
### C ### C
...@@ -51,5 +59,8 @@ where 'assistant' != exists(select post from employee as i); ...@@ -51,5 +59,8 @@ where 'assistant' != exists(select post from employee as i);
```sql ```sql
select id, name, dept select id, name, dept
from employee as o from employee as o
where 'assistant' = not exists(select post from employee as i where o.dept = i.dept); where 'assistant' = not exists(
select post
from employee as i
where o.dept = i.dept);
``` ```
\ No newline at end of file
...@@ -43,7 +43,9 @@ where dept in (select 'dev', 'hr'); ...@@ -43,7 +43,9 @@ where dept in (select 'dev', 'hr');
```sql ```sql
select id, dept, name, post select id, dept, name, post
from employee from employee
where dept in (select * from 'dev', 'hr'); where dept in
(select *
from 'dev', 'hr');
``` ```
### C ### C
......
...@@ -24,10 +24,10 @@ create table employee ...@@ -24,10 +24,10 @@ create table employee
## 答案 ## 答案
```sql ```sql
select distinct(dept) as dept, select distinct(dept) as dept,
(select count(*) (select count(*)
from employee as i from employee as i
where i.dept = o.dept) as emp where i.dept = o.dept) as emp
from employee as o; from employee as o;
``` ```
...@@ -36,28 +36,28 @@ from employee as o; ...@@ -36,28 +36,28 @@ from employee as o;
### A ### A
```sql ```sql
select distinct(dept) as dept, select distinct(dept) as dept,
(select count(*) (select count(*)
from employee as i) as emp from employee as i) as emp
from employee as o; from employee as o;
``` ```
### B ### B
```sql ```sql
select distinct(dept) as dept, select distinct(dept) as dept,
(select count(*) (select count(*)
from employee from employee
where dept = dept) as emp where dept = dept) as emp
from employee from employee
``` ```
### C ### C
```sql ```sql
select dept as dept, select dept as dept,
(select count(*) (select count(*)
from employee as i from employee as i
where i.dept = o.dept) as emp where i.dept = o.dept) as emp
from employee as o from employee as o
``` ```
\ No newline at end of file
...@@ -34,8 +34,11 @@ Joe 想要列出所有的部门,如果这个部门有部门助理(post 为 ` ...@@ -34,8 +34,11 @@ Joe 想要列出所有的部门,如果这个部门有部门助理(post 为 `
```sql ```sql
select d.id, d.name, e.name as assistant select d.id, d.name, e.name as assistant
from department as d from department as d
left join employee as e on e.dept = d.id left join (select name, dept_id
where e.post = 'assistant' from employee
where post='assistant') as e on e.dept_id = d.id
``` ```
## 选项 ## 选项
......
...@@ -46,7 +46,7 @@ where d.id is null; ...@@ -46,7 +46,7 @@ where d.id is null;
```sql ```sql
select e.id, e.name, e.dept select e.id, e.name, e.dept
from employee as e from employee as e
right join department as d on d.id = e.dept right join department as d on d.id = e.dept
where e.id is null; where e.id is null;
``` ```
...@@ -55,7 +55,7 @@ where e.id is null; ...@@ -55,7 +55,7 @@ where e.id is null;
```sql ```sql
select e.id, e.name, e.dept select e.id, e.name, e.dept
from employee as e from employee as e
right join department as d on d.id = e.dept right join department as d on d.id = e.dept
where d.id is null; where d.id is null;
``` ```
......
{ {
"node_id": "mysql-fe65d5c615ad40f8ac056cc654f2d788", "node_id": "mysql-fe65d5c615ad40f8ac056cc654f2d788",
"keywords": ["create index", "创建索引"], "keywords": [
"create index",
"创建索引"
],
"children": [], "children": [],
"export": [ "export": [
"create_index.json" "create_index.json"
......
...@@ -27,7 +27,8 @@ create table goods( ...@@ -27,7 +27,8 @@ create table goods(
## 答案 ## 答案
```sql ```sql
create index idx_goods_category on goods(category_id); create index idx_goods_category
on goods(category_id);
``` ```
## 选项 ## 选项
...@@ -35,17 +36,20 @@ create index idx_goods_category on goods(category_id); ...@@ -35,17 +36,20 @@ create index idx_goods_category on goods(category_id);
### A ### A
```sql ```sql
create unique index idx_goods_category on goods(category_id); create unique index idx_goods_category
on goods(category_id);
``` ```
### B ### B
```sql ```sql
create index idx_goods_category on goods(name); create index idx_goods_category
on goods(name);
``` ```
### C ### C
```sql ```sql
alter table goods add unique index (category_id); alter table goods
add unique index (category_id);
``` ```
\ No newline at end of file
...@@ -20,7 +20,8 @@ show index from goods; ...@@ -20,7 +20,8 @@ show index from goods;
查看 goods 表的索引(假设查到是 idx_goods_category),然后执行 查看 goods 表的索引(假设查到是 idx_goods_category),然后执行
```sql ```sql
alter table goods drop index idx_goods_category; alter table goods
drop index idx_goods_category;
``` ```
## 选项 ## 选项
...@@ -36,7 +37,8 @@ show index from goods; ...@@ -36,7 +37,8 @@ show index from goods;
查看 goods 表的索引(假设查到是 idx_goods_category),然后执行 查看 goods 表的索引(假设查到是 idx_goods_category),然后执行
```sql ```sql
alter table goods delete index idx_goods_category; alter table goods
delete index idx_goods_category;
``` ```
### B ### B
...@@ -50,7 +52,8 @@ show index from goods; ...@@ -50,7 +52,8 @@ show index from goods;
查看 goods 表的索引(假设查到是 idx_goods_category),然后执行 查看 goods 表的索引(假设查到是 idx_goods_category),然后执行
```sql ```sql
alter table goods remove index idx_goods_category; alter table goods
remove index idx_goods_category;
``` ```
### C ### C
...@@ -64,7 +67,8 @@ show index from goods; ...@@ -64,7 +67,8 @@ show index from goods;
查看 goods 表的索引(假设查到是 idx_goods_category),然后执行 查看 goods 表的索引(假设查到是 idx_goods_category),然后执行
```sql ```sql
alter table goods drop idx_goods_category; alter table goods
drop idx_goods_category;
``` ```
......
...@@ -25,7 +25,8 @@ Joe 需要确保同一个类型下没有重名的商品,他应该怎么做? ...@@ -25,7 +25,8 @@ Joe 需要确保同一个类型下没有重名的商品,他应该怎么做?
## 答案 ## 答案
```sql ```sql
alter table goods add unique index (category_id, name); alter table goods
add unique index (category_id, name);
``` ```
## 选项 ## 选项
...@@ -33,17 +34,20 @@ alter table goods add unique index (category_id, name); ...@@ -33,17 +34,20 @@ alter table goods add unique index (category_id, name);
### A ### A
```sql ```sql
alter table goods add index (category_id, name); alter table goods
add index (category_id, name);
``` ```
### B ### B
```sql ```sql
alter table goods add unique index (category_id + name); alter table goods
add unique index (category_id + name);
``` ```
### C ### C
```sql ```sql
alter table goods add unique index (concat(category_id, name)); alter table goods
add unique index (concat(category_id, name));
``` ```
...@@ -7,7 +7,12 @@ ...@@ -7,7 +7,12 @@
"export": [ "export": [
"total_index.json" "total_index.json"
], ],
"keywords_must": [["mysql", "全值匹配"]], "keywords_must": [
[
"mysql",
"全值匹配"
]
],
"keywords_forbid": [], "keywords_forbid": [],
"group": 2 "group": 2
} }
\ No newline at end of file
...@@ -25,7 +25,8 @@ create table goods( ...@@ -25,7 +25,8 @@ create table goods(
## 答案 ## 答案
```sql ```sql
alter table goods add index (name, price); alter table goods
add index (name, price);
``` ```
## 选项 ## 选项
...@@ -39,11 +40,14 @@ alter table goods add index (name, price); ...@@ -39,11 +40,14 @@ alter table goods add index (name, price);
建立一个计算字段: 建立一个计算字段:
```sql ```sql
alter table goods add summary varchar(1024) generated always as (concat(name, '(', 0.5, ')')); alter table goods
add summary varchar(1024)
generated always as (concat(name, '(', 0.5, ')'));
``` ```
### C ### C
```sql ```sql
alter table goods add index (concat(name, price)); alter table goods
add index (concat(name, price));
``` ```
\ No newline at end of file
...@@ -29,7 +29,12 @@ Joe 发现有大量查询 `select id, category_id, name, price from goods where ...@@ -29,7 +29,12 @@ Joe 发现有大量查询 `select id, category_id, name, price from goods where
将该查询改写为 将该查询改写为
```sql ```sql
select id, category_id, name, price from goods where category_id=? and name=?; select id,
category_id,
name,
price
from goods
where category_id=? and name=?;
``` ```
## 选项 ## 选项
...@@ -39,7 +44,12 @@ select id, category_id, name, price from goods where category_id=? and name=?; ...@@ -39,7 +44,12 @@ select id, category_id, name, price from goods where category_id=? and name=?;
将该查询改写为 将该查询改写为
```sql ```sql
select id, category_id, name, price from goods where category_id and name= (?, ?); select id,
category_id,
name,
price
from goods
where category_id and name= (?, ?);
``` ```
### B ### B
...@@ -47,7 +57,12 @@ select id, category_id, name, price from goods where category_id and name= (?, ? ...@@ -47,7 +57,12 @@ select id, category_id, name, price from goods where category_id and name= (?, ?
将该查询改写为 将该查询改写为
```sql ```sql
select id, category_id, name, price from goods where not (category_id !=? or name != ?); select id,
category_id,
name,
price
from goods
where not (category_id !=? or name != ?);
``` ```
### C ### C
...@@ -55,6 +70,11 @@ select id, category_id, name, price from goods where not (category_id !=? or nam ...@@ -55,6 +70,11 @@ select id, category_id, name, price from goods where not (category_id !=? or nam
将该查询改写为 将该查询改写为
```sql ```sql
select id, category_id, name, price from goods where not (category_id !=?) and not (name != ?); select id,
category_id,
name,
price
from goods
where not (category_id !=?) and not (name != ?);
``` ```
...@@ -14,7 +14,19 @@ create table goods( ...@@ -14,7 +14,19 @@ create table goods(
) )
``` ```
现有大量查询 `select id, category_id, name, price, stock from goods where stock=? and category_id=? and name like ?` 现有大量查询
```sql
select
id,
category_id,
name,
price,
stock
from goods
where stock=?
and category_id=?
and name like ?`,
```
Joe 应该如何优化? Joe 应该如何优化?
<hr/> <hr/>
...@@ -27,7 +39,8 @@ Joe 应该如何优化? ...@@ -27,7 +39,8 @@ Joe 应该如何优化?
## 答案 ## 答案
```sql ```sql
alter table goods add index (stock, category_id, name); alter table goods
add index (stock, category_id, name);
``` ```
## 选项 ## 选项
...@@ -35,17 +48,20 @@ alter table goods add index (stock, category_id, name); ...@@ -35,17 +48,20 @@ alter table goods add index (stock, category_id, name);
### A ### A
```sql ```sql
alter table goods add index (stock and category_id and name); alter table goods
add index (stock and category_id and name);
``` ```
### B ### B
```sql ```sql
alter table goods add index (concat(stock, category_id, name)); alter table goods
add index (concat(stock, category_id, name));
``` ```
### C ### C
```sql ```sql
alter table goods add index (stock + category_id + name); alter table goods
add index (stock + category_id + name);
``` ```
...@@ -22,8 +22,10 @@ create table shop ( ...@@ -22,8 +22,10 @@ create table shop (
## 答案 ## 答案
```sql ```sql
alter table shop modify location GEOMETRY not null; alter table shop
alter table shop add INDEX geo_index(location); modify location GEOMETRY not null;
alter table shop
add INDEX geo_index(location);
``` ```
## 选项 ## 选项
...@@ -31,25 +33,31 @@ alter table shop add INDEX geo_index(location); ...@@ -31,25 +33,31 @@ alter table shop add INDEX geo_index(location);
### A ### A
```sql ```sql
alter table shop add INDEX geo_index(location); alter table shop
add INDEX geo_index(location);
``` ```
### B ### B
```sql ```sql
alter table shop add INDEX location; alter table shop
add INDEX location;
``` ```
### C ### C
```sql ```sql
alter table shop modify location GEOMETRY not null; alter table shop
alter table shop add INDEX location; modify location GEOMETRY not null;
alter table shop
add INDEX location;
``` ```
### D ### D
```sql ```sql
alter table shop modify location GEOMETRY not null; alter table shop
alter table shop add INDEX location; modify location GEOMETRY not null;
alter table shop
add INDEX location;
``` ```
...@@ -22,7 +22,8 @@ create table shop ( ...@@ -22,7 +22,8 @@ create table shop (
## 答案 ## 答案
```sql ```sql
alter table shop add fulltext(description); alter table shop
add fulltext(description);
``` ```
## 选项 ## 选项
...@@ -30,17 +31,20 @@ alter table shop add fulltext(description); ...@@ -30,17 +31,20 @@ alter table shop add fulltext(description);
### A ### A
```sql ```sql
alter table shop add index fulltext(description); alter table shop
add index fulltext(description);
``` ```
### B ### B
```sql ```sql
alter table shop create fulltext(description); alter table shop
create fulltext(description);
``` ```
### C ### C
```sql ```sql
alter table shop alter description add fulltext(description); alter table shop
alter description add fulltext(description);
``` ```
\ No newline at end of file
{ {
"node_id": "mysql-c57e1195b0914cd38798ce029156ec87", "node_id": "mysql-c57e1195b0914cd38798ce029156ec87",
"keywords": ["invisible", "visible", "隐藏索引"], "keywords": [
"invisible",
"visible",
"隐藏索引"
],
"children": [], "children": [],
"export": [ "export": [
"invisible.json" "invisible.json"
......
...@@ -28,13 +28,15 @@ Joe 应该怎么做? ...@@ -28,13 +28,15 @@ Joe 应该怎么做?
先执行 先执行
```sql ```sql
alter table shop alter index description invisible ; alter table shop
alter index description invisible ;
``` ```
将索引隐藏,观察确认没有影响后再执行 将索引隐藏,观察确认没有影响后再执行
```sql ```sql
alter table shop drop index description; alter table shop
drop index description;
``` ```
删除。 删除。
...@@ -47,7 +49,8 @@ alter table shop drop index description; ...@@ -47,7 +49,8 @@ alter table shop drop index description;
先备份 shop 表,然后执行 先备份 shop 表,然后执行
```sql ```sql
alter table shop drop index description; alter table shop
drop index description;
``` ```
删除,有问题的话从备份文件恢复。 删除,有问题的话从备份文件恢复。
...@@ -60,13 +63,15 @@ alter table shop drop index description; ...@@ -60,13 +63,15 @@ alter table shop drop index description;
先执行 先执行
```sql ```sql
alter table shop alter index description invisible ; alter table shop
alter index description invisible ;
``` ```
将索引隐藏,确认后再执行 将索引隐藏,确认后再执行
```sql ```sql
alter table shop alter index description visible ; alter table shop
alter index description visible ;
``` ```
恢复索引可见。 恢复索引可见。
\ No newline at end of file
...@@ -40,7 +40,8 @@ mysql> desc payment; ...@@ -40,7 +40,8 @@ mysql> desc payment;
建立表达式索引 建立表达式索引
```sql ```sql
alter table payment add index idx_payment_date((date(payment_date))); alter table payment
add index idx_payment_date((date(payment_date)));
``` ```
## 选项 ## 选项
...@@ -50,7 +51,8 @@ alter table payment add index idx_payment_date((date(payment_date))); ...@@ -50,7 +51,8 @@ alter table payment add index idx_payment_date((date(payment_date)));
建立视图 建立视图
```sql ```sql
create view view_daily_payment as select date(payment_date) as day, amount from payment; create view view_daily_payment as
select date(payment_date) as day, amount from payment;
``` ```
然后在视图 view_daily_payment 上执行 然后在视图 view_daily_payment 上执行
...@@ -75,7 +77,8 @@ create index idx_payment_date on payment(payment_date); ...@@ -75,7 +77,8 @@ create index idx_payment_date on payment(payment_date);
建立计算列 建立计算列
```sql ```sql
alter table payment add day date generated always as (date(payment_date)) stored; alter table payment add day date
generated always as (date(payment_date)) stored;
``` ```
然后使用它改写查询 然后使用它改写查询
......
...@@ -28,13 +28,14 @@ create table orders ...@@ -28,13 +28,14 @@ create table orders
## 答案 ## 答案
```sql ```sql
with recursive r(id) as (select id with recursive r(id) as (
from orders select id
where id = $1 from orders
union where id = $1
select d.id union
from orders as d select d.id
join r on d.id = r.id + 1) from orders as d
join r on d.id = r.id + 1)
select orders.id, content select orders.id, content
from orders from orders
join r on orders.id = r.id; join r on orders.id = r.id;
...@@ -50,8 +51,8 @@ from orders ...@@ -50,8 +51,8 @@ from orders
```sql ```sql
select data.id, content select data.id, content
from orders from orders
join orders as r on orders.id = r.id - 1 join orders as r on orders.id = r.id - 1
where id = $1; where id = $1;
``` ```
...@@ -60,7 +61,10 @@ where id = $1; ...@@ -60,7 +61,10 @@ where id = $1;
```sql ```sql
select id, content select id, content
from orders from orders
where id in (select id from orders where id = id + 1); where id in (
select id
from orders
where id = id + 1);
``` ```
### D ### D
......
...@@ -25,7 +25,13 @@ create table employee ...@@ -25,7 +25,13 @@ create table employee
```sql ```sql
select id, name, dept, salary select id, name, dept, salary
from (select id, name, dept, salary, rank() over (partition by dept order by salary desc) as r from (select id,
name,
dept,
salary,
rank() over (
partition by dept
order by salary desc) as r
from employee) as t from employee) as t
where r <= 5; where r <= 5;
``` ```
...@@ -58,16 +64,23 @@ where count(r.id) <= 5; ...@@ -58,16 +64,23 @@ where count(r.id) <= 5;
```sql ```sql
select l.id, l.name, l.dept, l.salary select l.id, l.name, l.dept, l.salary
from employee as l from employee as l
join (select max(salary, 5) as salary, dept join (
from employee select max(salary, 5) as salary, dept
group by dept) as r from employee
on l.dept = r.dept and l.salary = r.salary group by dept) as r
on l.dept = r.dept and l.salary = r.salary
``` ```
### 结构错误 ### 结构错误
```sql ```sql
select id, name, dept, salary, rank() over (partition by dept order by salary desc) as r select id,
name,
dept,
salary,
rank() over (
partition by dept
order by salary desc) as r
from employee from employee
where r <= 5; where r <= 5;
``` ```
...@@ -75,7 +88,13 @@ where r <= 5; ...@@ -75,7 +88,13 @@ where r <= 5;
### 结构错误 ### 结构错误
```sql ```sql
select id, name, dept, salary, rank() as r over (partition by dept order by salary desc) select id,
name,
dept,
salary,
rank() as r over (
partition by dept
order by salary desc)
from employee from employee
where r <= 5; where r <= 5;
``` ```
...@@ -9,7 +9,9 @@ ...@@ -9,7 +9,9 @@
"export": [ "export": [
"pivot.json" "pivot.json"
], ],
"keywords_must": ["mysql"], "keywords_must": [
"mysql"
],
"keywords_forbid": [], "keywords_forbid": [],
"group": 1 "group": 1
} }
\ No newline at end of file
...@@ -25,18 +25,30 @@ create index on sales(created_at); ...@@ -25,18 +25,30 @@ create index on sales(created_at);
```sql ```sql
select sku_id, 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)
sum(case extract(month from created_at) when 2 then amount else 0 end) as Feb, when 1 then amount else 0 end) as Jan,
sum(case extract(month from created_at) when 3 then amount else 0 end) as Mar, sum(case extract(month from created_at)
sum(case extract(month from created_at) when 4 then amount else 0 end) as Apr, when 2 then amount else 0 end) as Feb,
sum(case extract(month from created_at) when 5 then amount else 0 end) as May, sum(case extract(month from created_at)
sum(case extract(month from created_at) when 6 then amount else 0 end) as June, when 3 then amount else 0 end) as Mar,
sum(case extract(month from created_at) when 7 then amount else 0 end) as July, sum(case extract(month from created_at)
sum(case extract(month from created_at) when 8 then amount else 0 end) as Aug, when 4 then amount else 0 end) as Apr,
sum(case extract(month from created_at) when 9 then amount else 0 end) as Sept, sum(case extract(month from created_at)
sum(case extract(month from created_at) when 10 then amount else 0 end) as Oct, when 5 then amount else 0 end) as May,
sum(case extract(month from created_at) when 11 then amount else 0 end) as Nov, sum(case extract(month from created_at)
sum(case extract(month from created_at) when 12 then amount else 0 end) as Dec 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 from sales
where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp
group by sku_id; group by sku_id;
...@@ -78,18 +90,30 @@ group by sku_id, extract(month from created_at); ...@@ -78,18 +90,30 @@ group by sku_id, extract(month from created_at);
```sql ```sql
select sku_id, select sku_id,
sum(amount having extract(month from created_at) = 1) as Jan, sum(amount having
sum(amount having extract(month from created_at) = 2) as Feb, extract(month from created_at) = 1) as Jan,
sum(amount having extract(month from created_at) = 3) as Mar, sum(amount having
sum(amount having extract(month from created_at) = 4) as Apr, extract(month from created_at) = 2) as Feb,
sum(amount having extract(month from created_at) = 5) as May, sum(amount having
sum(amount having extract(month from created_at) = 6) as June, extract(month from created_at) = 3) as Mar,
sum(amount having extract(month from created_at) = 7) as July, sum(amount having
sum(amount having extract(month from created_at) = 8) as Aug, extract(month from created_at) = 4) as Apr,
sum(amount having extract(month from created_at) = 9) as Sept, sum(amount having
sum(amount having extract(month from created_at) = 10) as Oct, extract(month from created_at) = 5) as May,
sum(amount having extract(month from created_at) = 11) as Nov, sum(amount having
sum(amount having extract(month from created_at) = 12) as Dec 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 from sales
where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp
group by sku_id, extract(month from created_at); group by sku_id, extract(month from created_at);
......
...@@ -46,14 +46,14 @@ Joe 想要找出参与了所有投标的企业(有围标嫌疑),那么这 ...@@ -46,14 +46,14 @@ Joe 想要找出参与了所有投标的企业(有围标嫌疑),那么这
select * select *
from company from company
where not exists( where not exists(
select * select *
from invitation from invitation
where not exists( where not exists(
select * select *
from bids from bids
where invitation.id = bids.invitation_id where invitation.id = bids.invitation_id
and bids.company_id = company.id and bids.company_id = company.id
) )
) )
``` ```
...@@ -82,7 +82,9 @@ where id = any (select company_id ...@@ -82,7 +82,9 @@ where id = any (select company_id
```sql ```sql
select * select *
from company from company
where company.id in (select distinct company_id from bids) where company.id in (
select distinct company_id
from bids)
``` ```
### D ### D
...@@ -91,8 +93,8 @@ where company.id in (select distinct company_id from bids) ...@@ -91,8 +93,8 @@ where company.id in (select distinct company_id from bids)
select * select *
from company from company
where exists( where exists(
select * select *
from bids from bids
where bids.company_id = company.id where bids.company_id = company.id
) )
``` ```
\ No newline at end of file
...@@ -9,7 +9,9 @@ ...@@ -9,7 +9,9 @@
"export": [ "export": [
"DoubleNotExists.json" "DoubleNotExists.json"
], ],
"keywords_must": ["mysql"], "keywords_must": [
"mysql"
],
"keywords_forbid": [], "keywords_forbid": [],
"groups": 2 "groups": 2
} }
\ No newline at end of file
...@@ -22,7 +22,8 @@ create table book_in( ...@@ -22,7 +22,8 @@ create table book_in(
## 答案 ## 答案
```sql ```sql
insert into book_in(login, score) values ($1, $2) on DUPLICATE KEY update score=$2; insert into book_in(login, score)
values ($1, $2) on DUPLICATE KEY update score=$2;
``` ```
## 选项 ## 选项
......
...@@ -74,7 +74,9 @@ end; ...@@ -74,7 +74,9 @@ end;
create procedure make_trade() create procedure make_trade()
begin begin
DECLARE done INT DEFAULT FALSE; DECLARE done INT DEFAULT FALSE;
declare cur_orders cursor for select id, price, item_id, amount from orders order by id limit 1000; declare cur_orders cursor for
select id, price, item_id, amount
from orders order by id limit 1000;
declare order_id, item_id, amount INT; declare order_id, item_id, amount INT;
declare price decimal(12, 4); declare price decimal(12, 4);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
...@@ -100,7 +102,11 @@ end; ...@@ -100,7 +102,11 @@ end;
```sql ```sql
create procedure make_trade() create procedure make_trade()
begin begin
declare cur_orders cursor for select id, price, item_id, amount from orders order by id limit 1000; declare cur_orders cursor for
select id, price, item_id, amount
from orders
order by id
limit 1000;
declare order_id, item_id, amount INT; declare order_id, item_id, amount INT;
declare price decimal(12, 4); declare price decimal(12, 4);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
...@@ -122,7 +128,11 @@ end; ...@@ -122,7 +128,11 @@ end;
create procedure make_trade() create procedure make_trade()
begin begin
DECLARE done INT DEFAULT FALSE; DECLARE done INT DEFAULT FALSE;
declare cur_orders cursor for select id, price, item_id, amount from orders order by id limit 1000; declare cur_orders cursor for
select id, price, item_id, amount
from orders
order by id
limit 1000;
declare order_id, item_id, amount INT; declare order_id, item_id, amount INT;
declare price decimal(12, 4); declare price decimal(12, 4);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
...@@ -143,7 +153,11 @@ end; ...@@ -143,7 +153,11 @@ end;
create procedure make_trade() create procedure make_trade()
begin begin
DECLARE done INT DEFAULT FALSE; DECLARE done INT DEFAULT FALSE;
declare cur_orders cursor for select id, price, item_id, amount from orders order by id limit 1000; declare cur_orders cursor for
select id, price, item_id, amount
from orders
order by id
limit 1000;
declare order_id, item_id, amount INT; declare order_id, item_id, amount INT;
declare price decimal(12, 4); declare price decimal(12, 4);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
......
...@@ -19,24 +19,16 @@ ...@@ -19,24 +19,16 @@
### A ### A
```
"DECLARE 游标名字 CURSOR FOR 查询语句;" 用来声明游标 "DECLARE 游标名字 CURSOR FOR 查询语句;" 用来声明游标
```
### B ### B
```
定义好游标之后,需要使用"OPEN 游标名;"来打开游标 定义好游标之后,需要使用"OPEN 游标名;"来打开游标
```
### C ### C
```
游标使用有需要关闭"CLOSE 游标名;" 游标使用有需要关闭"CLOSE 游标名;"
```
### D ### D
```
游标就像指针一样,可以定位操作查询的数据 游标就像指针一样,可以定位操作查询的数据
```
\ No newline at end of file
...@@ -30,7 +30,8 @@ column_name data_type [GENERATED ALWAYS] AS (expression) ...@@ -30,7 +30,8 @@ column_name data_type [GENERATED ALWAYS] AS (expression)
## 答案 ## 答案
```sql ```sql
alter table points add modulus double generated always as (sqrt(x*x + y*y)); alter table points add modulus double
generated always as (sqrt(x*x + y*y));
``` ```
## 选项 ## 选项
...@@ -38,17 +39,20 @@ alter table points add modulus double generated always as (sqrt(x*x + y*y)); ...@@ -38,17 +39,20 @@ alter table points add modulus double generated always as (sqrt(x*x + y*y));
### A ### A
```sql ```sql
create generated modulus on table points as (sqrt(x*x + y*y)); create generated modulus
on table points as (sqrt(x*x + y*y));
``` ```
### B ### B
```sql ```sql
alter table points add modulus generated always as (sqrt(x*x + y*y)); alter table points
add modulus generated always as (sqrt(x*x + y*y));
``` ```
### C ### C
```sql ```sql
alter table points add modulus float generated sqrt(x*x + y*y); alter table points
add modulus float generated sqrt(x*x + y*y);
``` ```
...@@ -38,26 +38,35 @@ group by date(payment_date); ...@@ -38,26 +38,35 @@ group by date(payment_date);
建立中间表并建立索引。 建立中间表并建立索引。
```postgresql ```postgresql
create table daily_payment(day date primary key , amount decimal(12, 4)); create table daily_payment(
day date primary key ,
amount decimal(12, 4));
insert into daily_payment(day, amount) insert into daily_payment(day, amount)
select payment_date::date as day, sum(amount) as amount from payment group by day; select payment_date::date as day,
sum(amount) as amount
from payment group by day;
``` ```
使用 使用
```sql ```sql
select day, amount from view_daily_payment where day between $1 and $2; select day, amount
from view_daily_payment
where day between $1 and $2;
``` ```
进行查询。并且每天定时执行一次刷新命令 进行查询。并且每天定时执行一次刷新命令
```sql ```sql
insert into daily_payment(day, amount) insert into daily_payment(day, amount)
select date(payment_date) as day, sum(amount) as amount select date(payment_date) as day,
sum(amount) as amount
from payment from payment
where date(payment_date) between DATE_SUB(CURDATE(), INTERVAL 2 DAY) and DATE_SUB(CURDATE(), INTERVAL 1 DAY) where date(payment_date)
between DATE_SUB(CURDATE(), INTERVAL 2 DAY)
and DATE_SUB(CURDATE(), INTERVAL 1 DAY)
group by day; group by day;
``` ```
...@@ -69,7 +78,8 @@ group by day; ...@@ -69,7 +78,8 @@ group by day;
在 payment_date 列上建立索引 在 payment_date 列上建立索引
```sql ```sql
create index idx_payment_date on payment(payment_date); create index idx_payment_date on
payment(payment_date);
``` ```
### 不会优化 sum ### 不会优化 sum
...@@ -77,7 +87,8 @@ create index idx_payment_date on payment(payment_date); ...@@ -77,7 +87,8 @@ create index idx_payment_date on payment(payment_date);
建立计算列 建立计算列
```sql ```sql
alter table payment add day date generated always as ( payment_date::date ) stored alter table payment add day date
generated always as ( payment_date::date ) stored
``` ```
然后使用它改写查询 然后使用它改写查询
...@@ -94,7 +105,8 @@ group by day; ...@@ -94,7 +105,8 @@ group by day;
建立表达式索引 建立表达式索引
```sql ```sql
create index idx_payment_day on payment((date(payment_date))); create index idx_payment_day
on payment((date(payment_date)));
``` ```
### 不做物化,对查询速度不会有显著改善 ### 不做物化,对查询速度不会有显著改善
...@@ -102,7 +114,8 @@ create index idx_payment_day on payment((date(payment_date))); ...@@ -102,7 +114,8 @@ create index idx_payment_day on payment((date(payment_date)));
建立视图 建立视图
```sql ```sql
create view view_daily_payment as select date(payment_date) as day, amount from payment; create view view_daily_payment as
select date(payment_date) as day, amount from payment;
``` ```
然后在视图 view_daily_payment 上执行 然后在视图 view_daily_payment 上执行
......
...@@ -84,7 +84,8 @@ SET GLOBAL replicate-ignore-db=goods,auth; ...@@ -84,7 +84,8 @@ SET GLOBAL replicate-ignore-db=goods,auth;
可以在从库执行以下命令,设定忽略 goods 数据库和 auth 数据库 可以在从库执行以下命令,设定忽略 goods 数据库和 auth 数据库
```sql ```sql
CHANGE REPLICATION FILTER REPLICATE_IGNORE_DB=(goods, auth); CHANGE REPLICATION FILTER
REPLICATE_IGNORE_DB=(goods, auth);
``` ```
### J ### J
...@@ -108,7 +109,8 @@ SET GLOBAL replicate-do-table=goods.goods,goods.orders; ...@@ -108,7 +109,8 @@ SET GLOBAL replicate-do-table=goods.goods,goods.orders;
可以在在从库的 MySQL 命令行设定只同步 goods 表和 orders 表 可以在在从库的 MySQL 命令行设定只同步 goods 表和 orders 表
```sql ```sql
CHANGE REPLICATION FILTER REPLICATE_DO_TABLE=( goods.goods,goods.orders); CHANGE REPLICATION FILTER
REPLICATE_DO_TABLE=( goods.goods,goods.orders);
``` ```
### M ### M
...@@ -124,7 +126,8 @@ replicate-do-table=goods.goods,goods.orders ...@@ -124,7 +126,8 @@ replicate-do-table=goods.goods,goods.orders
可以在在从库的 MySQL 命令行设定只同步名称前缀为 `t_` 的表 可以在在从库的 MySQL 命令行设定只同步名称前缀为 `t_` 的表
```sql ```sql
CHANGE REPLICATION FILTER REPLICATE_WILD_DO_TABLE =( goods.t_%); CHANGE REPLICATION FILTER
REPLICATE_WILD_DO_TABLE =( goods.t_%);
``` ```
### P ### P
...@@ -140,7 +143,8 @@ SET GLOBAL replicate-ignore-table=goods.orders,goods.trade; ...@@ -140,7 +143,8 @@ SET GLOBAL replicate-ignore-table=goods.orders,goods.trade;
可以在从库的 MySQL 命令行设定忽略 orders 和 trade 表 可以在从库的 MySQL 命令行设定忽略 orders 和 trade 表
```sql ```sql
CHANGE REPLICATION FILTER REPLICATE_ IGNORE_TABLE=( goods.orders,goods.trade); CHANGE REPLICATION FILTER
REPLICATE_ IGNORE_TABLE=( goods.orders,goods.trade);
``` ```
...@@ -157,7 +161,8 @@ replicate-ignore-table =goods.orders,goods.trade ...@@ -157,7 +161,8 @@ replicate-ignore-table =goods.orders,goods.trade
可以在在从库的 MySQL 命令行设定忽略名称前缀为 `o_` 的表 可以在在从库的 MySQL 命令行设定忽略名称前缀为 `o_` 的表
```sql ```sql
CHANGE REPLICATION FILTER REPLICATE_WILD_DO_TABLE =( goods.o_%); CHANGE REPLICATION FILTER
REPLICATE_WILD_DO_TABLE =( goods.o_%);
``` ```
### S ### S
......
...@@ -3,13 +3,14 @@ ...@@ -3,13 +3,14 @@
Joe 从交易服务中发现了一些高频查询和慢查询,例如 Joe 从交易服务中发现了一些高频查询和慢查询,例如
```sql ```sql
with recursive r(id) as (select id with recursive r(id) as (
from orders select id
where id = $1 from orders
union where id = $1
select d.id union
from orders as d select d.id
join r on d.id = r.id + 1) from orders as d
join r on d.id = r.id + 1)
select orders.id, content select orders.id, content
from orders from orders
join r on orders.id = r.id; join r on orders.id = r.id;
...@@ -29,13 +30,14 @@ from orders ...@@ -29,13 +30,14 @@ from orders
在测试库使用 在测试库使用
```sql ```sql
explain with recursive r(id) as (select id explain with recursive r(id) as (
from orders select id
where id = $1 from orders
union where id = $1
select d.id union
from orders as d select d.id
join r on d.id = r.id + 1) from orders as d
join r on d.id = r.id + 1)
select orders.id, content select orders.id, content
from orders from orders
join r on orders.id = r.id; join r on orders.id = r.id;
...@@ -44,13 +46,14 @@ from orders ...@@ -44,13 +46,14 @@ from orders
进行剖分,对于需要了解详细执行计划的使用 进行剖分,对于需要了解详细执行计划的使用
```sql ```sql
explain analyze with recursive r(id) as (select id explain analyze with recursive r(id) as (
from orders select id
where id = 100 from orders
union where id = 100
select d.id union
from orders as d select d.id
join r on d.id = r.id + 1) from orders as d
join r on d.id = r.id + 1)
select orders.id select orders.id
from orders from orders
join r on orders.id = r.id; join r on orders.id = r.id;
...@@ -73,13 +76,14 @@ from orders ...@@ -73,13 +76,14 @@ from orders
使用 使用
```sql ```sql
analyze with recursive r(id) as (select id analyze with recursive r(id) as (
from orders select id
where id = 100 from orders
union where id = 100
select d.id union
from orders as d select d.id
join r on d.id = r.id + 1) from orders as d
join r on d.id = r.id + 1)
select orders.id select orders.id
from orders from orders
join r on orders.id = r.id; join r on orders.id = r.id;
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册