Whole Number Data Types: INT and SMALLINT, Explained

UPI Study › Courses › Database Programming › Free lessons › Lesson 3.5 Free with a free account

Whole Number Data Types: INT and SMALLINT

Database Programming · Chapter 3: Data Definition Language and Schema DesignSection 3.5 · lesson 19 of 769 min · free with a free account

← Character Data Types: CHAR vs. VARCHARNext lesson: Precision Number Types: DECIMAL and NUMERIC →

A free account plays all of Chapter 1 and 3 more lessons of Database Programming. Create a free account →

Earn 3 college credits for Database ProgrammingRecommended for credit by NCCRS · Computer Science 381 · self-paced · one payment, lifetime access

In this lesson

Pick integer types by range, signedness, and real business rules.

  • Compare storage sizes and ranges of TINYINT, SMALLINT, INT, and BIGINT
  • Explain how MySQL UNSIGNED shifts a range and how SQL Server differs
  • Describe overflow behavior in strict and relaxed modes
  • Enforce business rules such as nonnegative stock with a CHECK constraint

Free study notes

Get this lesson's notes by email

The key points and 4 practice questions with answers, in your inbox. One email, free.

One email. Unsubscribe in one click.

Transcript

Everything said in the video, section by section (1,021 words). Read along, or search it.

Opening Question

Can a Smaller Type Stop Negative Stock

Maya Rao, owner of Lantern Books, wants a stock count that cannot become negative. Leo Park, the developer, suggests a smaller integer type. That saves space on some database systems, but does it stop negative stock? A type's capacity and a business rule answer different questions. We will choose room for growth, then test the rule explicitly on a disposable inventory table.

Small Integer Types

TINYINT, SMALLINT, and MEDIUMINT Ranges

An integer has no fractional part. Zero, positive counts, and negative adjustments can all be integers; prices and measurements need separate decimal decisions. In ordinary MySQL integer storage, TINYINT uses one byte, SMALLINT two, and MEDIUMINT three. Larger types cover larger ranges. Signed values include negatives. MySQL's signed TINYINT spans negative one hundred twenty-eight through one hundred twenty-seven; SMALLINT reaches positive thirty-two thousand seven hundred sixty-seven. MEDIUMINT reaches a little over eight million on its signed positive side. It is a MySQL type, with no equivalent type name in SQL Server. SQL Server TINYINT instead runs from zero through two hundred fifty-five. The other shared integer types are signed.

Larger Types

INT and BIGINT Capacity

INT uses four bytes in MySQL and SQL Server. Its signed range extends a little beyond negative and positive two billion. BIGINT uses eight bytes, reaching about nine quintillion in each direction. The exact limits are shown here for both types. Choose BIGINT when identifiers or accumulated counts could exceed INT. A common type is not automatically large enough for every growing application. Older MySQL declarations sometimes include a display width, such as INT with eleven in parentheses. That deprecated width does not change the integer range. These fixed-width comparisons describe the server types. SQLite uses a different storage model; its SMALLINT name does not impose the server's two-byte boundary.

Bit Patterns

Two Bytes Give 65,536 Patterns

A byte contains eight bits. Each bit has two states, so one byte has two hundred fifty-six possible patterns. Two bytes provide sixty-five thousand five hundred thirty-six patterns. Signed SMALLINT starts at negative thirty-two thousand seven hundred sixty-eight, ending at positive thirty-two thousand seven hundred sixty-seven. MySQL UNSIGNED shifts those same patterns to zero through sixty-five thousand five hundred thirty-five. No extra storage appears; negative values become unavailable. In general, n bytes provide two hundred fifty-six raised to n possible patterns. SQL Server has no UNSIGNED keyword; use an appropriate signed type and constraints.

UNSIGNED

Shifting the Range to Zero

MySQL writes UNSIGNED after an integer type. Its TINYINT then reaches two hundred fifty-five, and INT reaches a little beyond four billion. The displayed unsigned maxima show the same pattern for every width. A nonnegative requirement alone does not determine how much future growth needs room. To migrate unsigned data to SQL Server, compare actual required ranges. Unsigned SMALLINT values may require INT, while unsigned INT values may require BIGINT. A CHECK rule can prohibit negative values. It cannot enlarge the maximum that the underlying server type can represent.

Portability Test

SQLite Stores 40,000 in SMALLINT

Test a portability assumption before making it a production rule. This SQLite table declares SMALLINT, then receives forty thousand. Run the script. A MySQL or SQL Server signed SMALLINT would be too narrow, but that fact does not establish SQLite's behavior. SQLite stores forty thousand as an integer. Its ordinary type declaration gives affinity rather than the server's SMALLINT range enforcement. The successful insert is evidence about this engine only. A server migration must still validate every value against the destination type's limits.

Overflow

Out-of-Range Values and Integer Division

Overflow means a value exceeds its type's range. Strict MySQL rejects an out-of-range integer; SQL Server also reports an error. MySQL's relaxed modes can instead clip the value to a boundary and issue a warning. That is a changed quantity, not the original request. Do not describe this as wrapping around into a negative number. Check the mode and read warnings, rather than assuming every successful insert preserved its input. Arithmetic has product-specific rules too. SQL Server integer division discards the fractional part; MySQL's slash operator gives a decimal result for integer operands.

Business Rule

A CHECK for Nonnegative Stock

Here is the missing stock policy. The sandbox starts with twelve copies of product eleven, matching our shared product baseline. An explicit CHECK requires a nonnegative integer. SQLite's typeof test closes the gap left by an ordinary integer affinity declaration. The result confirms the initial stock and the default reorder level of ten. A default supplies an omitted value; CHECK decides whether a supplied value is acceptable. On MySQL and SQL Server, use their integer type for integer enforcement, plus the nonnegative CHECK. The business restriction remains separate from the type's capacity.

Choosing a Type

Headroom Without Wasted Bytes

Start by asking whether the value is a quantity. Phone numbers and postal codes belong in text, because their signs, letters, and leading zeros matter. Estimate the largest future value, decide whether negatives are valid, and choose a type with headroom. A boundary already nearly full is a migration waiting to happen. A five-star rating needs little capacity. A hall with twelve hundred seats fits SMALLINT, while a large order identifier may need BIGINT. In an illustrative ten-million-row table, replacing four-byte INT with one-byte TINYINT saves thirty million raw bytes for that column. That is thirty decimal megabytes before overhead or compression. Indexes containing the column can multiply the storage effect; the smallest safe type is the target.

Quick Check

Sixty Thousand in a SQLite Column

Leo tests a new SQLite column with an explicit unsigned-style range. The inserted value is sixty thousand. Read both the declaration and the CHECK. Which outcome does the final query produce on this engine? Pause, and decide. Sixty thousand is stored. SQLite's SMALLINT declaration does not enforce the signed server limit, and the explicit CHECK accepts this integer within its range. Run the file to verify. The rule defines a nonnegative range in SQLite; it does not turn SQLite into MySQL's unsigned physical storage format. Clipping to the signed maximum is not requested here. Rejecting the row would assume a type limit this SQLite declaration does not impose.

Key Takeaways

Capacity and Meaning Together

Maya's stock policy now has both capacity and meaning. Pick a range large enough for growth, then enforce the business restriction explicitly. Check signedness and dialect before reusing a type name. SQLite's flexible declarations differ from MySQL and SQL Server range enforcement. Treat overflow and warnings as evidence to investigate. A smaller type helps only while it can still represent every legitimate value.

About Database Programming at UPI Study

Database Programming (Computer Science 381) is a self-paced online course of 76 video lessons in 12 chapters, from database applications, SQL setup, schema planning and normalization to keys and constraints, data types, filtering and sorting, joins, subqueries, views, indexes, ROLLUP and PIVOT, regular expressions and database security. It carries a NCCRS credit recommendation for 3 semester credits.

Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Compare: Database Fundamentals vs Database Programming

Why take it here

Why students take Database Programming with UPI Study

Write the SQL behind every appDatabase Programming teaches SQL in MySQL and SQL Server side by side: designing and normalizing tables, keys and constraints, data types, inserting, updating and deleting rows, filtering, grouping, joins, subqueries, views, indexes, transactions, and protecting data from SQL injection. It suits computer science and information technology degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

Database Programming carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.

FAQ

Questions students ask us

About Whole Number Data Types: INT and SMALLINT

What is the range of SMALLINT and INT in SQL?
In MySQL and SQL Server, a signed SMALLINT uses 2 bytes and holds -32,768 to 32,767, while INT uses 4 bytes and holds -2,147,483,648 to 2,147,483,647. BIGINT uses 8 bytes and reaches about 9.2 quintillion in each direction. SQLite behaves differently: a SMALLINT declaration gives the column integer affinity, so SQLite does not enforce the two-byte range.
What does UNSIGNED mean for MySQL integers?
In MySQL, UNSIGNED removes negative values and shifts the range upward, so an unsigned SMALLINT holds 0 to 65,535 and an unsigned INT holds 0 to 4,294,967,295. SQL Server has no UNSIGNED option, so migrated unsigned data may need a wider type, such as INT for unsigned SMALLINT values. A CHECK constraint can block negatives, but it cannot raise the maximum of the underlying type.
What happens when you insert a number too large for an integer column?
It depends on the system and mode. MySQL in strict mode rejects the out-of-range value with an error, and SQL Server also reports an error. MySQL's relaxed modes can clip the value to the nearest boundary with a warning, which changes the stored quantity; it does not wrap around to a negative number. SQLite stores 40,000 in a SMALLINT column, because its declared types give affinity rather than range limits.
How do you prevent negative numbers in an integer column?
Add a CHECK constraint such as CHECK (stock >= 0), plus NOT NULL if a value is required. MySQL also offers UNSIGNED, but a CHECK states the business rule directly and works in SQL Server too. In ordinary SQLite tables, add typeof(stock) = 'integer' to the check, because integer affinity lets text such as 'abc' through, and SQLite compares text as greater than any number.

About the Database Programming course

How many college credits is Database Programming?
Database Programming (Computer Science 381) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Database Programming cost and what is included?
$250, paid once, with lifetime access: all 76 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Database Programming take?
It is self-paced with no deadlines. The 76 lessons total about 11 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in Database Programming.

76 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

Updated

Database Programming
3 credits · NCCRS
Enroll →