当前位置:网站首页>MySQL开发注意事项(阿里巴巴开发手册)
MySQL开发注意事项(阿里巴巴开发手册)
2022-06-12 15:15:00 【Stars_min】
1. 建表规约
1.1 表示是否概念的字段约束
必须使用is_xxx的方式命名,数据类型是unsigned tinyint( 1 表示是,0 表示否 )
注意:任何字段如果是非负数,必须是unsigned
1.2 表名、字段名约束
对于表名、字段名必须使用小写字母或数字,禁止数字开头,禁止两个下划线之间只出现数字,而且表名不能使用复数名称
1.3 禁用保留字,比如:desc、range、match等等
1.4 索引名约束
| 索引名 | 名称约束 |
|---|---|
| 主键索引 | pk_字段名 |
| 唯一索引 | uk_字段名 |
| 普通索引 | idx_字段名 |
全文索引:InnoDB不支持,Myisam支持性能比较好,一般在char、varchar、text列上创建
1.5 小数类型约束
小数类型为decimal,禁止使用float和double
原因:在存储的时候,float和double都存在精度损失的问题
建议:存储数据的范围超过decimal的范围,建议将数据拆分为整数和小数分开进行存储
1.6 如果存储的字符串长度几乎相等,使用char定长字符串类型
2. 索引规约
2.1 业务上具有唯一特性的字段,即使是多个字段的组合,也必须建成唯一索引
说明:建立唯一索引影响insert的数度可以忽略不记,但是建立唯一索引查找速度就会增加
2.2 超过三个表禁止join,需要join的字段,数据类型必须一致;多表关联查询时,保证被关联的字段需要有索引
2.3 在varchar字段上建立索引,必须指定索引长度,没必要对全字段建立索引
2.4 页面搜索严禁左模糊或者全模糊,如果需要请走搜索引擎来解决。
3. SQL语句
3.1 不要使用count(列名)或者count(常量)来替代count(*)
count(*)会统计值为NULL的行,而count(列)不会统计列为null值的行
3.2 count(distinct col)计算该列除NULL之外的重复行
注意:count(distinct col1,col2)如果其中一列全为null,那么即使另一列有不同的值,也会返回0
3.3 当某一列的值全是NULL,count(col)返回结果为0但是sum(col)的返回结果为NULL
3.4 使用isnull()来判断是否为null值
说明:null与任何值的比较都为null
1.null<>null返回值为null,而不是false
2.null=null返回值为null,而不是true
3.null<>1的返回结果是null,而不是true
3.5 代码写分页查询逻辑时,若count为0应直接返回,避免执行后面的分页语句
3.6 不得使用外键与级联,一切外键必须在应用层解决
3.7 禁止使用存储过程,存储过程难以调试和扩展,更没有移植性
3.8 数据订正(删除或者修改时)要先select,避免误删,确认无误在进行订正
4. ORM映射
4.1 在表查询的时候,一律不准使用 * 作为查询字段列表,需要哪些字段必须明确写明
4.2 POJO类的布尔属性不准加is,而数据库字段必须加is-,要求在resultMap中进行字段和属性之间的映射
4.3 不要用resultClass当返回参数,即使所有类属性名和数据库字段一一对应,也需要定义
4.4 sql.xml配置参数使用#{}
4.5 不允许直接拿HashMap与HashTable作为查询结果集的输出
边栏推荐
- Error 1105: message:\“raft entry is too large
- 简单的爬虫框架:解析51job页面岗位信息
- 交换数字,异或求单,操作符相关
- 2021-06-27
- Scala download and idea installation of scala plug-ins (nanny level tutorial is super detailed)
- Distributed concurrent repeated submission
- Apprendre est une chose contre la nature humaine
- Acwing暑期每日一题(6月10日性感素数)
- ShardingSphere实践(6)——弹性伸缩
- [game server design cases] insights
猜你喜欢

粒子滤波学习记录

Jetpack architecture component learning (3) -- activity results API usage

Seaborn Brief

Simple crawler framework: parsing 51job page position information

Preparation of service for robot moving forward and rotating

Kinect2.0+ORBSLAM2_ with_ pointcloud_ map

Solve log4j2 vulnerability and be attacked by mining and zombie process viruses

Multi thread knowledge induction

C 字符串

Change according to the situation, the road to promotion in the second half of 2022
随机推荐
[jvm learning] parental delegation mechanism and PC register (program counter)
C operator
C main function
Error 1105: message:\“raft entry is too large
FIRST集与FOLLOW集白话版
tc菜单分割
[wp][beginner level] attack and defense world game
2021-06-20
[SPARK][CORE] 面试问题之什么是 external shuffle service?
Autofac Beginner (1)
How to add WWW to the domain name
Autofac (2)
[spark][core] what is an external shuffle service?
C语言打开中文路径文件
學習是一件逆人性的事情(成為高手的內功心法)
C 数据类型
宝塔面板新建数据库提示数据库名不能大于16位的解决方法
机器人前行、旋转的service编写
ROS beginners write the server that the little turtle rotates a certain angle at a certain speed
Jetpack architecture component learning (3) -- activity results API usage