{"id":3845,"date":"2012-07-06T14:36:31","date_gmt":"2012-07-06T09:06:31","guid":{"rendered":"http:\/\/windowsvj.com\/wpblog\/?p=3845"},"modified":"2012-07-06T14:36:31","modified_gmt":"2012-07-06T09:06:31","slug":"difference-between-int1-vs-tinyint1-in-mysql","status":"publish","type":"post","link":"http:\/\/windowsvj.com\/wpblog\/2012\/07\/difference-between-int1-vs-tinyint1-in-mysql\/","title":{"rendered":"Difference between INT(1) vs TINYINT(1) in MySQL"},"content":{"rendered":"<p>The other day while designing a database schema i was suggested to use Tinyint instead of int. Tinyint, never ever used in SQL. So i digged a bit about this datatype. Later i thought probably i can use INT(1) for declaring the database field rather than TINYINT(1) as both might be restricted to a single digit only. Yes they would. But representation of TINYINT(1) is not exactly this. I&#8217;ve always just used INT(1) to say it&#8217;s an integer and it will only be one digit, which I assume means it could only be a value 0 through 9. <\/p>\n<p>So, is there any difference between the various integer types INT, TINYINT, SMALLINT, MEDIUMINT, and BIGINT if you define a length of 1 for each type? And the answer is Yes there is a big diffeence between these data types though they belong to same class. <\/p>\n<p>The number in parentheses for integer column types is the &#8220;display width&#8221;. This does not effect the storage requirements as they are pre-defined. INT(1) does not allow only one character, infact it sets the display width for that column in result sets. The display width is a number from 1 to 255. You can set the display width if you want all of your integer values to &#8220;appear&#8221; similarly. So INT(5) will not hold maximum value as 99999 but it will set the width of number as 5 for display and will truncate more or pad with space if less. <\/p>\n<p>As an example quoted from <a href=\"http:\/\/matthom.com\/archive\/2006\/11\/28\/mysql-integer-columns-and-display-width\" title=\"Matthom\">Matthom<\/a><\/p>\n<p>INT(6) sets the display as: <\/p>\n<p>    55<br \/>\n   643<br \/>\n  1098<br \/>\n 81153<\/p>\n<p>where the left side are padded with spaces.<\/p>\n<p>To help visualize it easier, try this:<\/p>\n<p>_ _ _ _ 5 5<br \/>\n_ _ _ 6 4 3<br \/>\n_ _ 1 0 9 8<br \/>\n_ 8 1 1 5 3<\/p>\n<p>Notice how they all have a display width of 6.<\/p>\n<p>Also, notice the datatype range and size would be different for int and tinyint. While int takes 4 bytes, tinyint takes just a single byte.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The other day while designing a database schema i was suggested to use Tinyint instead of int. Tinyint, never ever used in SQL. So i digged a bit about this datatype. Later i thought probably i can use INT(1) for declaring the database field rather than TINYINT(1) as both might [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[258],"tags":[],"class_list":["post-3845","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/posts\/3845","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/comments?post=3845"}],"version-history":[{"count":1,"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/posts\/3845\/revisions"}],"predecessor-version":[{"id":3847,"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/posts\/3845\/revisions\/3847"}],"wp:attachment":[{"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/media?parent=3845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/categories?post=3845"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/tags?post=3845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}