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
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Database Programming.
Lesson complete
Earn the 3 college credits for Database Programming
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Precision Number Types: DECIMAL and NUMERIC →← 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 →
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.
Enroll now → Course details Download the syllabus All Computer Science courses Every credit course with PRO →
Why take it here
Why students take Database Programming with UPI Study
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.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →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?
What does UNSIGNED mean for MySQL integers?
What happens when you insert a number too large for an integer column?
How do you prevent negative numbers in an integer column?
About the Database Programming course
How many college credits is Database Programming?
How much does Database Programming cost and what is included?
Which universities accept UPI Study credits?
How long does Database Programming take?
How are the exams done?
How do my credits reach my college?
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
3 credits · NCCRSEnroll →