Pages

Tuesday, 24 March 2015

What is the difference between int(1) and int(10)? Can the field with int(1) store 1000 in it?


Of course yes. Int(1) can store 1000 in it. Here's why:

    An int is always 4 bytes. That can store signed numbers from -2 billion to +2 billion (and unsigned numbers 0 to 4B). So, what does it mean if you declare an int(10)? It does not restrict the number of digits to 10. The (10) is the display width. It's only used if you use UNSIGNED and ZEROFILL with an integer type. Then the display of those numbers with a zero-padding on the left to 10 digits if they contain less than 10 digits. The field will have a default value of 0 for int(1) and 0000000000 for int(10). Example:


CREATE TABLE `employee` (
`id` int(10) unsigned zerofill DEFAULT NULL
)

 SELECT * FROM employee;
+----------------+

| id                |

+----------------+

| 0000000042 |

| 0000000101 |

| 0009876543 |

+----------------+

No comments:

Post a Comment