Data Type as validation constraint – with example in MySQL

Oftentimes, those less-than-obvious gems of programming go unnoticed. Some things are in place for us to utilize, yet we never think about them. In this blog post, I will cover how you can use the integer data type itself, as a sort of validation constraint for data coming into your table(s). Of course, all situations are unique, so this is not a one-size-fits-all solution. However, under the right circumstances, it could be a handy little trick to have up your sleeve.

several-sets-of-numbers-on-a-column
Photo by Tyler Easton on Unsplash

Note: All data, names or naming found within the database presented in this post, are strictly used for practice, learning, instruction, and testing purposes. It by no means depicts actual data belonging to or being used by any party or organization.

OS and DB used:


  • Xubuntu Linux 16.04.5 LTS (Xenial Xerus)
  • MySQL 8.0.14

Suppose we have this simple table that stores the first name and age of some of our friends:

1
2
3
4
5
6
7
8
mysql> SHOW CREATE TABLE friend_list\G
*************************** 1. row ***************************
       Table: friend_list
Create Table: CREATE TABLE `friend_list` (
  `f_name` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
1 row in set (0.01 sec)

These values are inserted just fine, no problem at all:

1
2
3
4
mysql> INSERT INTO friend_list(f_name, age)
    -> VALUES('Josh', 43), ('Jimmy', 19), ('Annie', -32);
Query OK, 3 rows affected (0.09 sec)
Records: 3  Duplicates: 0  Warnings: 0

But, there is a problem. Notice the last set of values contained the negative number -32 for the age column.

Lucky for us there is a way we can prohibit these type of values from coming into our tables with just the data type alone. In this particular case, the INTEGER data type will work nicely since it can be specified to be UNSIGNED.

Let’s start over:

1
2
mysql> TRUNCATE TABLE friend_list;
Query OK, 0 rows affected (0.64 sec)

And we will modify that age column to have an INTEGER(11) UNSIGNED data type:

1
2
3
mysql> ALTER TABLE friend_list MODIFY COLUMN age INTEGER(11) UNSIGNED;
Query OK, 0 rows affected (0.99 sec)
Records: 0  Duplicates: 0  Warnings: 0

We now have this table structure:

1
2
3
4
5
6
7
8
mysql> SHOW CREATE TABLE friend_list\G
*************************** 1. row ***************************
       Table: friend_list
Create Table: CREATE TABLE `friend_list` (
  `f_name` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `age` int(11) unsigned DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
1 row in set (0.00 sec)

Let’s now run that same INSERT statement again and see what happens:

1
2
3
mysql> INSERT INTO friend_list(f_name, age)
    -> VALUES('Josh', 43), ('Jimmy', 19), ('Annie', -32);
ERROR 1264 (22003): Out of range value for column 'age' at row 3

Notice that error message?

And we can see below, no data was allowed into the table:

1
2
mysql> SELECT * FROM friend_list;
Empty set (0.00 sec)

Reason being, an UNSIGNED INTEGER only accepts positive whole number values.

Pretty useful huh!

Like what you have read? See anything incorrect? Please comment below and thanks for reading!!!

Explore the official MySQL 8.0 Online Manual for more information.

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.

Have I mentioned how much I love a cup of coffee?!?!

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, is performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

Advertisements

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.