Every good habit is a fortune. This article is divided into three directions: SQL regret medicine, SQL performance optimization, and SQL specification elegance. Share and write 21 good habits of SQL, thank you for reading, keep up the good work~
When developing and writing SQL in daily life, try to develop it as much as possible This is a good habit: after writing the SQL, use explain to analyze it, paying special attention to whether the index is used.
explain select * from user where userid =10086 or age =18;复制代码
When executing the delete or update statement, try to add a limit, as follows Take the following SQL as an example:
delete from euser where age > 30 limit 200;复制代码
Because adding limit has the following main benefits:
This good habit must be developed when designing database tables , all tables and fields are added with corresponding comments, making it easier to maintain later.
Positive example:
CREATE TABLE `account` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键Id', `name` varchar(255) DEFAULT NULL COMMENT '账户名', `balance` int(11) DEFAULT NULL COMMENT '余额', `create_time` datetime NOT NULL COMMENT '创建时间', `update_time` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (`id`), KEY `idx_name` (`name`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=1570068 DEFAULT CHARSET=utf8 ROW_FORMAT=REDUNDANT COMMENT='账户表';复制代码
Counter example:
CREATE TABLE `account` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `balance` int(11) DEFAULT NULL, `create_time` datetime NOT NULL , `update_time` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `idx_name` (`name`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=1570068 DEFAULT CHARSET=utf8;复制代码
Positive example:
SELECT stu.name, sum(stu.score) FROM Student stu WHERE stu.classNo = '1班' GROUP BY stu.name复制代码
Counterexample:
SELECT stu.name, sum(stu.score) from Student stu WHERE stu.classNo = '1班' group by stu.name.复制代码
Obviously, unifying keywords in the same case and using indentation alignment will make your SQL look better. It looks more elegant~
Counter example:
insert into Student values ('666','捡田螺的小男孩','100');复制代码
Positive example:
insert into Student(student_id,name,score) values ('666','捡田螺的小男孩','100');复制代码
Counter example:
CREATE TABLE `account` ( `name` varchar(255) DEFAULT NULL COMMENT '账户名', `balance` int(11) DEFAULT NULL COMMENT '余额', ) ENGINE=InnoDB AUTO_INCREMENT=1570068 DEFAULT CHARSET=utf8 ROW_FORMAT=REDUNDANT COMMENT='账户表';复制代码
Positive example:
CREATE TABLE `account` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键Id', `name` varchar(255) DEFAULT NULL COMMENT '账户名', `balance` int(11) DEFAULT NULL COMMENT '余额', `create_time` datetime NOT NULL COMMENT '创建时间', `update_time` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (`id`), KEY `idx_name` (`name`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=1570068 DEFAULT CHARSET=utf8 ROW_FORMAT=REDUNDANT COMMENT='账户表';复制代码
Reason:
Alibaba Development Manual also mentions this point, as shown in Figure
Counter example:
select * from user where address ='深圳' order by age ;复制代码
Positive example:
添加索引 alter table user add index idx_address_age (address,age)复制代码
If you want to modify or delete data, you must back up the data to be modified before executing SQL, in case of misoperation , you can also take mouthfulregret medicine~
反例:
//userid 是varchar字符串类型 select * from user where userid =123;复制代码
正例:
select * from user where userid ='123';复制代码
理由:
尤其在操作生产的数据时,遇到修改或者删除的SQL,先加个where查询一下,确认OK之后,再执行update或者delete操作
反例:
select * from employee;复制代码
正例:
select id,name from employee;复制代码
理由:
Innodb 支持事务,支持行级锁,更好的恢复性,高并发下性能更好,所以呢,没有特殊要求(即Innodb无法满足的功能如:列存储,存储空间数据等)的情况下,所有表必须使用Innodb存储引擎
统一使用UTF8编码
如果是存储表情的,可以考虑 utf8mb4
反例:
`deptName` char(100) DEFAULT NULL COMMENT '部门名称'复制代码
正例:
`deptName` varchar(100) DEFAULT NULL COMMENT '部门名称'复制代码
理由:
这个点,是阿里开发手册中,Mysql的规约。你的字段,尤其是表示枚举状态时,如果含义被修改了,或者状态追加时,为了后面更好维护,需要即时更新字段的注释。
正例:
begin; update account set balance =1000000 where name ='捡田螺的小男孩'; commit;复制代码
反例:
update account set balance =1000000 where name ='捡田螺的小男孩';复制代码
说明: pk_ 即 primary key;uk _ 即 unique key;idx _ 即 index 的简称。
假设loginTime加了索引
反例:
select userId,loginTime from loginuser where Date_ADD(loginTime,Interval 7 DAY) >=now();复制代码
正例:
explain select userId,loginTime from loginuser where loginTime >= Date_ADD(NOW(),INTERVAL - 7 DAY);复制代码
理由:
反例:
delete from account limit 100000;复制代码
正例:
for each(200次) { delete from account limit 500; }复制代码
理由:
相关免费学习推荐:mysql视频教程
The above is the detailed content of Have 21 good habits for writing SQL in Mysql. For more information, please follow other related articles on the PHP Chinese website!