chevron_left
Data types Cookbook
thumb_up
0
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings
When to use INT or STRING in MySQL
Database
chevron_rightMySQL
chevron_rightCookbooks
chevron_rightData types Cookbook
schedule Jul 1, 2022
Last updated local_offer MySQL
Tags tocTable of Contents
expand_more When you need to select a data type for what appears to be a number, always think about whether you would need to do any computations with them. For instance, suppose we are deciding whether to make our ZIP code a number or a string.
Sample ZIP Code: 010-1613
Since we do not need to do any arithmetic with the ZIP Code, we can simply choose the data type to be string.
NOTE
In particular, when our ZIP code starts with a 0, MySQL will drop the leading 0 when treating it as an INT
. By using a string, we make sure that the 0 in the front remains!
Example
To create a table that stores ZIP code and specify data type of zip_code
as VARCHAR
:
CREATE TABLE address ( id INT UNSIGNED AUTO_INCREMENT, zip_code VARCHAR(8), PRIMARY KEY (id));INSERT INTO address (zip_code) VALUES ('01016');SELECT * FROM address;
+----+----------+| id | zip_code |+----+----------+| 1 | 01016 |+----+----------+
If we had specified data type of zip_code
as INT
:
CREATE TABLE address ( id INT UNSIGNED AUTO_INCREMENT, zip_code INT, PRIMARY KEY (id));INSERT INTO address (zip_code) VALUES (01016);SELECT * FROM address;
+----+----------+| id | zip_code |+----+----------+| 1 | 1016 |+----+----------+
Notice that the 0 at the start of the ZIP code is dropped.
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!