mysql - 括号中的数字究竟是什么意思?

标签 mysql sql ddl

一直以为括号里的数字代表字段长度

但是,我知道情况并非总是如此。也许这是一个MySQL问题?有人告诉我,如果我将字段设置为 9 个字符长,我可以添加一个超过 9 个字符的值,但只会保存前 9 个字符。

例子:

CREATE TABLE `person` (
    id INT,
    age INT(2)
);

如果是这样,我不应该选择像 TINYINT 这样的东西来代替 INT 吗?

最佳答案

INT(2) 将生成一个带有 minimum display width of 2 的 INT :

MySQL supports an extension for optionally specifying the display width of integer data types in parentheses following the base keyword for the type. For example, INT(4) specifies an INT with a display width of four digits. This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces. (That is, this width is present in the metadata returned with result sets. Whether it is used or not is up to the application.)

The display width does not constrain the range of values that can be stored in the column. Nor does it prevent values wider than the column display width from being displayed correctly. For example, a column specified as SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range permitted by three digits are displayed in full using more than three digits.

不会影响可以存储在字段中的可能值的范围;它也不是用于存储它的字节数。除非使用 ZEROFILL(参见链接页面),否则这似乎只是对应用程序如何显示值的建议。

一个无符号的TINYINT (0...255) 可能也可以,除非cryopreservation在您的应用程序的生命周期中向前迈出了一大步。

关于mysql - 括号中的数字究竟是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4055564/

相关文章:

java - 替代 Java.sql.time

sql - 不选择具有唯一键的重复记录

java - JPA - 如何在 DDL 中将字符串列设置为 varchar(max)

php - PHP OOP 登录注册系统中的 fatal error

mysql - 使用一对多 SQL 保留系列顺序

mysql - sql选择问题(mysql)

mysql - 一个表的内部连接并防止重复?

sql - Find Last Record in Chain - 一个客户合并过程

sql-server - 无论如何要为 "SELECT"语句创建 SQL Server DDL 触发器吗?

hadoop - 具有复杂数据类型的 parquet 格式的 Hive DDL