今天在整理项目中数据库时,发现自己对于数据库字段长度的问题一直不清楚,这里做个总结。
下表列出每种数据类型的默认长度和最大长度
数据类型 | 默认长度 | 最大长度 |
varchar | 255 | 65535 |
char | 255 | 65535 |
double | 0 | 255 |
float | 0 | 255 |
int | 11 | 255 |
tinyint | 4 | 255 |
bigint | 20 | 255 |
date | 0 | 0 |
datetime | 0 | 0 |
text | 0 | 0 |
表1
什么是长度,例如int的默认长度是11就意味着这个字段可以最大存 9999999999 么,不是的!所谓长度只是显示的宽度。
执行建表sql:
CREATE TABLE `table_name` (
`id` int(1) NOT NULL,
`order_no` int(5) unsigned zerofill DEFAULT NULL,
`hh` int(5) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入三条数据:
INSERT INTO table_name VALUES ('1', '00012', '122222');
INSERT INTO table_name VALUES ('2', '00005', '588');
INSERT INTO table_name VALUES ('4', '00005', '122');
注意‘order_no’是zerofill,这意味当次字段长度小于5时将会用‘0’补齐;‘hh’字段由于没有设置zerofill,所以不会有0补齐
CHAR | 0-255字符 | 定长字符串 |
VARCHAR | 0-21845字符 | 变长字符串 |
int | 4字节 |
- 表2
所以对于int来说,存储空间是一定的(表2),这时的int表示范围(-2 147 483 648,2 147 483 647),但是在我们平时建表时,int多用在id主键上,主键没有负数,所以通常采用无符号(unsigned)int,例如上面的建表语句 `order_no` int(5) unsigned zerofill DEFAULT NULL,这时int的范围在(0,4 294 967 295)。
对于varchar来说,有点区别
4.0版本以下,varchar(50),指的是50字节,如果存放UTF8汉字时,只能存16个(每个中文3字节)
5.0版本以上,varchar(50),指的是50字符,无论存放的是数字、字母还是UTF8中文(每个中文3字节),都可以存放50个
经过测试varchar最大可以为21845个字符
以前建表时没有关注过字段的命名,我参考《阿里巴巴java开发手册1.2.0》,学习到:
表名、字段名必须使用小写字母或数字,禁止出现数字开头,禁止两个下划线中间只出现数字。数据库字段名的修改代价很大,因为无法进行预发布,所以字段名称需要慎重考虑。
在以前的项目中,有些表以code作为主键,并且用code做主外键关联。这种方式是不好的,建议使用id做主键,这样的效率比较高
CHAR | 0-255字符 | 定长字符串 |
VARCHAR | 0-21845字符 | 变长字符串 |
int | 4字节 |
- 表2
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xlink:href="#csdnc-thumbsup"></use> </svg><span class="name">点赞</span> <span class="count"></span> </a></li> <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-Collection-G"></use> </svg><span class="name">收藏</span></a></li> <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-fenxiang"></use> </svg>分享</a></li> <!--打赏开始--> <!--打赏结束--> <li class="tool-item tool-more"> <a> <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg> </a> <ul class="more-box"> <li class="item"><a class="article-report">文章举报</a></li> </ul> </li> </ul> </div> </div> <div class="person-messagebox"> <div class="left-message"><a href="https://blog.csdn.net/hxhdj"> <img src="https://profile.csdnimg.cn/9/A/D/3_hxhdj" class="avatar_pic" username="hxhdj"> <img src="https://g.csdnimg.cn/static/user-reg-year/2x/5.png" class="user-years"> </a></div> <div class="middle-message"> <div class="title"><span class="tit"><a href="https://blog.csdn.net/hxhdj" data-report-click="{"mod":"popu_379"}" target="_blank">hxhdj</a></span> </div> <div class="text"><span>发布了11 篇原创文章</span> · <span>获赞 1</span> · <span>访问量 4728</span></div> </div> <div class="right-message"> <a href="https://im.csdn.net/im/main.html?userName=hxhdj" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信 </a> <a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a> </div> </div> </div>