advisor.Rule{Item:"KWR.002", Severity:"L2", Summary:"不建议使用 MySQL 关键字做列名或表名", Content:"当使用关键字做为列名或表名时程序需要对列名和表名进行转义,如果疏忽被将导致请求无法执行。", Case:"CREATE TABLE tbl ( `select` int )", Position:0, Func:func(*advisor.Query4Audit) advisor.Rule {...}}
advisor.Rule{Item:"KWR.003", Severity:"L1", Summary:"不建议使用复数做列名或表名", Content:"表名应该仅仅表示表里面的实体内容,不应该表示实体数量,对应于 DO 类名也是单数形式,符合表达习惯。", Case:"CREATE TABLE tbl ( `books` int )", Position:0, Func:func(*advisor.Query4Audit) advisor.Rule {...}}
advisor.Rule{Item:"KWR.004", Severity:"L1", Summary:"不建议使用使用多字节编码字符(中文)命名", Content:"为库、表、列、别名命名时建议使用英文,数字,下划线等字符,不建议使用中文或其他多字节编码字符。", Case:"select col as 列 from tb", Position:0, Func:func(*advisor.Query4Audit) advisor.Rule {...}}
advisor.Rule{Item:"KWR.005", Severity:"L1", Summary:"SQL 中包含 unicode 特殊字符", Content:"部分 IDE 会自动在 SQL 插入肉眼不可见的 unicode 字符。如:non-break space, zero-width space 等。Linux 下可使用 `cat -A file.sql` 命令查看不可见字符。", Case:"update\u00a0tb set\u00a0status\u00a0=\u00a01 where\u00a0id\u00a0=\u00a01;", Position:0, Func:func(*advisor.Query4Audit) advisor.Rule {...}}
advisor.Rule{Item:"LCK.001", Severity:"L3", Summary:"INSERT INTO xx SELECT 加锁粒度较大请谨慎", Content:"INSERT INTO xx SELECT 加锁粒度较大请谨慎", Case:"INSERT INTO tbl SELECT * FROM tbl2;", Position:0, Func:func(*advisor.Query4Audit) advisor.Rule {...}}
advisor.Rule{Item:"LCK.002", Severity:"L3", Summary:"请慎用 INSERT ON DUPLICATE KEY UPDATE", Content:"当主键为自增键时使用 INSERT ON DUPLICATE KEY UPDATE 可能会导致主键出现大量不连续快速增长,导致主键快速溢出无法继续写入。极端情况下还有可能导致主从数据不一致。", Case:"INSERT INTO t1(a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;", Position:0, Func:func(*advisor.Query4Audit) advisor.Rule {...}}
@@ -9,7 +9,7 @@ select * from ta join tb using (id)
select * from ta, tb limit 1
[`sakila`.`ta` `sakila`.`tb`]
drop table tb
[`sakila`.tb`]
[`sakila`.`tb`]
drop table db.tb
[`db`.`tb`]
drop database db
...
...
@@ -17,9 +17,9 @@ drop database db
create database db
[`db`.`dual`]
create index idx_col on tbl (col)
[`sakila`.tbl`]
[`sakila`.`tbl`]
DROP INDEX idx_col on tbl
[`sakila`.tbl`]
[`sakila`.`tbl`]
SELECT * FROM film WHERE length = 86;
[`sakila`.`film`]
SELECT * FROM film WHERE length IS NULL;
...
...
@@ -177,11 +177,11 @@ SELECT last_update FROM film order by date(last_update);
SELECT description FROM film WHERE description IN('NEWS','asd') GROUP BY description;
[`sakila`.`film`]
alter table address add index idx_city_id(city_id);
[`sakila`.address`]
[`sakila`.`address`]
alter table inventory add index `idx_store_film` (`store_id`,`film_id`);
[`sakila`.inventory`]
[`sakila`.`inventory`]
alter table inventory add index `idx_store_film` (`store_id`,`film_id`),add index `idx_store_film` (`store_id`,`film_id`),add index `idx_store_film` (`store_id`,`film_id`);
[`sakila`.inventory`]
[`sakila`.`inventory`]
SELECT DATE_FORMAT(t.last_update, '%Y-%m-%d'), COUNT(DISTINCT (t.city)) FROM city t WHERE t.last_update > '2018-10-22 00:00:00' AND t.city LIKE '%Chrome%' AND t.city = 'eip' GROUP BY DATE_FORMAT(t.last_update, '%Y-%m-%d') ORDER BY DATE_FORMAT(t.last_update, '%Y-%m-%d');
[`sakila`.`city`]
create table hello.t (id int unsigned);
...
...
@@ -189,7 +189,7 @@ create table hello.t (id int unsigned);
select * from tb where data >= '';
[`sakila`.`tb`]
alter table tb alter column id drop default;
[`sakila`.tb`]
[`sakila`.`tb`]
select maxId, minId from (select max(film_id) maxId, min(film_id) minId from film where last_update > '2016-03-27 02:01:01') as d;
[`sakila`.`film`]
select maxId, minId from (select max(film_id) maxId, min(film_id) minId from film) as d;