MySQL
keyboard_arrow_down 295 guides
chevron_leftData types Cookbook
check_circle
Mark as learned thumb_up
0
thumb_down
0
chat_bubble_outline
0
Comment auto_stories Bi-column layout
settings
When to use INT or STRING in MySQL
schedule Aug 12, 2023
Last updated local_offer
Tags MySQL
tocTable of Contents
expand_more Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!
Start your free 7-days trial now!
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.
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
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!