search
Search
Login
Math ML
Map of Data Science
Join our weekly DS/ML newsletter
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Outline
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
brightness_medium
share
arrow_backShare
check_circle
Mark as learned
thumb_up
0
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings

When to use INT or STRING in MySQL

Database
chevron_right
MySQL
chevron_right
Cookbooks
chevron_right
Data 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.

mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!