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

fix bug

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