Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

Morten Hauge
Morten Hauge
9,240 Points

What data type would you use to store a phone number?

In a code challenge in "Modifying Databases With SQL" we inserted a phone number into our database. I wrapped the phone number in quotes as if it was a string and the challenge passed, but is this the right practice?

A phone number is just an integer, no? What data type would be considered best practice if you were to store a phone number in your database? One downside I guess would be that you could "accidentaly" perform math on the phone number if you store it as an integer, but I really don't see how you could end up doing that.

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

A phone number should always be stored as a string or text and never an integer. Some phone numbers generally use hyphens and possibly parentheses. Also, you might need to indicate the country code before the phone number such as +46 5555-555555. Keep in mind also, that not all phone numbers have the same number of digits for area codes and exchanges in different countries. I live in Sweden, and my phone number might look and be formatted much differently than yours.

And you're correct, there's no situation I know of where you'd want to perform any arithmetic operations on a phone number, social security number or similar types of numbers.

Hope this helps! :sparkles:

Morten Hauge
Morten Hauge
9,240 Points

What you said about the hyphen and country codes actually makes a lot of sense. Thanks for clearing that up for me!

Kevin Korte
Kevin Korte
28,149 Points

I'd like to echo what Jennifer said. While a phone number is mostly numbers, its purpose is more suited as a "string", since we're typically storing it to display again.

Lets say you were building a web app that took subscriptions. You might store the last 4 digits of their credit card, they expirey month and year, etc. While those would all be numbers, it would make more since to store them as a string.

However, if you were storing how many upvotes a forum post got ahem, it would make sense to store that as a number, so it's easier to increase that number by one, when you upvote my post ahem.

All kidding aside though, if you think about how you'll use your number in the future, it should make it more clear if its a number type, or a string.

Morten Hauge
Morten Hauge
9,240 Points

Thanks for the tips, Kevin! I'll be sure to keep these things in mind for later.

You guys are great!

Kevin Korte
Kevin Korte
28,149 Points

You're welcome Morten, and thanks Jennifer Nordell for the upvote...LOL