为什么我不能将我的 INT 从无符号更改为有符号?可以签名 Zerofill 吗?

Why can't I change my INT from unsigned to signed? Is signed Zerofill possible?

我在一个名为 TEST

的 table 中有一个名为 NUMBER 的字段
NUMBER is int(8) Unsigned Zerofill Not Null

我现在需要此字段中的负数,但我仍然希望它们为 zerofilled 例如-00023419

我可以将其更改为已签名,但似乎无法进行 zerofilled。

如果我这样做:alter table test modify number int(8) signed zerofill not null - 它保持无符号 zerofill

如果我这样做:alter table test modify number int(8) zerofill not null - 它保持无符号 zerofill

如果我这样做:alter table test modify number int(8) signed not null - 我得到了签名但没有 zerofill

当我将它设置为有符号时,我输入了一个负数然后尝试更改为 zerofill 并且数字更改为 00000000 并且所有内容都再次设置为无符号。不可能有签名的 zerofilled 数字?

https://dev.mysql.com/doc/refman/5.7/en/numeric-type-attributes.html 说:

If you specify ZEROFILL for a numeric column, MySQL automatically adds the UNSIGNED attribute to the column.

在 2006 年被报告为错误,关闭为 "Not a Bug"。 https://bugs.mysql.com/bug.php?id=24356


回复您的评论:

So does this mean no one out there has negative zero filled data in a mysql database?

没错。 ZEROFILL 最常见的用例是确保邮政编码或银行帐号等内容使用固定数量的数字。这些情况无论如何都不支持负值,所以为什么要为 -00001234 这样丑陋的字符串烦恼呢?

can you convert it to zerofilled with PHP?

是的,您可以在 PHP 中用零填充格式化数字。

<?php

$n = -1234;
printf("Number is %08d\n", $n);

输出:

Number is -0001234

阅读 http://php.net/manual/en/function.sprintf.php 了解您可以使用 printf()/sprintf() 进行的更简洁的格式化操作。

好吧,如果您的列是外键或主键,MySQL 不允许进行此修改。您必须事先执行此操作。

ZEROFILL 表示未签名。

MySQL 参考手册 https://dev.mysql.com/doc/refman/5.7/en/numeric-type-attributes.html

If you specify ZEROFILL for a numeric column, MySQL automatically adds the UNSIGNED attribute to the column.

由于存在所有潜在问题,我避免使用非标准 MySQL ZEROFILL 属性。