JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Formatting and storing phone numbers in MySQL

Author:JIYIK Last Updated:2025/04/10 Views:

MySQL provides a variety of convenient data types for storing expected input into its database. Knowing the correct data type to use to store special data is key to ensuring an optimized and efficient database.

This tutorial explains the format and storage of telephone numbers in a MySQL database.


Understanding MySQL Data Types

MySQL generally has three data types:

  1. Numbers ( INT, BIT, FLOATetc.)
  2. String ( CHAR, VARCHAR, TEXTetc.)
  3. Date and time ( DATE, DATETIME, TIMESTAMPetc.)

These general data types also have subtypes for specific cases of data processing in the database.

Check out this reference on various RDBMS (relational database management system) data types from w3schools for further reading.

When dealing with phone numbers, you might quickly consider using the Number data type. However, because phone numbers come in various formats (country codes, separators, etc.) and have special characters, this can be problematic.

Since a phone number can be up to fifteen (15) digits in length, according to the International Telecommunication Union, a string type CHAR, VARCHARor TINYTEXT, is the best consideration.

CHARand TINYTEXTare limited to two hundred fifty-five (255) characters, while VARCHARuses dynamic memory allocation to store data, up to a defined limit between 0 and 65535.

Let's take the example of storing phone numbers for each data type to determine the most appropriate choice.


CHARStoring phone numbers in MySQL using

CHARThe data type (short for character) can store fixed-length strings between 0 and 255 characters. A column that implements CHAR can specify an upper constraint between 0 and 255, and MySQL expects each string in the column to be the same size.

When you enter a string shorter than the specified constraint, MySQL fills the remaining space with spaces.

For example, if a table column specifies a CHAR data type with a size constraint of thirty characters (30), passing a 10-character value still takes up 30 characters of space (10 data characters and 20 spaces).

This can be discussed further in this official MySQL documentation.

To illustrate, let's create a sample registration system database.

-- Initializing
CREATE DATABASE new_registration_db;
USE new_registration_db;

-- CREATING TABLES
CREATE TABLE registered_users (
	id INT AUTO_INCREMENT,
    username VARCHAR (255) NOT NULL,
    email VARCHAR(255),
    phone CHAR (15) NOT NULL,
    PRIMARY KEY(id)
);

-- POPULATING THE TABLE WITH SAMPLE REGISTRATION DATA
INSERT INTO registered_users(username, email, phone) Values
	('Mark Laurent', 'MarkRLaurent@teleworm.us','+1 908-204-0495'),
    ('Patricia Todd', 'PatriciaJTodd@teleworm.us','+1 801-752-2367'),
    ('Victoria McDonald', 'VictoriaAMcDonald@dayrep.com', '+1 608-299-8640'),
	('Vin Petrol', 'vin_not_diesel@crudemail.com','+1 870-381-6967');

Output:

1 row(s) affected
0 row(s) affected
0 row(s) affected
4 row(s) affected Records: 4  Duplicates: 0  Warnings: 0

Now, let's preview the resulting table.

SELECT * FROM registered_users;    -- Checking the table

Output:

id	username			email							phone
1	Mark Laurent		MarkRLaurent@teleworm.us		+1 908-204-0495
2	Patricia Todd		PatriciaJTodd@teleworm.us		+1 801-752-2367
3	Victoria McDonald	VictoriaAMcDonald@dayrep.com	+1 608-299-8640
4	Vin Petrol			vin_not_diesel@crudemail.com	+1 870-381-6967
-----------------------------------------------------------------------------------------
4 row(s) returned

Here, since the length of the phone number is fixed to 15 characters, the CHAR data type will provide efficient storage. Also, it can be used as an index for filtering purposes.

However, the CHAR data type might not be efficient in managing memory for applications that use variable-sized phone numbers (global applications) because of the padding with spaces, as described previously.


TINYTEXTStoring phone numbers in MySQL using

TINYTEXTCHARThe data type is the smallest TEXT type data type. It has the same memory limit as the data type between 0 and 255 characters .

However, unlike CHAR, it can dynamically allocate space for the passed value based on its character length. Therefore, for this example of storing phone numbers, it provides better memory efficiency than CHAR.

It does have the disadvantage of having no default value, making it not indexable for sorting or aggregation.

Now, let's redo the previous example using TINYTEXT.

-- CREATING TABLES
CREATE TABLE registered_users2 (
	id INT AUTO_INCREMENT,
    username VARCHAR (255) NOT NULL,
    email VARCHAR(255),
    phone TINYTEXT NOT NULL,
    PRIMARY KEY(id)
);

-- POPULATING THE TABLE WITH SAMPLE REGISTRATION DATA
INSERT INTO registered_users2(username, email, phone) Values
	('Mark Laurent', 'MarkRLaurent@teleworm.us','+1 908-204-0495'),
    ('Patricia Todd', 'PatriciaJTodd@teleworm.us','+1 801-752-2367'),
    ('Victoria McDonald', 'VictoriaAMcDonald@dayrep.com', '+1 608-299-8640'),
	('Vin Petrol', 'vin_not_diesel@crudemail.com','+1 870-381-6967');

SELECT * FROM registered_users2;    -- Checking the table

Output:

id	username			email							phone
1	Mark Laurent		MarkRLaurent@teleworm.us		+1 908-204-0495
2	Patricia Todd		PatriciaJTodd@teleworm.us		+1 801-752-2367
3	Victoria McDonald	VictoriaAMcDonald@dayrep.com	+1 608-299-8640
4	Vin Petrol			vin_not_diesel@crudemail.com	+1 870-381-6967
-----------------------------------------------------------------------------------------
0 row(s) affected
4 row(s) affected Records: 4  Duplicates: 0  Warnings: 0
4 row(s) returned

We get the expected result. Check out this reference for more information about MySQL TINYTEXT and other TEXT data types.


VARCHARStoring phone numbers in MySQL using

The final recommended way to handle phone numbers in MySQL is to use VARCHARthe VARCHAR data type. VARCHAR provides the flexibility of dynamic memory allocation when the length of the phone number varies among database users.

It usually allocates two (2) additional bytes to store the length information. Therefore, if a length of six (6) characters is stored, VARCHAR needs to allocate a total of 8 bytes of memory.

Columns that specify the VARCHAR data type are also indexable for sorting, aggregation, and primary/foreign key constraints.

Let's create a third table implementing VARCHAR for phone number assignments.

-- CREATING TABLES
CREATE TABLE registered_users3 (
	id INT AUTO_INCREMENT,
    username VARCHAR (255) NOT NULL,
    email VARCHAR(255),
    phone VARCHAR (15) NOT NULL,
    PRIMARY KEY(id)
);

-- POPULATING THE TABLE WITH SAMPLE REGISTRATION DATA
INSERT INTO registered_users3(username, email, phone) Values
	('Mark Laurent', 'MarkRLaurent@teleworm.us','+1 908-204-0495'),
    ('Patricia Todd', 'PatriciaJTodd@teleworm.us','+1 801-752-2367'),
    ('Victoria McDonald', 'VictoriaAMcDonald@dayrep.com', '+1 608-299-8640'),
	('Vin Petrol', 'vin_not_diesel@crudemail.com','+1 870-381-6967');

SELECT * FROM registered_users3;    -- Checking the table

Output:

id	username			email							phone
1	Mark Laurent		MarkRLaurent@teleworm.us		+1 908-204-0495
2	Patricia Todd		PatriciaJTodd@teleworm.us		+1 801-752-2367
3	Victoria McDonald	VictoriaAMcDonald@dayrep.com	+1 608-299-8640
4	Vin Petrol			vin_not_diesel@crudemail.com	+1 870-381-6967
-----------------------------------------------------------------------------------------
0 row(s) affected
4 row(s) affected Records: 4  Duplicates: 0  Warnings: 0
4 row(s) returned

Implementing any of the three data types above is sufficient to handle phone numbers in a MySQL database.

However, choosing the most appropriate data type in terms of memory efficiency and speed depends on the intended database application.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

If ELSE in MySQL

Publish Date:2025/04/11 Views:85 Category:MySQL

In this tutorial, we aim to explore how to use IF ELSE the statement in MySQL. One of the key roles of a data analyst is to gather insights from the data and produce meaningful results. It can be done with the help of several data filtering

DATETIME vs. TIMESTAMP in MySQL

Publish Date:2025/04/11 Views:117 Category:MySQL

DATETIME and TIMESTAMP are two different data types that can be used to store values ​​that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea

Execute multiple joins in one query in MYSQL

Publish Date:2025/04/11 Views:94 Category:MySQL

Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.

Joining 3 tables in MySQL

Publish Date:2025/04/11 Views:187 Category:MySQL

In this tutorial, we will learn how to join three tables in MySQL. Businesses and organizations may have to visualize three tables simultaneously based on certain matching columns common to all three tables. This operation is allowed in MyS

Use of UPDATE JOIN in MySQL

Publish Date:2025/04/11 Views:85 Category:MySQL

This tutorial will explain how to use the statement in MySQL database UPDATE JOIN . We generally use joins to iterate over the rows in a particular table which may or may not have similar rows in other tables. We can UPDATE use JOIN the cla

How to use the Row_Number() function in MySQL

Publish Date:2025/04/11 Views:142 Category:MySQL

In this tutorial, we will explain how to use the VALUES function in MySQL ROW_NUMBER() . This is a sorting method that assigns consecutive numbers within a partition starting from 1. It is important to note that no two rows within a partiti

Multiple primary keys in MySQL

Publish Date:2025/04/11 Views:66 Category:MySQL

In this tutorial, our goal is to explore the concept of multiple primary keys for a table in MySQL. Many times, businesses and organizations have to assign certain columns as primary keys. This primary key has multiple uses and reasons to b

Displaying foreign keys in MySQL

Publish Date:2025/04/11 Views:55 Category:MySQL

In this tutorial, we aim to explore how to display foreign keys for tables and columns in MySQL. The type of key that references a primary key, also known as the primary key of another table, is called a foreign key. Understanding the forei

Select first N rows in MySQL

Publish Date:2025/04/11 Views:85 Category:MySQL

Sometimes, you have to select first N rows of MySQL database according to your project requirements. n The value of varies according to the requirement; it can be TOP 1 row or TOP 30 rows. We will learn how to select top N rows using the cl

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial