提交 ff78e9ab 编写于 作者: 张志晨

fix bug

上级 fab1565d
......@@ -24,7 +24,7 @@ sudo su
mysql mysql
```
创建数据库用户 joe 并授权:
```mysql
```sql
create user 'joe'@'localhost' identified by 'joe';
grant all privileges on *.* to `joe`@`localhost`;
flush privileges ;
......@@ -40,7 +40,7 @@ flush privileges ;
mysql mysql
```
为 joe 授权
```mysql
```sql
grant all privileges on *.* to joe;
flush privileges ;
```
......@@ -54,7 +54,7 @@ mysql mysql
```
为 joe 授权
```mysql
```sql
grant all privileges on *.* to joe;
flush privileges ;
```
......@@ -67,7 +67,7 @@ mysql mysql
```
创建数据库用户 joe
```mysql
```sql
create user 'joe'@'localhost' identified by 'joe';
flush privileges ;
```
......@@ -80,7 +80,7 @@ sudo su
mysql mysql
```
创建数据库用户 joe 并授权:
```mysql
```sql
create user 'joe'@'%' identified by 'joe';
grant all privileges on *.* to joe;
flush privileges ;
......
......@@ -11,7 +11,7 @@ Joe 在开发机上创建了一个名为 goods 的数据库,做了一些练习
## 答案
```mysql
```sql
drop database goods;
create database goods;
```
......@@ -20,14 +20,14 @@ create database goods;
### A
```mysql
```sql
delete database goods;
create database goods;
```
### B
```mysql
```sql
if exists(database goods) then
begin
drop database goods;
......@@ -37,7 +37,7 @@ create database goods;
### C
```mysql
```sql
use goods;
drop database goods;
create database goods;
......@@ -45,7 +45,7 @@ create database goods;
### D
```mysql
```sql
use goods;
drop database goods;
make database goods;
......@@ -53,14 +53,14 @@ make database goods;
### E
```mysql
```sql
drop database goods;
make database goods;
```
### F
```mysql
```sql
cd goods;
drop database goods;
create database goods;
......
......@@ -11,7 +11,7 @@ Joe 需要重建一个 id 为自增字段的 goods_category 。他已经删除
## 答案
```mysql
```sql
CREATE TABLE goods_category
(
id INT PRIMARY KEY AUTO_INCREMENT,
......@@ -24,7 +24,7 @@ CREATE TABLE goods_category
### A
```mysql
```sql
CREATE TABLE goods_category
(
id INT PRIMARY KEY SERIALS,
......@@ -35,7 +35,7 @@ CREATE TABLE goods_category
### B
```mysql
```sql
CREATE TABLE goods_category
(
id INT ,
......@@ -48,7 +48,7 @@ CREATE TABLE goods_category
### C
```mysql
```sql
CREATE TABLE goods_category
(
id INT PRIMARY KEY,
......@@ -59,7 +59,7 @@ CREATE TABLE goods_category
### D
```mysql
```sql
CREATE TABLE goods_category
(
id INT PRIMARY KEY SERIALS,
......
......@@ -11,7 +11,7 @@ Joe 想要在 goods 数据库创建一个 goods_category 表,管理商品的
## 答案
```mysql
```sql
CREATE TABLE goods_category
(
id INT PRIMARY KEY ,
......@@ -24,7 +24,7 @@ CREATE TABLE goods_category
### A
```mysql
```sql
MAKE TABLE goods_category
(
id INT PRIMARY KEY ,
......@@ -35,7 +35,7 @@ MAKE TABLE goods_category
### B
```mysql
```sql
DROP TABLE goods_category
(
id INT PRIMARY KEY,
......@@ -46,7 +46,7 @@ DROP TABLE goods_category
### C
```mysql
```sql
SAVE TABLE goods_category
(
id INT PRIMARY KEY,
......@@ -57,7 +57,7 @@ SAVE TABLE goods_category
### D
```mysql
```sql
ADD TABLE goods_category
(
id INT PRIMARY KEY,
......@@ -68,7 +68,7 @@ ADD TABLE goods_category
### E
```mysql
```sql
PUT TABLE goods_category
(
id INT PRIMARY KEY,
......
......@@ -11,7 +11,7 @@ Joe 想要删除数据库中的 good_category 表,他应该怎么操作?
## 答案
```mysql
```sql
drop table goods_category;
```
......@@ -19,30 +19,30 @@ drop table goods_category;
### A
```mysql
```sql
delete table good_category;
```
### B
```mysql
```sql
delete table where name = 'good_category';
```
### C
```mysql
```sql
remove table good_category;
```
### D
```mysql
```sql
truncate table goods_category;
```
### E
```mysql
```sql
clean table goods_category;
```
......@@ -11,7 +11,7 @@ Joe 需要确保 goods_category 表的存储引擎为 innodb ,那么建表语
## 答案
```mysql
```sql
CREATE TABLE goods_category
(
id INT,
......@@ -24,7 +24,7 @@ CREATE TABLE goods_category
### A
```mysql
```sql
CREATE TABLE goods_category
(
id INT,
......@@ -35,7 +35,7 @@ CREATE TABLE goods_category
### B
```mysql
```sql
WITH ENGINE=INNODB CREATE TABLE goods_category
(
id INT,
......@@ -46,7 +46,7 @@ WITH ENGINE=INNODB CREATE TABLE goods_category
### C
```mysql
```sql
SAVE TABLE goods_category
(
id INT,
......@@ -57,7 +57,7 @@ SAVE TABLE goods_category
### D
```mysql
```sql
CREATE TABLE goods_category
(
id INT,
......
......@@ -9,7 +9,7 @@ Joe 需要使用下列表做一项数值计算
* `show databases;` 列出所有数据库
* `show tables;` 列出所有表
```mysql
```sql
create table points(
id int primary key auto_increment,
x int,
......@@ -19,7 +19,7 @@ create table points(
计算查询为:
```mysql
```sql
select id, (x^2 + y^2)/2 as result from points;
```
......
......@@ -2,7 +2,7 @@
Joe 写了一个订单表的创建语句:
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -23,7 +23,7 @@ create table orders (
## 答案
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -38,7 +38,7 @@ create table orders (
### A
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -51,7 +51,7 @@ create table orders (
### B
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -64,7 +64,7 @@ create table orders (
### C
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -77,7 +77,7 @@ create table orders (
### D
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......
......@@ -2,7 +2,7 @@
Joe 在设计订单表,他已经完成了下列内容:
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -24,7 +24,7 @@ create table orders (
## 答案
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -40,7 +40,7 @@ create table orders (
### A
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -54,7 +54,7 @@ create table orders (
### B
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -68,7 +68,7 @@ create table orders (
### C
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -82,7 +82,7 @@ create table orders (
### D
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......
......@@ -2,7 +2,7 @@
现在 Joe 的订单表已经有了如下形态:
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -25,7 +25,7 @@ create table orders (
## 答案
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -42,7 +42,7 @@ create table orders (
### A
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -57,7 +57,7 @@ create table orders (
### B
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -72,7 +72,7 @@ create table orders (
### C
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -87,7 +87,7 @@ create table orders (
### D
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......
......@@ -2,7 +2,7 @@
Goods 数据库中有一个表:
```mysql
```sql
create table book(
id int primary key auto_increment,
title varchar(200) not null ,
......@@ -25,7 +25,7 @@ create unique index idx_book_isbn on book(isbn);
## 答案
```mysql
```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, price, isbn, publish_at) select 'a other book title', 25.4, 'yy-yyyy-xxxx', '2019-12-1';
```
......@@ -34,28 +34,28 @@ insert into book(title, price, isbn, publish_at) select 'a other book title', 25
### 唯一键冲突
```mysql
```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, price, isbn, publish_at) select 'a other book title', 35.4, 'xx-xxxx-xxxx', '2019-12-1';
```
### 缺少必要的列
```mysql
```sql
insert into book(price, 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';
```
### 类型错误
```mysql
```sql
insert into book(title, 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';
```
### 违反非空约束
```mysql
```sql
insert into book(title, 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';
```
......@@ -2,7 +2,7 @@
现有 employee 表如下:
```mysql
```sql
create table employee
(
id serial primary key,
......@@ -23,7 +23,7 @@ Joe 希望修改销售部(dept 字段为 sale)员工 Dora Muk 的工资,将其
## 答案
```mysql
```sql
update employee set salary = salary + 1000 where dept = 'sale' and name = 'Dora Muk';
```
......@@ -31,20 +31,20 @@ update employee set salary = salary + 1000 where dept = 'sale' and name = 'Dora
### 过滤条件不严谨
```mysql
```sql
update employee set salary = salary + 1000 where name = 'Dora Muk';
```
### 缺少过滤条件
```mysql
```sql
update employee set salary = salary + 1000;
```
### 错误的赋值语句
```mysql
```sql
update employee set salary += 1000;
```
......@@ -2,7 +2,7 @@
现在 orders 表结构如下:
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -28,7 +28,7 @@ create table orders (
在一个独立的定时任务中执行
```mysql
```sql
delete
from orders
where deal;
......@@ -40,7 +40,7 @@ where deal;
在一个独立的定时任务中执行
```mysql
```sql
truncate orders;
```
......@@ -48,7 +48,7 @@ truncate orders;
在一个独立的定时任务中执行
```mysql
```sql
delete
from orders;
```
......@@ -57,7 +57,7 @@ from orders;
在一个独立的定时任务中执行
```mysql
```sql
drop table orders;
create table if not exists orders (
id int primary key auto_increment,
......@@ -75,7 +75,7 @@ create table if not exists orders (
建立视图
```mysql
```sql
create view order_view as
select id, meta, content, created_at
from orders
......@@ -88,7 +88,7 @@ where not deal;
在一个独立的定时任务中执行
```mysql
```sql
delete
from orders
where deal;
......
......@@ -11,7 +11,7 @@
## 答案
```mysql
```sql
from test select abc;
```
......@@ -20,31 +20,31 @@ from test select abc;
### A
```mysql
```sql
select 3.14;
```
### B
```mysql
```sql
select * from employee;
```
### C
```mysql
```sql
select * from employee where dept = 'hr';
```
### D
```mysql
```sql
select id, name, dept, salary from employee where salary > 10000;
```
### E
```mysql
```sql
select now();
```
......@@ -2,7 +2,7 @@
Joe 希望从 orders 表
```mysql
```sql
create table orders
(
id int primary key auto_increment,
......@@ -27,7 +27,7 @@ create table orders
## 答案
```mysql
```sql
select id
from orders
where date(ts) = '2022-05-25'
......@@ -38,7 +38,7 @@ where date(ts) = '2022-05-25'
### A
```mysql
```sql
select id
from (select * from orders where date(ts) = '2022-05-25') as o
where unit_prise < 20;
......@@ -46,7 +46,7 @@ where unit_prise < 20;
### B
```mysql
```sql
select id
from orders
where date(ts) = '2022-05-25'
......@@ -55,7 +55,7 @@ where date(ts) = '2022-05-25'
### C
```mysql
```sql
select id
from orders
if date(ts) = '2022-05-25' or unit_prise < 20;
......@@ -63,7 +63,7 @@ if date(ts) = '2022-05-25' or unit_prise < 20;
### D
```mysql
```sql
select id
from orders
which date(ts) = '2022-05-25'
......
......@@ -2,7 +2,7 @@
Points 表结构如下:
```mysql
```sql
create table points(
id int primary key auto_increment,
x float,
......@@ -21,7 +21,7 @@ create table points(
## 答案
```mysql
```sql
select id, sqrt(x^2 + y^2) from points;
```
......@@ -29,24 +29,24 @@ select id, sqrt(x^2 + y^2) from points;
### A
```mysql
```sql
select sqrt(vx+vy) from (select x^2 as vx, y^2 as vy from points) as t;
```
### B
```mysql
```sql
select sqrt(vx + vy) from points where x^2 as vx, y^2 as vy ;
```
### C
```mysql
```sql
select id + sqrt(x^2 + y^2) from points;
```
### D
```mysql
```sql
select id || sqrt(x^2 + y^2) from points;
```
\ No newline at end of file
......@@ -12,7 +12,7 @@
## 答案
```mysql
```sql
create table trade (
id int primary key auto_increment,
content varchar(8000),
......@@ -24,7 +24,7 @@ create table trade (
### 主键没有设置自增,不符合题意
```mysql
```sql
create table trade (
id integer primary key,
content varchar(8000),
......@@ -34,7 +34,7 @@ create table trade (
### 时间戳没有设置默认值
```mysql
```sql
create table trade (
id serial primary key,
content varchar(8000),
......@@ -44,7 +44,7 @@ create table trade (
### 没有主键,不符合题设
```mysql
```sql
create table trade (
id serial,
content varchar(8000),
......
......@@ -2,7 +2,7 @@
Joe 需要给 goods 表
```mysql
```sql
create table goods(
id int primary key auto_increment,
category_id int,
......@@ -25,7 +25,7 @@ create table goods(
## 答案
```mysql
```sql
CREATE VIEW view_name_price
AS
SELECT name, price
......@@ -37,7 +37,7 @@ CREATE VIEW view_name_price
### A
```mysql
```sql
CREATE VIEW view_name_price
AS
SELECT name, price
......@@ -46,7 +46,7 @@ CREATE VIEW view_name_price
### B
```mysql
```sql
CREATE VIEW view_name_price
AS
SELECT *
......@@ -56,7 +56,7 @@ CREATE VIEW view_name_price
### C
```mysql
```sql
CREATE VIEW view_name_price
AS
BEGIN
......@@ -68,7 +68,7 @@ END;
### D
```mysql
```sql
CREATE VIEW view_name_price
AS
BEGIN
......
......@@ -12,7 +12,7 @@
## 答案
```mysql
```sql
create function individual_income_tax(salary decimal(12, 4)) returns decimal(12, 4)
deterministic
begin
......@@ -24,7 +24,7 @@ end;
### A
```mysql
```sql
create store function individual_income_tax(salary decimal(12, 4)) returns decimal(12, 4)
deterministic
begin
......@@ -34,7 +34,7 @@ end;
### B
```mysql
```sql
create function individual_income_tax(salary decimal(12, 4))
deterministic
begin
......@@ -45,7 +45,7 @@ end;
### C
```mysql
```sql
create function decimal(12, 4) individual_income_tax(salary decimal(12, 4))
begin
-- ...
......
......@@ -13,7 +13,7 @@ Joe 希望这个计算更紧凑一些,在已经有 individual_income_tax 的
## 答案
```mysql
```sql
create procedure sp_idt(in salary decimal(12, 4), out tax decimal(12, 4), out take_home decimal(12, 4))
begin
set tax = individual_income_tax(salary);
......@@ -25,7 +25,7 @@ end;
### A
```mysql
```sql
create procedure sp_idt(salary decimal(12, 4)) returns (tax decimal(12, 4), take_home decimal(12, 4))
begin
set tax = individual_income_tax(salary);
......@@ -36,7 +36,7 @@ end;
### B
```mysql
```sql
create procedure sp_idt(salary decimal(12, 4)) returns (decimal(12, 4), decimal(12, 4))
begin
declare tax, take_home decimal(12, 4);
......@@ -48,7 +48,7 @@ end;
### C
```mysql
```sql
create procedure sp_idt(in salary decimal(12, 4), out tax decimal(12, 4), out take_home decimal(12, 4)) returns void
begin
set tax = individual_income_tax(salary);
......@@ -58,7 +58,7 @@ end;
### D
```mysql
```sql
create procedure sp_idt(in salary decimal(12, 4))
begin
declare tax, take_home decimal(12, 4);
......
......@@ -11,7 +11,7 @@ Joe 将计税逻辑放到了 sp_idt 中,现在不需要 individual_income_tax
## 答案
```mysql
```sql
drop function individual_income_tax;
```
......@@ -19,20 +19,20 @@ drop function individual_income_tax;
### A
```mysql
```sql
delete function individual_income_tax;
```
### B
```mysql
```sql
remove function individual_income_tax;
```
### C
```mysql
```sql
drop function individual_income_tax(decimal(12, 4));
```
......@@ -2,7 +2,7 @@
现有一个图书登记表:
```mysql
```sql
create table book(
id int primary key auto_increment,
title text,
......
......@@ -2,7 +2,7 @@
现有一个图书登记表:
```mysql
```sql
create table book(
id int primary key auto_increment,
title text,
......
......@@ -2,7 +2,7 @@
Orders 表
```mysql
```sql
create table orders
(
id int primary key auto_increment,
......@@ -17,7 +17,7 @@ create table orders
记录了未完成的订单。审计部门现在需要记录其变更——新增或删除,该表不会发生update——即将修改都记录到 orders_log 表
```mysql
```sql
create table orders_log
(
log_id int primary key auto_increment,
......@@ -44,7 +44,7 @@ create table orders_log
## 答案
```mysql
```sql
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)
values(NEW.id, NEW.item_id, NEW.amount, NEW.unit_price, NEW.total, NEW.description, NEW.ts, 'in');
......@@ -58,7 +58,7 @@ create trigger out_orders after delete on orders
### A
```mysql
```sql
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)
values(NEW.id, NEW.item_id, NEW.amount, NEW.unit_price, NEW.total, NEW.description, NEW.ts, 'in');
......@@ -67,7 +67,7 @@ create trigger in_orders after insert on orders
### B
```mysql
```sql
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)
......@@ -77,7 +77,7 @@ create trigger out_orders after delete on orders
### C
```mysql
```sql
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)
values(NEW.id, NEW.item_id, NEW.amount, NEW.unit_price, NEW.total, NEW.description, NEW.ts, 'in');
......@@ -88,7 +88,7 @@ create trigger in_orders after insert on orders
### D
```mysql
```sql
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)
values(NEW.id, NEW.item_id, NEW.amount, NEW.unit_price, NEW.total, NEW.description, NEW.ts, 'in');
......
......@@ -2,7 +2,7 @@
SmartMarket 公司的OA数据库中包含以下结构:
```mysql
```sql
create table employee(
id serial primary key ,
name varchar(256),
......
......@@ -11,7 +11,7 @@ Goods 表的存储引擎是 MyISAM,Joe 需要把它修改为 InnoDB,他应
## 答案
```mysql
```sql
alter table goods engine innodb;
```
......@@ -19,7 +19,7 @@ alter table goods engine innodb;
### A
```mysql
```sql
alter table goods set engine innodb;
```
......@@ -31,7 +31,7 @@ alter table goods set engine innodb;
### C
```mysql
```sql
alter table goods add engine innodb;
```
......
......@@ -12,7 +12,7 @@ Joe 要给数据组的 John 创建一个用户,他希望John 能够从 `192.16
## 答案
```mysql
```sql
create user 'john'@'192.168.7.42' identified by 'goods123' password expire;
grant select on goods.* to 'john'@'192.168.7.42';
flush privileges;
......@@ -22,7 +22,7 @@ flush privileges;
### A
```mysql
```sql
create user 'john'@'192.168.7.42' identified by 'goods123';
grant select on goods.* to 'john'@'192.168.7.42';
flush privileges;
......@@ -30,7 +30,7 @@ flush privileges;
### B
```mysql
```sql
create user 'john'@'192.168.7.42' identified by 'goods123';
grant usage on goods.* to 'john'@'192.168.7.42';
flush privileges;
......@@ -38,7 +38,7 @@ flush privileges;
### C
```mysql
```sql
create user 'john'@'192.168.7.42' identified by 'goods123' on database goods;
flush privileges;
```
\ No newline at end of file
......@@ -11,7 +11,7 @@
## 答案
```mysql
```sql
grant select on table employee to joe;
```
......@@ -19,30 +19,30 @@ grant select on table employee to joe;
### 权限名错误
```mysql
```sql
grant query on table employee to joe;
```
### 权限名错误
```mysql
```sql
grant read on table employee to joe;
```
### 操作关键词错误
```mysql
```sql
grant select on table employee of joe;
```
### 操作错误
```mysql
```sql
grant select on table employee.* of joe;
```
### 权限过高
```mysql
```sql
grant all on table employee to joe;
```
......@@ -12,7 +12,7 @@ Joe 现在是团队的 DBA,公司数据分析组有 Fred、Alice、James、Jon
## 答案
```mysql
```sql
create role analysis;
grant analysis to fred, alice, james, jone;
grant select on goods.* to analysis;
......@@ -23,13 +23,13 @@ flush privileges;
### 将来人员变动管理会很繁琐
```mysql
```sql
grant select on goods.* to fred, alice, james, jone;
```
### 错误的语法
```mysql
```sql
create role analysis;
grant analysis to fred, alice, james, jone;
grant all on all tables in schema goods to analysis;
......@@ -38,7 +38,7 @@ flush privileges;
### 过度授权
```mysql
```sql
create role analysis;
grant analysis to fred, alice, james, jone;
grant select on *.* to analysis;
......
......@@ -11,7 +11,7 @@ Joe 想要查看 Fred 的 MySQL 账户 `'fred'@'%'` 的权限,他应该怎么
## 答案
```mysql
```sql
show grants for fred;
```
......@@ -19,18 +19,18 @@ show grants for fred;
### A
```mysql
```sql
select grants from fred;
```
### B
```mysql
```sql
show privileges for fred;
```
### B
```mysql
```sql
show privileges from fred;
```
......@@ -12,7 +12,7 @@
## 答案
```mysql
```sql
revoke select on trade from fred;
```
......@@ -20,19 +20,19 @@ revoke select on trade from fred;
### 操作错误
```mysql
```sql
grant not select on trade to fred;
```
### 操作关键字错误
```mysql
```sql
revoke select on trade to fred;
```
### 指定权限错误
```mysql
```sql
revoke owned trade from fred;
```
......
......@@ -13,7 +13,7 @@ Joe 准备将这个账户的口令初始化为 `goods123fred` ,
## 答案
```mysql
```sql
alter user 'fred'@'%' identified by 'goods123fred' password expire;
```
......@@ -21,18 +21,18 @@ alter user 'fred'@'%' identified by 'goods123fred' password expire;
### A
```mysql
```sql
alter user 'fred'@'%' identified by 'goods123fred';
```
### B
```mysql
```sql
alter user 'fred'@'%' set password 'goods123fred';
```
### C
```mysql
```sql
alter user 'fred'@'%' set password 'goods123fred' expire;
```
\ No newline at end of file
......@@ -11,7 +11,7 @@ Jane 从公司离职,Joe 需要从数据库服务器删除她的账号 `'jane'
## 答案
```mysql
```sql
drop user 'jane'@'%';
```
......@@ -19,18 +19,18 @@ drop user 'jane'@'%';
### A
```mysql
```sql
delete user 'jane'@'%';
```
### B
```mysql
```sql
remove user 'jane'@'%';
```
### C
```mysql
```sql
revoke user 'jane'@'%';
```
\ No newline at end of file
......@@ -11,7 +11,7 @@ Joe 需要限制数据分析组(role analysis)的用户, 每小时查询次
## 答案
```mysql
```sql
alter user analysis with MAX_QUERIES_PER_HOUR 10000;
```
......@@ -19,25 +19,25 @@ alter user analysis with MAX_QUERIES_PER_HOUR 10000;
### A
```mysql
```sql
set user analysis with MAX_QUERIES_PER_HOUR 10000;
```
### B
```mysql
```sql
alter role analysis with MAX_QUERIES_PER_HOUR 10000;
```
### C
```mysql
```sql
alter role analysis with MAX_QUERIES_PER_HOUR=10000;
```
### D
```mysql
```sql
alter user analysis set MAX_QUERIES_PER_HOUR 10000;
```
# Between
Joe 要查询 goods 表
```mysql
```sql
create table goods(
id int primary key auto_increment,
category_id int,
......@@ -23,7 +23,7 @@ create table goods(
## 答案
```mysql
```sql
SELECT * FROM goods HAVING price BETWEEN 1000 AND 2000;
```
......@@ -32,19 +32,19 @@ SELECT * FROM goods HAVING price BETWEEN 1000 AND 2000;
### A
```mysql
```sql
SELECT * FROM goods WHERE price BETWEEN 1000 AND 2000;
```
### B
```mysql
```sql
SELECT * FROM goods WHERE price >= 1000 AND price <= 2000;
```
### C
```mysql
```sql
SELECT * FROM goods WHERE not (price < 1000 or price > 2000);
```
......@@ -2,7 +2,7 @@
Goods 表结构如下
```mysql
```sql
create table goods(
id int primary key auto_increment,
category_id int,
......@@ -26,7 +26,7 @@ normal,这个查询应该怎么写?
## 答案
```mysql
```sql
select name,
case
when price < 10 then 'cheap'
......@@ -40,7 +40,7 @@ from goods;
### A
```mysql
```sql
select name,
case price
when < 10 then 'cheap'
......@@ -52,7 +52,7 @@ from goods;
### B
```mysql
```sql
select name,
case
when price < 10 'cheap'
......@@ -64,7 +64,7 @@ from goods;
### C
```mysql
```sql
select name,
case
when price < 10 then 'cheap'
......@@ -76,7 +76,7 @@ from goods;
### C
```mysql
```sql
select name,
case
when price < 10 then 'cheap'
......
......@@ -2,7 +2,7 @@
Joe 想统计以下 goods 表
```mysql
```sql
create table goods(
id int primary key auto_increment,
category_id int,
......@@ -25,7 +25,7 @@ create table goods(
## 答案
```mysql
```sql
select count(distinct price) from goods;
```
......@@ -33,24 +33,24 @@ select count(distinct price) from goods;
### A
```mysql
```sql
select count(distinct *) from goods;
```
### B
```mysql
```sql
select distinct count(price) from goods;
```
### C
```mysql
```sql
select count(price) from goods;
```
### D
```mysql
```sql
select distinct price from goods;
```
......@@ -2,7 +2,7 @@
Joe 需要根据员工表
```mysql
```sql
create table employee
(
id serial primary key,
......@@ -23,7 +23,7 @@ create table employee
## 答案
```mysql
```sql
select id, name, dept, salary from employee order by dept, salary desc;
```
......@@ -31,29 +31,29 @@ select id, name, dept, salary from employee order by dept, salary desc;
### A
```mysql
```sql
select id, name, dept, salary from employee order by dept, salary;
```
### B
```mysql
```sql
select id, name, dept, salary from employee order by dept desc, salary desc;
```
### C
```mysql
```sql
select id, name, dept, salary from employee order by dept and salary desc;
```
### D
```mysql
```sql
select id, name, dept, salary from employee order by dept, salary, id, name;
```
### E
```mysql
```sql
select id, name, dept, salary from employee order by dept, salary desc;
```
......@@ -2,7 +2,7 @@
现有员工信息表和顾客信息表如下
```mysql
```sql
create table employee(
id int primary key auto_increment,
name varchar(256),
......@@ -32,7 +32,7 @@ Joe 需要员工和顾客的联系方式(姓名+地址)清单,用于邮寄
## 答案
```mysql
```sql
select name, address
from customer
union
......@@ -44,7 +44,7 @@ from employee
### A
```mysql
```sql
select *
from customer
union
......@@ -54,7 +54,7 @@ from employee
### B
```mysql
```sql
select *
from customer
join employee
......@@ -62,7 +62,7 @@ join employee
### C
```mysql
```sql
select *
from customer
join employee on customer.id = employee.id
......@@ -70,14 +70,14 @@ join employee on customer.id = employee.id
### D
```mysql
```sql
select *
from customer, employee
```
### E
```mysql
```sql
select name, address
from customer, employee
```
......
......@@ -11,7 +11,7 @@ Joe 想要找出 goods 表中所有名称包含牛奶的冰激凌,他应该怎
## 答案
```mysql
```sql
select *
from goods
where name regexp '牛奶.*冰激凌';
......@@ -21,7 +21,7 @@ where name regexp '牛奶.*冰激凌';
### A
```mysql
```sql
select *
from goods
where name like '牛奶%冰激凌'
......@@ -30,7 +30,7 @@ where name like '牛奶%冰激凌'
### B
```mysql
```sql
select *
from goods
where name like '牛奶.*冰激凌';
......@@ -38,7 +38,7 @@ where name like '牛奶.*冰激凌';
### C
```mysql
```sql
select *
from goods
where name regexp '牛奶冰激凌';
......
......@@ -2,7 +2,7 @@
我们有如下订单表:
```mysql
```sql
create table orders
(
id serial primary key,
......@@ -24,7 +24,7 @@ create table orders
## 答案
```mysql
```sql
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1
......@@ -35,7 +35,7 @@ offset $2 limit 100;
### 缺少 limit
```mysql
```sql
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1
......@@ -44,7 +44,7 @@ offset $2;
### 缺少 offset
```mysql
```sql
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1;
......@@ -52,7 +52,7 @@ where date = $1;
### 结构不对
```mysql
```sql
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1 and
......
......@@ -11,7 +11,7 @@ Joe 想要把字符串表示的整数转为整数类型,可行的方法是:
## 答案
```mysql
```sql
SELECT CAST('123' AS SIGNED);
```
......@@ -19,7 +19,7 @@ SELECT CAST('123' AS SIGNED);
### A
```mysql
```sql
select int('123');
```
......
......@@ -2,7 +2,7 @@
Joe 需要将下面这个查询
```mysql
```sql
select name from goods;
```
......@@ -17,7 +17,7 @@ select name from goods;
## 答案
```mysql
```sql
select convert(name using 'gb18030') from goods;
```
......@@ -26,27 +26,27 @@ select convert(name using 'gb18030') from goods;
### A
```mysql
```sql
select str(name, 'gb18303') from goods;
```
### B
```mysql
```sql
select encode(decode(name, 'utf8mb4'), 'gb18303') from goods;
```
### C
```mysql
```sql
select convert(name from 'utf8mb4' to 'gb18303') from goods;
```
### D
```mysql
```sql
select convert(name to 'gb18303') from goods;
```
......
......@@ -16,7 +16,7 @@ mysql> select * from items;
当他执行
```mysql
```sql
select count(*), count(item) from items;
```
......
......@@ -2,7 +2,7 @@
Joe 想要得到 orders 表
```mysql
```sql
create table orders (
id int primary key auto_increment,
item_id int,
......@@ -26,7 +26,7 @@ create table orders (
## 答案
```mysql
```sql
select sum(total) from orders where deal and unit_price > 1000;
```
......@@ -34,24 +34,24 @@ select sum(total) from orders where deal and unit_price > 1000;
### A
```mysql
```sql
select sum(total) from orders where deal and unit_price > 1000;
```
### B
```mysql
```sql
select sum(total) from orders having deal and unit_price > 1000;
```
### C
```mysql
```sql
select sum(total) from orders group by deal having unit_price > 1000;
```
### D
```mysql
```sql
select sum(total) from orders having deal and unit_price > 1000;
```
\ No newline at end of file
......@@ -2,7 +2,7 @@
Joe 想要得到 employee 表
```mysql
```sql
create table employee
(
id serial primary key,
......@@ -23,7 +23,7 @@ create table employee
## 答案
```mysql
```sql
select dept, min(salary) from employee group by dept;
```
......@@ -31,24 +31,24 @@ select dept, min(salary) from employee group by dept;
### A
```mysql
```sql
select dept, min(salary) from employee;
```
### B
```mysql
```sql
select dept, min(salary) from employee;
```
### C
```mysql
```sql
select dept, min(salary) from employee;
```
### D
```mysql
```sql
select dept, min(total) from employee;
```
\ No newline at end of file
......@@ -2,7 +2,7 @@
利用员工信息表:
```mysql
```sql
create table employee
(
id serial primary key,
......@@ -23,7 +23,7 @@ Joe 做了一些关于 max 函数的练习,其中不正确的是:
## 答案
```mysql
```sql
select id, dept, max(salary) as salary, name
from employee
group by dept
......@@ -35,7 +35,7 @@ group by dept
### A
```mysql
```sql
select dept, max(salary) as salary
from employee
group by dept
......@@ -45,7 +45,7 @@ group by dept
### B
```mysql
```sql
select max(salary) as salary
from employee
```
......@@ -54,7 +54,7 @@ from employee
### C
```mysql
```sql
select dept, sum(salary)
from employee
group by dept
......@@ -65,7 +65,7 @@ having max(salary) < 20000
### D
```mysql
```sql
select dept, min(salary)
from employee
group by dept
......
......@@ -2,7 +2,7 @@
Joe 想要得到 employee 表
```mysql
```sql
create table employee
(
id serial primary key,
......@@ -23,7 +23,7 @@ create table employee
## 答案
```mysql
```sql
select dept, avg(salary) from employee group by dept;
```
......@@ -31,24 +31,24 @@ select dept, avg(salary) from employee group by dept;
### A
```mysql
```sql
select dept, avg(salary) from employee;
```
### B
```mysql
```sql
select dept, avg(salary) from employee;
```
### C
```mysql
```sql
select dept, avg(salary) from employee;
```
### D
```mysql
```sql
select dept, avg(salary) from employee ;
```
\ No newline at end of file
......@@ -2,7 +2,7 @@
Joe 要从 employee 表
```mysql
```sql
create table employee
(
id serial primary key,
......@@ -23,7 +23,7 @@ create table employee
## 答案
```mysql
```sql
select dept from employee group by dept having sum(salary) > 100000;
```
......@@ -31,25 +31,25 @@ select dept from employee group by dept having sum(salary) > 100000;
### A
```mysql
```sql
select dept from employee group by dept where sum(salary) > 100000;
```
### B
```mysql
```sql
select dept from employee where sum(salary) > 100000 group by dept;
```
### C
```mysql
```sql
select dept from employee where sum(salary) > 100000 order by dept;
```
### D
```mysql
```sql
select dept from employee group by dept where sum(salary) > 100000;
```
......
......@@ -2,7 +2,7 @@
现有员工表
```mysql
```sql
create table employee
(
id serial primary key,
......@@ -23,7 +23,7 @@ Joe 希望找出比销售部(dept 为 sale)工资最高的员工工资更高
## 答案
```mysql
```sql
select id, name, dept, salary
from employee
where salary > (select max(salary)
......@@ -35,7 +35,7 @@ where salary > (select max(salary)
### A
```mysql
```sql
select id, name, dept, salary
from employee
where dept = 'sale'
......@@ -45,7 +45,7 @@ having salary > max(salary)
### B
```mysql
```sql
select l.id, l.name, l.dept, l.salary
from employee as l
join employee as r on l.salary > max(r.salary)
......@@ -55,7 +55,7 @@ group by r.dept
### C
```mysql
```sql
select id, name, dept, salary
from employee
having salary > (select max(salary) from employee where dept = 'sale')
......
......@@ -2,7 +2,7 @@
Joe 想要从员工表
```mysql
```sql
create table employee(
id int primary key auto_increment,
name varchar(256),
......@@ -22,7 +22,7 @@ create table employee(
## 答案
```mysql
```sql
select id, name, dept, salary
from employee as o
where o.salary < any(select salary from employee as i where i.dept=o.dept)
......@@ -32,7 +32,7 @@ where o.salary < any(select salary from employee as i where i.dept=o.dept)
### A
```mysql
```sql
select id, name, dept, salary
from employee as o
join employee as i on o.dept = i.dept and o.salary < i.salary
......@@ -40,7 +40,7 @@ join employee as i on o.dept = i.dept and o.salary < i.salary
### B
```mysql
```sql
select o.id, o.name, o.dept, o.salary
from employee as o
left join employee as i on o.dept = i.dept and o.salary < i.salary
......@@ -49,7 +49,7 @@ where i.id is null;
### C
```mysql
```sql
select o.id, o.name, o.dept, o.salary
from employee as o
left join employee as i on o.dept = i.dept and o.salary < i.salary
......
......@@ -2,7 +2,7 @@
Joe 想从员工表
```mysql
```sql
create table employee(
id int primary key auto_increment,
dept_id int,
......@@ -22,7 +22,7 @@ create table employee(
## 答案
```mysql
```sql
select id, name, dept
from employee as o
where 'assistant' != all(select post from employee as i where o.dept = i.dept);
......@@ -32,7 +32,7 @@ where 'assistant' != all(select post from employee as i where o.dept = i.dept);
### A
```mysql
```sql
select id, name, dept
from employee as o
where 'assistant' = all(select post from employee as i where o.dept = i.dept);
......@@ -40,14 +40,14 @@ where 'assistant' = all(select post from employee as i where o.dept = i.dept);
### B
```mysql
```sql
select id, name, dept
from employee as o
where 'assistant' != all(select post from employee as i where o.dept = i.dept);
```
### C
```mysql
```sql
select id, name, dept
from employee as o
where 'assistant' != all(select post from employee as i);
......@@ -55,7 +55,7 @@ where 'assistant' != all(select post from employee as i);
### D
```mysql
```sql
select id, name, dept
from employee as o
where 'assistant' != ANY(select post from employee as i where o.dept = i.dept);
......
......@@ -2,7 +2,7 @@
Joe 想从员工表
```mysql
```sql
create table employee(
id int primary key auto_increment,
dept_id int,
......@@ -23,7 +23,7 @@ create table employee(
## 答案
```mysql
```sql
select id, name, dept
from employee as o
where not exists(select * from employee as i where o.dept = i.dept and post='assistant');
......@@ -33,7 +33,7 @@ where not exists(select * from employee as i where o.dept = i.dept and post='ass
### A
```mysql
```sql
select id, name, dept
from employee as o
where exists(select * from employee as i where o.dept = i.dept and post='assistant');
......@@ -41,14 +41,14 @@ where exists(select * from employee as i where o.dept = i.dept and post='assista
### B
```mysql
```sql
select id, name, dept
from employee as o
where not exists(select * from employee as i where o.dept = i.dept and post='assistant');
```
### C
```mysql
```sql
select id, name, dept
from employee as o
where 'assistant' != exists(select post from employee as i);
......@@ -56,7 +56,7 @@ where 'assistant' != exists(select post from employee as i);
### D
```mysql
```sql
select id, name, dept
from employee as o
where 'assistant' = not exists(select post from employee as i where o.dept = i.dept);
......
......@@ -2,7 +2,7 @@
Joe 想要从员工表
```mysql
```sql
create table employee(
id int primary key auto_increment,
dept_id int,
......@@ -22,7 +22,7 @@ create table employee(
## 答案
```mysql
```sql
select id, dept, name, post
from employee
where dept in ('dev', 'hr');
......@@ -32,7 +32,7 @@ where dept in ('dev', 'hr');
### A
```mysql
```sql
select id, dept, name, post
from employee
where dept in (select 'dev', 'hr');
......@@ -40,7 +40,7 @@ where dept in (select 'dev', 'hr');
### B
```mysql
```sql
select id, dept, name, post
from employee
where dept in (select * from 'dev', 'hr');
......@@ -48,7 +48,7 @@ where dept in (select * from 'dev', 'hr');
### C
```mysql
```sql
select id, dept, name, post
from employee
where dept in ('dev' and 'hr');
......@@ -56,7 +56,7 @@ where dept in ('dev' and 'hr');
#### D
```mysql
```sql
select id, dept, name, post
from employee
where dept in ('dev' or 'hr');
......
......@@ -2,7 +2,7 @@
Joe 打算写一个查询,根据员工表
```mysql
```sql
create table employee
(
id int primary key auto_increment,
......@@ -23,7 +23,7 @@ create table employee
## 答案
```mysql
```sql
select distinct(dept) as dept,
(select count(*)
from employee as i
......@@ -35,7 +35,7 @@ from employee as o;
### A
```mysql
```sql
select distinct(dept) as dept,
(select count(*)
from employee as i) as emp
......@@ -44,7 +44,7 @@ from employee as o;
### B
```mysql
```sql
select distinct(dept) as dept,
(select count(*)
from employee
......@@ -54,7 +54,7 @@ from employee
### C
```mysql
```sql
select dept as dept,
(select count(*)
from employee as i
......
......@@ -2,7 +2,7 @@
现有 node 表如下:
```mysql
```sql
create table node(
id int primary key auto_increment,
pid int,
......@@ -22,7 +22,7 @@ create table node(
## 答案
```mysql
```sql
select l.id as parent_id,
l.content as parent_content,
r.id as child_id,
......@@ -36,7 +36,7 @@ where l.content like 'fork-%';
### A
```mysql
```sql
select l.id as parent_id,
l.content as parent_content,
r.id as child_id,
......@@ -48,7 +48,7 @@ where l.content like 'fork-%';
### B
```mysql
```sql
select l.id as parent_id,
l.content as parent_content,
r.id as child_id,
......@@ -59,7 +59,7 @@ where l.id =(+) r.pid l.content like 'fork-%';
### C
```mysql
```sql
select l.id as parent_id,
l.content as parent_content,
r.id as child_id,
......@@ -71,7 +71,7 @@ where l.content like 'fork-%';
### D
```mysql
```sql
select l.id as parent_id,
l.content as parent_content,
r.id as child_id,
......
......@@ -2,7 +2,7 @@
现有部门表
```mysql
```sql
create table department(
id int primary key auto_increment,
name varchar(256)
......@@ -11,7 +11,7 @@ create table department(
和员工表
```mysql
```sql
create table employee(
id int primary key auto_increment,
dept_id int,
......@@ -31,7 +31,7 @@ Joe 想要列出所有的部门,如果这个部门有部门助理(post 为 `
## 答案
```mysql
```sql
select d.id, d.name, e.name as assistant
from department as d
left join employee as e on e.dept = d.id
......@@ -42,7 +42,7 @@ where e.post = 'assistant'
### A
```mysql
```sql
select d.id, d.name, e.name as assistant
from department as d
cross join employee as e on e.dept = d.id
......@@ -51,7 +51,7 @@ where e.post = 'assistant'
### B
```mysql
```sql
select d.id, d.name, e.name as assistant
from department as d
join employee as e on e.dept = d.id
......@@ -60,7 +60,7 @@ where e.post = 'assistant'
### C
```mysql
```sql
select d.id, d.name, e.name as assistant
from department as d
cross join employee as e on e.dept = d.id
......@@ -69,7 +69,7 @@ where e.post = 'assistant'
### D
```mysql
```sql
select d.id, d.name, e.name as assistant
from employee as e
left join department as d on e.dept = d.id
......
......@@ -2,7 +2,7 @@
现有部门表
```mysql
```sql
create table department(
id int primary key auto_increment,
name varchar(256)
......@@ -11,7 +11,7 @@ create table department(
和员工表
```mysql
```sql
create table employee(
id int primary key auto_increment,
dept_id int,
......@@ -32,7 +32,7 @@ create table employee(
## 答案
```mysql
```sql
select e.id, e.name, e.dept
from department as d
right join employee as e on d.id = e.dept
......@@ -43,7 +43,7 @@ where d.id is null;
### A
```mysql
```sql
select e.id, e.name, e.dept
from employee as e
right join department as d on d.id = e.dept
......@@ -52,7 +52,7 @@ where e.id is null;
### B
```mysql
```sql
select e.id, e.name, e.dept
from employee as e
right join department as d on d.id = e.dept
......@@ -61,7 +61,7 @@ where d.id is null;
### C
```mysql
```sql
select e.id, e.name, e.dept
from department as d
join employee as e on d.id = e.dept
......@@ -71,7 +71,7 @@ where d.id is null;
### D
```mysql
```sql
select e.id, e.name, e.dept
from department as d
right join employee as e on d.id = e.dept
......
......@@ -2,7 +2,7 @@
Joe 需要生成 goods 表
```mysql
```sql
create table goods(
id int primary key auto_increment,
category varchar(64),
......@@ -15,7 +15,7 @@ create table goods(
中所有T恤(category为`T-Shirt`)的所有尺寸,尺寸信息在 size 表
```mysql
```sql
create table size(
id int primary key auto_increment,
name varchar(16)
......@@ -33,7 +33,7 @@ create table size(
## 答案
```mysql
```sql
select g.id, g.name, s.name as size
from goods as g
cross join size as s
......@@ -44,7 +44,7 @@ where g.category = 'T-Shirt';
### A
```mysql
```sql
select g.id, g.name, s.name as size
from goods as g
full join size as s
......@@ -53,7 +53,7 @@ where g.category = 'T-Shirt';
### B
```mysql
```sql
select g.id, g.name, s.name as size
from goods as g
left join size as s
......@@ -62,7 +62,7 @@ where g.category = 'T-Shirt';
### C
```mysql
```sql
select g.id, g.name, s.name as size
from goods as g
left join size as s
......@@ -71,7 +71,7 @@ where g.category = 'T-Shirt';
### D
```mysql
```sql
select g.id, g.name, s.name as size
from goods as g
right join size as s
......@@ -80,7 +80,7 @@ where g.category = 'T-Shirt';
### E
```mysql
```sql
select g.id, g.name, s.name as size
from goods as g
outter join size as s
......
......@@ -2,7 +2,7 @@
现有员工信息表如下:
```mysql
```sql
create table employee
(
id serial primary key,
......@@ -23,7 +23,7 @@ create table employee
## 答案
```mysql
```sql
select l.id, l.name, l.dept, l.salary
from employee as l
join (select max(salary) as salary, dept
......@@ -36,7 +36,7 @@ from employee as l
### select 与 group by 不匹配
```mysql
```sql
select id, name, dept, max(salary)
from employee
group by dept;
......@@ -44,7 +44,7 @@ group by dept;
### group by 不对
```mysql
```sql
select id, name, dept, max(salary)
from employee
group by dept, id, name;
......@@ -52,7 +52,7 @@ group by dept, id, name;
### group by 不对
```mysql
```sql
select id, name, dept, max(salary)
from employee
group by dept, id, name
......@@ -61,7 +61,7 @@ having salary = max(salary);
### 结构错误
```mysql
```sql
select id, name, dept, max(salary)
from employee
where salary = max(salary)
......
......@@ -2,7 +2,7 @@
Joe 想给 Goods 表
```mysql
```sql
create table goods(
id int primary key auto_increment,
category_id int,
......@@ -26,7 +26,7 @@ create table goods(
## 答案
```mysql
```sql
create index idx_goods_category on goods(category_id);
```
......@@ -34,18 +34,18 @@ create index idx_goods_category on goods(category_id);
### A
```mysql
```sql
create unique index idx_goods_category on goods(category_id);
```
### B
```mysql
```sql
create index idx_goods_category on goods(category_id);
```
### C
```mysql
```sql
alter table goods add unique index (category_id);
```
\ No newline at end of file
......@@ -13,13 +13,13 @@ Joe 想要删除创建在 goods 表创建的索引,但是他已经忘了这个
执行
```mysql
```sql
show index from goods;
```
查看 goods 表的索引(假设查到是 idx_goods_category),然后执行
```mysql
```sql
alter table goods drop index idx_goods_category;
```
......@@ -29,13 +29,13 @@ alter table goods drop index idx_goods_category;
执行
```mysql
```sql
show index from goods;
```
查看 goods 表的索引(假设查到是 idx_goods_category),然后执行
```mysql
```sql
alter table goods delete index idx_goods_category;
```
......@@ -43,13 +43,13 @@ alter table goods delete index idx_goods_category;
执行
```mysql
```sql
show index from goods;
```
查看 goods 表的索引(假设查到是 idx_goods_category),然后执行
```mysql
```sql
alter table goods remove index idx_goods_category;
```
......@@ -57,13 +57,13 @@ alter table goods remove index idx_goods_category;
执行
```mysql
```sql
show index from goods;
```
查看 goods 表的索引(假设查到是 idx_goods_category),然后执行
```mysql
```sql
alter table goods drop idx_goods_category;
```
......
......@@ -2,7 +2,7 @@
Goods 表结构如下:
```mysql
```sql
create table goods(
id int primary key auto_increment,
category_id int,
......@@ -24,7 +24,7 @@ Joe 需要确保同一个类型下没有重名的商品,他应该怎么做?
## 答案
```mysql
```sql
alter table goods add unique index (category_id, name);
```
......@@ -32,18 +32,18 @@ alter table goods add unique index (category_id, name);
### A
```mysql
```sql
alter table goods add index (category_id, name);
```
### B
```mysql
```sql
alter table goods add unique index (category_id + name);
```
### C
```mysql
```sql
alter table goods add unique index (concat(category_id, name));
```
......@@ -2,7 +2,7 @@
Goods 表结构如下:
```mysql
```sql
create table goods(
id int primary key auto_increment,
category_id int,
......@@ -24,7 +24,7 @@ create table goods(
## 答案
```mysql
```sql
alter table goods add index (name, price);
```
......@@ -38,12 +38,12 @@ alter table goods add index (name, price);
建立一个计算字段:
```mysql
```sql
alter table goods add summary varchar(1024) generated always as (concat(name, '(', 0.5, ')'));
```
### C
```mysql
```sql
alter table goods add index (concat(name, price));
```
\ No newline at end of file
......@@ -2,7 +2,7 @@
Goods 表结构如下:
```mysql
```sql
create table goods(
id int primary key auto_increment,
category_id int,
......@@ -28,7 +28,7 @@ Joe 发现有大量查询 `select id, category_id, name, price from goods where
将该查询改写为
```mysql
```sql
select id, category_id, name, price from goods where category_id=? and name=?;
```
......@@ -38,7 +38,7 @@ select id, category_id, name, price from goods where category_id=? and name=?;
将该查询改写为
```mysql
```sql
select id, category_id, name, price from goods where category_id and name= (?, ?);
```
......@@ -46,7 +46,7 @@ select id, category_id, name, price from goods where category_id and name= (?, ?
将该查询改写为
```mysql
```sql
select id, category_id, name, price from goods where not (category_id !=? or name != ?);
```
......@@ -54,7 +54,7 @@ select id, category_id, name, price from goods where not (category_id !=? or nam
将该查询改写为
```mysql
```sql
select id, category_id, name, price from goods where not (category_id !=?) and not (name != ?);
```
......@@ -2,7 +2,7 @@
Goods 表结构如下:
```mysql
```sql
create table goods(
id int primary key auto_increment,
category_id int,
......@@ -26,7 +26,7 @@ Joe 应该如何优化?
## 答案
```mysql
```sql
alter table goods add index (`stock-id`, category_id, name);
```
......@@ -34,18 +34,18 @@ alter table goods add index (`stock-id`, category_id, name);
### A
```mysql
```sql
alter table goods add index (`stock-id` and category_id and name);
```
### B
```mysql
```sql
alter table goods add index (concat(stock, category_id, name));
```
### C
```mysql
```sql
alter table goods add index (`stock-id` + category_id + name);
```
......@@ -2,7 +2,7 @@
Goods 数据库中有一个 shop 表,其中包含如下字段:
```mysql
```sql
create table shop (
id int primary key auto_increment,
location GEOMETRY
......@@ -21,7 +21,7 @@ create table shop (
## 答案
```mysql
```sql
alter table shop modify location GEOMETRY not null;
alter table shop add INDEX geo_index(location);
```
......@@ -30,26 +30,26 @@ alter table shop add INDEX geo_index(location);
### A
```mysql
```sql
alter table shop add INDEX geo_index(location);
```
### B
```mysql
```sql
alter table shop add INDEX location;
```
### C
```mysql
```sql
alter table shop modify location GEOMETRY not null;
alter table shop add INDEX location;
```
### D
```mysql
```sql
alter table shop modify location GEOMETRY not null;
alter table shop add INDEX location;
```
......@@ -2,7 +2,7 @@
Shop 表的部分字段如下:
```mysql
```sql
create table shop (
id int primary key auto_increment,
description varchar(8000)
......@@ -21,7 +21,7 @@ create table shop (
## 答案
```mysql
```sql
alter table shop add fulltext(description);
```
......@@ -29,18 +29,18 @@ alter table shop add fulltext(description);
### A
```mysql
```sql
alter table shop add index fulltext(description);
```
### B
```mysql
```sql
alter table shop create fulltext(description);
```
### C
```mysql
```sql
alter table shop alter description add fulltext(description);
```
\ No newline at end of file
......@@ -2,7 +2,7 @@
Shop 表的部分字段如下:
```mysql
```sql
create table shop (
id int primary key auto_increment,
description varchar(8000),
......@@ -27,13 +27,13 @@ Joe 应该怎么做?
先执行
```mysql
```sql
alter table shop alter index description invisible ;
```
将索引隐藏,观察确认没有影响后再执行
```mysql
```sql
alter table shop drop index description;
```
......@@ -46,7 +46,7 @@ alter table shop drop index description;
先备份 shop 表,然后执行
```mysql
```sql
alter table shop drop index description;
```
删除,有问题的话从备份文件恢复。
......@@ -59,13 +59,13 @@ alter table shop drop index description;
先执行
```mysql
```sql
alter table shop alter index description invisible ;
```
将索引隐藏,确认后再执行
```mysql
```sql
alter table shop alter index description visible ;
```
......
......@@ -2,7 +2,7 @@
数据分析组需要经常执行以下查询生成从昨天到之前某一天的交易量统计
```mysql
```sql
select date(payment_date) as day, sum(amount)
from payment
where date(payment_date) between $1 and DATE_SUB(CURDATE(), INTERVAL 1 DAY)
......@@ -39,7 +39,7 @@ mysql> desc payment;
建立表达式索引
```mysql
```sql
alter table payment add index idx_payment_date((date(payment_date)));
```
......@@ -49,13 +49,13 @@ alter table payment add index idx_payment_date((date(payment_date)));
建立视图
```mysql
```sql
create view view_daily_payment as select date(payment_date) as day, amount from payment;
```
然后在视图 view_daily_payment 上执行
```mysql
```sql
select day, sum(amount)
from view_daily_payment
where day between $1 and DATE_SUB(CURDATE(), INTERVAL 1 DAY)
......@@ -66,7 +66,7 @@ group by day
在 payment_date 列上建立索引
```mysql
```sql
create index idx_payment_date on payment(payment_date);
```
......@@ -74,13 +74,13 @@ create index idx_payment_date on payment(payment_date);
建立计算列
```mysql
```sql
alter table payment add day date generated always as (date(payment_date)) stored;
```
然后使用它改写查询
```mysql
```sql
select day as day, sum(amount)
from payment
where day between $1 and date('yesterday')
......
......@@ -3,7 +3,7 @@
SmartMarket 交易所的系统中,所有订单在生成时,都从一个 oracle Sequence 中获取唯一的
序列号,完成交易计算后各个撮合程序将其插入如下的 orders 表中:
```mysql
```sql
create table orders
(
id integer primary key,
......@@ -27,7 +27,7 @@ create table orders
## 答案
```mysql
```sql
with recursive r(id) as (select id
from orders
where id = $1
......@@ -48,7 +48,7 @@ from orders
### B
```mysql
```sql
select data.id, content
from orders
join orders as r on orders.id = r.id - 1
......@@ -57,7 +57,7 @@ where id = $1;
### C
```mysql
```sql
select id, content
from orders
where id in (select id from orders where id = id + 1);
......@@ -65,7 +65,7 @@ where id in (select id from orders where id = id + 1);
### D
```mysql
```sql
with r as (select id
from orders
where id = $1
......
......@@ -2,7 +2,7 @@
现有一个表 node
```mysql
```sql
create table node
(
id int primary key auto_increment,
......@@ -24,7 +24,7 @@ create table node
## 答案
```mysql
```sql
with recursive t(id, pid, val) as (
select id, pid, val
from node
......@@ -42,7 +42,7 @@ from node
### 没有递归定义
```mysql
```sql
with t as (
select id, pid, val
from node
......@@ -58,7 +58,7 @@ from node
### 平凡的连接查询无法处理递归问题
```mysql
```sql
select node.id, node.pid, node.val
from node
join node as p on node.pid = p.id
......@@ -67,7 +67,7 @@ where id = $1;
### 子查询无法处理递归问题
```mysql
```sql
select node.id, node.pid, node val
from node as t
where t.pid = (select id from t where id = t.pid)
......
......@@ -2,7 +2,7 @@
现有员工信息表如下:
```mysql
```sql
create table employee
(
id serial primary key,
......@@ -23,7 +23,7 @@ create table employee
## 答案
```mysql
```sql
select id, name, dept, salary
from (select id, name, dept, salary, rank() over (partition by dept order by salary desc) as r
from employee) as t
......@@ -34,7 +34,7 @@ where r <= 5;
### 结构错误
```mysql
```sql
select id, name, dept, max(salary) as salary
from employee
group by dept
......@@ -43,7 +43,7 @@ having id < 6;
### 结构错误
```mysql
```sql
select l.id, l.name, l.dept, l.salary
from employee as l
join (select max(salary) as salary, dept
......@@ -55,7 +55,7 @@ where count(r.id) <= 5;
### 结构错误
```mysql
```sql
select l.id, l.name, l.dept, l.salary
from employee as l
join (select max(salary, 5) as salary, dept
......@@ -66,7 +66,7 @@ from employee as l
### 结构错误
```mysql
```sql
select id, name, dept, salary, rank() over (partition by dept order by salary desc) as r
from employee
where r <= 5;
......@@ -74,7 +74,7 @@ where r <= 5;
### 结构错误
```mysql
```sql
select id, name, dept, salary, rank() as r over (partition by dept order by salary desc)
from employee
where r <= 5;
......
......@@ -2,7 +2,7 @@
现有销售记录表
```mysql
```sql
create table sales(
id serial primary key ,
sku_id integer not null ,
......@@ -23,7 +23,7 @@ create index on sales(created_at);
## 答案
```mysql
```sql
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,
......@@ -46,7 +46,7 @@ group by sku_id;
### 格式不相符
```mysql
```sql
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
......@@ -55,7 +55,7 @@ group by 1, 2;
### 计算逻辑错误
```mysql
```sql
select sku_id,
sum(amount) as Jan,
sum(amount) as Feb,
......@@ -76,7 +76,7 @@ group by sku_id, extract(month from created_at);
### 计算格式错误
```mysql
```sql
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,
......
......@@ -2,7 +2,7 @@
风控部门的数据库中有一组关于招投标的数据,关键信息如下:
```mysql
```sql
-- 招标项目
create table invitation
(
......@@ -42,7 +42,7 @@ Joe 想要找出参与了所有投标的企业(有围标嫌疑),那么这
## 答案
```mysql
```sql
select *
from company
where not exists(
......@@ -61,7 +61,7 @@ where not exists(
### A
```mysql
```sql
select *
from company
where id = all (select company_id
......@@ -70,7 +70,7 @@ where id = all (select company_id
### B
```mysql
```sql
select *
from company
where id = any (select company_id
......@@ -79,7 +79,7 @@ where id = any (select company_id
### C
```mysql
```sql
select *
from company
where company.id in (select distinct company_id from bids)
......@@ -87,7 +87,7 @@ where company.id in (select distinct company_id from bids)
### D
```mysql
```sql
select *
from company
where exists(
......
......@@ -2,7 +2,7 @@
现有一个表
```mysql
```sql
create table book_in(
login integer primary key ,
score integer default 0
......@@ -21,7 +21,7 @@ create table book_in(
## 答案
```mysql
```sql
insert into book_in(login, score) values ($1, $2) on DUPLICATE KEY update score=$2;
```
......@@ -29,7 +29,7 @@ insert into book_in(login, score) values ($1, $2) on DUPLICATE KEY update score=
### A
```mysql
```sql
try
insert into book_in(login, score) values ($1, $2);
catch
......@@ -40,18 +40,18 @@ finally
### B
```mysql
```sql
insert into book_in(login, score) select $1, $2;
```
### C
```mysql
```sql
replace book_in(login, score) select $1, $2;
```
### D
```mysql
```sql
upsert into book_in(login, score) select $1, $2;
```
......@@ -2,13 +2,13 @@
现有 test1 表如下
```mysql
```sql
create table test1(a integer primary key );
```
我们执行下面的语句
```mysql
```sql
CREATE PROCEDURE transaction_test1()
BEGIN
declare idx int;
......
......@@ -17,7 +17,7 @@ Goods 数据库中有一个名为 trade 的存储过程,封装了交易过程
## 答案
```mysql
```sql
set @counter = @counter + 1;
if @counter % 1000 = 0 then
set @total_price = @total_price * 0.8;
......@@ -28,7 +28,7 @@ end if;
### A
```mysql
```sql
set @counter = @counter + 1;
if @counter % 1000 = 0 {
set @total_price = @total_price * 0.8;
......@@ -37,7 +37,7 @@ if @counter % 1000 = 0 {
### B
```mysql
```sql
set @counter = @counter + 1;
if (@counter % 1000 = 0) {
set @total_price = @total_price * 0.8;
......@@ -46,7 +46,7 @@ if (@counter % 1000 = 0) {
### C
```mysql
```sql
if @counter % 1000 = 0 begin ;
set @total_price = @total_price * 0.8;
end;
......@@ -54,7 +54,7 @@ end;
### D
```mysql
```sql
set @counter ++;
if @counter % 1000 = 0 then begin
select @total_price = @total_price * 0.8;
......
......@@ -13,7 +13,7 @@
## 答案
```mysql
```sql
-- ...
trade: LOOP
if @price > 1000 then
......@@ -33,7 +33,7 @@ end LOOP trace;
### A
```mysql
```sql
-- ...
trade: LOOP
if @price > 1000 then
......@@ -49,7 +49,7 @@ end LOOP trace;
### B
```mysql
```sql
-- ...
trade: LOOP
if @price > 1000 then
......@@ -67,7 +67,7 @@ end LOOP trace;
### C
```mysql
```sql
-- ...
trade: LOOP
if @price > 1000 then
......@@ -85,7 +85,7 @@ end LOOP trace;
### D
```mysql
```sql
-- ...
trade: LOOP
if @price > 1000 then
......
......@@ -2,7 +2,7 @@
交易过程 trade 中有一个遍历交易项的循环,当累计的总交易额 `@total_price` 超过 20000, 这个循环就结束
```mysql
```sql
-- ...
trade: LOOP
-- ...
......@@ -26,7 +26,7 @@ end LOOP trace;
## 答案
```mysql
```sql
trade: REPEATE
-- 省略交易过程
UNTIL @total_price > 20000
......@@ -37,7 +37,7 @@ END REPATE trade;
### A
```mysql
```sql
trade: REPEATE
UNTIL @total_price > 20000
-- 省略交易过程
......@@ -46,7 +46,7 @@ END REPATE trade;
### B
```mysql
```sql
REPEATE @total_price > 20000
-- 省略交易过程
END REPATE;
......@@ -54,7 +54,7 @@ END REPATE;
### C
```mysql
```sql
trade: REPEATE
-- 省略交易过程
IF @total_price > 20000
......@@ -63,7 +63,7 @@ END REPATE trade;
### D
```mysql
```sql
trade: REPEATE
IF @total_price > 20000
-- 省略交易过程
......
......@@ -2,7 +2,7 @@
交易过程 trade 中有一个遍历交易项的循环,当累计的总交易额 `@total_price` 超过 20000, 这个循环就结束
```mysql
```sql
-- ...
trade: LOOP
-- ...
......@@ -26,7 +26,7 @@ end LOOP trace;
## 答案
```mysql
```sql
trade: WHILE @total_price > 20000
-- 省略交易过程
END WHILE trade;
......@@ -36,7 +36,7 @@ END WHILE trade;
### A
```mysql
```sql
trade: DO
-- 省略交易过程
WHILE @total_price > 20000;
......@@ -44,7 +44,7 @@ WHILE @total_price > 20000;
### B
```mysql
```sql
trade: WHILE @total_price > 20000
-- 省略交易过程
END WHILE trade;
......@@ -52,7 +52,7 @@ END WHILE trade;
### C
```mysql
```sql
trade: WHILE @total_price > 20000 DO
-- 省略交易过程
END WHILE trade;
......@@ -60,7 +60,7 @@ END WHILE trade;
### D
```mysql
```sql
trade: WHILE @total_price > 20000
-- 省略交易过程
END WHILE;
......
......@@ -2,7 +2,7 @@
Joe 需要开发一个存储过程 make_trade,从 orders 表
```mysql
```sql
create table orders (
id int primary key auto_increment,
price decimal(12, 4),
......@@ -13,7 +13,7 @@ create table orders (
中,按 id 从小到大加载订单数据,生成交易单,写入 trade 表
```mysql
```sql
create table trade (
id int primary key auto_increment,
total decimal(12, 4)
......@@ -36,7 +36,7 @@ create table trade (
## 答案
```mysql
```sql
create procedure make_trade()
begin
DECLARE done INT DEFAULT FALSE;
......@@ -70,7 +70,7 @@ end;
### A
```mysql
```sql
create procedure make_trade()
begin
DECLARE done INT DEFAULT FALSE;
......@@ -97,7 +97,7 @@ end;
### B
```mysql
```sql
create procedure make_trade()
begin
declare cur_orders cursor for select id, price, item_id, amount from orders order by id limit 1000;
......@@ -118,7 +118,7 @@ end;
### C
```mysql
```sql
create procedure make_trade()
begin
DECLARE done INT DEFAULT FALSE;
......@@ -139,7 +139,7 @@ end;
### D
```mysql
```sql
create procedure make_trade()
begin
DECLARE done INT DEFAULT FALSE;
......
......@@ -2,7 +2,7 @@
Joe 需要为 Points 表
```mysql
```sql
create table points(
id int primary key auto_increment,
x float,
......@@ -21,7 +21,7 @@ create table points(
## 答案
```mysql
```sql
alter table points add modulus double generated always as (sqrt(x*x + y*y));
```
......@@ -29,18 +29,18 @@ alter table points add modulus double generated always as (sqrt(x*x + y*y));
### A
```mysql
```sql
create generated modulus on table points as (sqrt(x*x + y*y));
```
### B
```mysql
```sql
alter table points add modulus generated always as (sqrt(x*x + y*y));
```
### C
```mysql
```sql
alter table points add modulus float generated sqrt(x*x + y*y);
```
......@@ -2,7 +2,7 @@
Goods 中有客户/订单系统如下:
```mysql
```sql
create table customers
(
id serial primary key,
......
......@@ -3,7 +3,7 @@
# 每日报表
分析过去一段时间的查询数据,Joe 发现 payment 表
```mysql
```sql
create table payment(
payment_id int primary key auto_increment,
customer_id int,
......@@ -16,7 +16,7 @@ create table payment(
每天的订单量很大,下面这个查询的统计过程占用了大多数数据库资源,查询不会
早于当天,总是在历史上某一个日期段内查询
```mysql
```sql
select date(payment_date) as day, sum(amount)
from payment
where date(payment_date) between $1 and $2
......@@ -47,13 +47,13 @@ select payment_date::date as day, sum(amount) as amount from payment group by da
使用
```mysql
```sql
select day, amount from view_daily_payment where day between $1 and $2;
```
进行查询。并且每天定时执行一次刷新命令
```mysql
```sql
insert into daily_payment(day, amount)
select date(payment_date) as day, sum(amount) as amount
from payment
......@@ -68,7 +68,7 @@ group by day;
在 payment_date 列上建立索引
```mysql
```sql
create index idx_payment_date on payment(payment_date);
```
......@@ -76,13 +76,13 @@ create index idx_payment_date on payment(payment_date);
建立计算列
```mysql
```sql
alter table payment add day date generated always as ( payment_date::date ) stored
```
然后使用它改写查询
```mysql
```sql
select day as day, sum(amount)
from payment
where day between $1 and DATE_SUB(CURDATE(), INTERVAL 1 DAY)
......@@ -93,7 +93,7 @@ group by day;
建立表达式索引
```mysql
```sql
create index idx_payment_day on payment((date(payment_date)));
```
......@@ -101,13 +101,13 @@ create index idx_payment_day on payment((date(payment_date)));
建立视图
```mysql
```sql
create view view_daily_payment as select date(payment_date) as day, amount from payment;
```
然后在视图 view_daily_payment 上执行
```mysql
```sql
select day, sum(amount)
from view_daily_payment
where day between $1 and date('yesterday')
......
......@@ -19,7 +19,7 @@
可以在主库中执行以下命令设定只复制 goods 数据库和 auth 数据库
```mysql
```sql
SET GLOBAL binlog-do-db=goods,auth;
```
......@@ -35,7 +35,7 @@ binlog-do-db=goods,auth
可以在从库执行以下命令,设定只复制 goods 数据库和 auth 数据库
```mysql
```sql
SET GLOBAL replicate-do-db=goods,auth;
```
......@@ -43,7 +43,7 @@ SET GLOBAL replicate-do-db=goods,auth;
可以在从库执行以下命令,设定只复制 goods 数据库和 auth 数据库
```mysql
```sql
CHANGE REPLICATION FILTER REPLICATE-DO-DB=(goods, order);
```
......@@ -59,7 +59,7 @@ replicate-do-db=goods,order;
可以在主库中执行以下命令设定忽略 goods 数据库和 auth 数据库
```mysql
```sql
SET GLOBAL binlog-ignore-db=goods,auth;
```
......@@ -75,7 +75,7 @@ binlog-ignore-db=goods,auth
可以在从库执行以下命令,设定忽略 goods 数据库和 auth 数据库
```mysql
```sql
SET GLOBAL replicate-ignore-db=goods,auth;
```
......@@ -83,7 +83,7 @@ SET GLOBAL replicate-ignore-db=goods,auth;
可以在从库执行以下命令,设定忽略 goods 数据库和 auth 数据库
```mysql
```sql
CHANGE REPLICATION FILTER REPLICATE_IGNORE_DB=(goods, auth);
```
......@@ -99,7 +99,7 @@ replicate-ignore-db=goods,order;
可以在在从库的 MySQL 命令行设定只同步 goods 表和 orders 表
```mysql
```sql
SET GLOBAL replicate-do-table=goods.goods,goods.orders;
```
......@@ -107,7 +107,7 @@ SET GLOBAL replicate-do-table=goods.goods,goods.orders;
可以在在从库的 MySQL 命令行设定只同步 goods 表和 orders 表
```mysql
```sql
CHANGE REPLICATION FILTER REPLICATE_DO_TABLE=( goods.goods,goods.orders);
```
......@@ -123,7 +123,7 @@ replicate-do-table=goods.goods,goods.orders
可以在在从库的 MySQL 命令行设定只同步名称前缀为 `t_` 的表
```mysql
```sql
CHANGE REPLICATION FILTER REPLICATE_WILD_DO_TABLE =( goods.t_%);
```
......@@ -131,7 +131,7 @@ CHANGE REPLICATION FILTER REPLICATE_WILD_DO_TABLE =( goods.t_%);
可以在从库的 MySQL 命令行设定忽略 orders 和 trade 表
```mysql
```sql
SET GLOBAL replicate-ignore-table=goods.orders,goods.trade;
```
......@@ -139,7 +139,7 @@ SET GLOBAL replicate-ignore-table=goods.orders,goods.trade;
可以在从库的 MySQL 命令行设定忽略 orders 和 trade 表
```mysql
```sql
CHANGE REPLICATION FILTER REPLICATE_ IGNORE_TABLE=( goods.orders,goods.trade);
```
......@@ -156,7 +156,7 @@ replicate-ignore-table =goods.orders,goods.trade
可以在在从库的 MySQL 命令行设定忽略名称前缀为 `o_` 的表
```mysql
```sql
CHANGE REPLICATION FILTER REPLICATE_WILD_DO_TABLE =( goods.o_%);
```
......@@ -164,6 +164,6 @@ CHANGE REPLICATION FILTER REPLICATE_WILD_DO_TABLE =( goods.o_%);
设定过滤器时,可以指定通道,例如
```mysql
```sql
CHANGE REPLICATION FILTER REPLICATE-DO-DB=(goods, auth) FOR CHANNEL channel-name;
```
\ No newline at end of file
......@@ -11,7 +11,7 @@ Goods 数据库近期在每日高峰时段很慢,Joe 想初步的查看一下
## 答案
```mysql
```sql
show global status;
```
......@@ -19,19 +19,19 @@ show global status;
### A
```mysql
```sql
show status;
```
### B
```mysql
```sql
show session status;
```
### C
```mysql
```sql
show local status;
```
......@@ -2,7 +2,7 @@
Joe 从交易服务中发现了一些高频查询和慢查询,例如
```mysql
```sql
with recursive r(id) as (select id
from orders
where id = $1
......@@ -28,7 +28,7 @@ from orders
在测试库使用
```mysql
```sql
explain with recursive r(id) as (select id
from orders
where id = $1
......@@ -43,7 +43,7 @@ from orders
进行剖分,对于需要了解详细执行计划的使用
```mysql
```sql
explain analyze with recursive r(id) as (select id
from orders
where id = 100
......@@ -72,7 +72,7 @@ from orders
使用
```mysql
```sql
analyze with recursive r(id) as (select id
from orders
where id = 100
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册