---
title: "Mysql tinyint和Sqlserver tinyint的区别，zerofill的使用"
author: "Kevin12"
date: "2016-11-05T22:05:45Z"
tags: ["msyql", "sqlserver"]
categories: ["数据库"]
slug: "mysql-tinyint-sqlserver-tinyint-zerofill"
draft: false
lastmod: "2016-11-05T22:05:45Z"
id: 2335577
url: "https://kevin12.iteye.com/blog/2335577"
---

数据库表设计的时候一定要注意：
Mysql里面，tinyint默认是有符号的，取值范围是-128到127，unsigned属性就是将数字类型无符号化 unsigned tinyint才是0-255；
SQL Server支持bignt, int, smallint, tinyint这四种int，前三种都是有符号的，而tinyint却是无符号的，取值范围是0-255；
所以，如果在这两个数据库直接迁移表结构的话，就要注意了。 

----------------------------
zerofill的使用：
CREATE TABLE `test` (
  `u_id` INT(11) NOT NULL AUTO_INCREMENT,
  `u_name` VARCHAR(50) DEFAULT NULL,
  `tinyint1`TINYINT(1),
  `tinyint2`TINYINT(2) ZEROFILL,
  `tinyint3`TINYINT(3) ZEROFILL,
  `tinyint4`TINYINT(4)ZEROFILL,
  PRIMARY KEY (`u_id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
INSERT INTO test(u_name,tinyint1,tinyint2,tinyint3,tinyint4)VALUES('zerofill就是将默认补充的空格用0填充',6,6,6,6);
SELECT * FROM test;

![](http://dl2.iteye.com/upload/attachment/0121/0499/0d072b3e-e4dd-3e72-8dff-31444c9d1cd6.jpg)