MySQL Chapter 2: Tables & Data Types

Chapter 2: Tables & Data Types

🎯 Goal:

By the end of this chapter, you'll understand:

  • What a table is.

  • What rows and columns are.

  • What records and fields mean.

  • Why databases use tables.

  • Common SQL data types (INT, VARCHAR, BOOLEAN, DATE, etc.).

  • How choosing the right data type improves performance and data quality.


Part 1 — The Story (Learn Like a Movie)

🎬 The Great Library Gets Organized

In the previous chapter, Dave built the magical database library.

The king was impressed.

But after a few months...

Chaos returned.


The Problem

Every piece of information was being stored on the same giant paper.

Rahul
25
Blue Horse
$500
Doctor
Monday
Pizza
Delhi
Engineer

The king asked,

"Whose age is 25?"

Nobody knew.


Then he asked,

"Is Blue Horse the person's pet, vehicle, or favorite color?"

Nobody knew.


Dave sighed.

"The problem isn't the database anymore...
it's that we aren't organizing the data."


Dave Invents Magic Tables

Instead of one giant sheet...

Dave made separate tables.

👥 People Table

IDNameAge
1Rahul25
2Priya30

🐎 Horses Table

Horse IDOwner IDColor
1011White
1022Black

🏥 Doctors Table

Doctor IDNameSpecialization
1JohnHeart
2AliceSkin

Now everything had a proper place.

The king smiled.


But Another Problem Appeared...

One day...

A guard entered:

"Sir! Someone wrote this!"

Age = Banana

Everyone laughed.


Another person wrote:

Phone Number = Elephant

Chaos again.


Dave realized...

"Not every column should accept everything."


Dave Creates Rules

He told the magical tables:

Age

Only numbers.

25 ✅

40 ✅

Banana ❌

Dog ❌

Name

Only text.

Rahul ✅

Priya ✅

12345 ❌ (not as a person's name)

Is Married

Only

TRUE

FALSE

Not

Maybe ❌

Tomorrow ❌

Birthday

Only dates.

2000-05-20 ✅

2024-01-01 ✅

Pizza ❌

The tables became smarter.

Instead of accepting garbage...

They protected themselves.


Dave's Golden Rule

Dave gathered everyone and said:

"Every shelf has a purpose.

Every column has a purpose.

Every value must match that purpose."

The kingdom never had messy data again.


What actually happened?

Dave's organized shelves are Tables.

Each table stores one type of information.

Instead of mixing everything together...

We separate information into meaningful tables.


What is a Table?

Think of an Excel sheet.

Students
IDNameAge
1Rahul22
2Priya24
3Aman21

This entire structure is called a table.

A table stores related information about one type of thing.


What is a Column?

Columns describe what kind of information is stored.

Example:

| ID | Name | Age |

Each column has one purpose.

ID → Student ID

Name → Student Name

Age → Student Age

Think of columns as questions:

  • What is the ID?

  • What is the name?

  • What is the age?


What is a Row?

A row represents one complete item.

Example:

IDNameAge
1Rahul22

This row contains everything about one student.

Another row:

IDNameAge
2Priya24

One row = One student.


Record vs Row

These words are often used interchangeably.

Row = Record

Both mean one complete entry in a table.


Field vs Column

Similarly,

Column = Field

Example:

Name

Age

Salary

These are fields (columns).


Real-Life Examples

🛒 Amazon

Instead of one giant table...

Amazon has:

Customers

Products

Orders

Payments

Reviews

Sellers

Each stores one type of information.


🏥 Hospital

Tables:

Patients

Doctors

Appointments

Medicines

Bills

Each table has a specific responsibility.


🎓 School

Students

Teachers

Subjects

Marks

Attendance

Everything stays organized.


Now Let's Talk About Data Types

Remember the guard who entered:

Age = Banana

😂

That happened because there were no rules.

Data Types are those rules.


What is a Data Type?

A data type defines what kind of value a column can store.

Think of it as the column's "personality."


INT

Stores whole numbers.

Examples:

21

100

-5

0

Used for:

  • Age

  • Quantity

  • Marks

  • IDs (often)

Not allowed:

25.5 ❌

Hello ❌

VARCHAR

Stores text.

Examples:

Rahul

India

Apple

Database

The number in VARCHAR(n) tells the maximum number of characters allowed.

VARCHAR(50)

Means

Maximum 50 characters.

Examples:

Rahul ✅

Christopher ✅ (if ≤ 50 characters)

A very, very, very long sentence... ❌ (if it exceeds 50 characters)

CHAR

Also stores text.

Difference:

CHAR(5)

Always reserves space for 5 characters.

CAT

Internally becomes something like:

CAT__

(two unused spaces)

Useful for fixed-length values like country codes (US, IN) or short status codes.


BOOLEAN

Only two values.

TRUE

FALSE

Used for:

IsActive

IsMarried

IsVerified

HasPaid

DATE

Stores dates.

2025-07-10

2026-01-01

Useful for:

  • Birthday

  • Joining Date

  • Order Date


DECIMAL

Stores numbers with decimals accurately.

19.99

250.75

1000.50

Used for:

  • Prices

  • Account balances

  • Tax calculations

Unlike floating-point types, DECIMAL is designed to avoid rounding surprises in financial data.


FLOAT / DOUBLE

Stores decimal numbers too, but with approximate precision.

Useful for:

  • Scientific measurements

  • Sensor readings

  • GPS coordinates

Less suitable for money because tiny rounding differences can occur.


Why Can't We Make Everything VARCHAR?

Why not store everything as text?

Age = "25"

Salary = "5000"

Price = "100"

It seems easier...

But then imagine sorting ages.

2

10

100

25

As text, you could get:

10

100

2

25

That's not numeric order!

Using the correct data type allows the database to compare, sort, calculate, and store values efficiently.


Why Data Types Matter

Suppose you store age as text.

Twenty

Banana

Blue

Cat

The database cannot calculate:

  • Average age

  • Maximum age

  • Minimum age

With INT, those operations become straightforward.


Real-Life Example

Imagine a bank.

Balance:

$500.25

Should be stored as:

DECIMAL

Customer name:

Rahul

Should be stored as:

VARCHAR

Account opened on:

2026-01-15

Should be stored as:

DATE

Verified customer?

TRUE

Should be stored as:

BOOLEAN

Quick Summary Table

Data TypeStoresExample
INTWhole numbers25
VARCHAR(50)Variable-length textRahul
CHAR(2)Fixed-length textIN
BOOLEANTrue/FalseTRUE
DATEDates2026-07-25
DECIMAL(10,2)Exact decimal values99.99
FLOATApproximate decimal values3.14159

Part 2 — Question & Answer (Progressive Learning)

Q1. What is a table in a database?

Answer: A table is a structured collection of related data arranged in rows and columns. It usually stores information about one type of entity, such as students or products.


Q2. Why do databases use tables?

Answer: Tables keep data organized, reduce confusion, and make searching, updating, and maintaining information much easier.


Q3. What is a column?

Answer: A column defines one type of information, such as Name, Age, or Salary. Every value in a column represents the same kind of data.


Q4. What is a row?

Answer: A row (also called a record) contains all the information about one item, such as one student or one product.


Q5. What is the difference between a row and a column?

Answer: A row represents one complete record, while a column represents one attribute shared by all records.


Q6. What is a data type?

Answer: A data type specifies what kind of values a column can store, helping the database reject invalid data and process valid data efficiently.


Q7. Why are data types important?

Answer: They improve data accuracy, save storage, enable correct calculations and sorting, and prevent invalid values from being stored.


Q8. Why shouldn't we store everything as text (VARCHAR)?

Answer: Because numeric calculations, sorting, comparisons, and validations become inefficient or incorrect. Using appropriate data types improves both correctness and performance.


Q9. When should we use DECIMAL instead of FLOAT?

Answer: Use DECIMAL for exact values like money. Use FLOAT for approximate values such as scientific measurements where tiny rounding differences are acceptable.


Q10. Can different columns in the same table have different data types?

Answer: Yes. For example, a Students table might have an INT for ID, a VARCHAR for Name, a DATE for Birth Date, and a BOOLEAN for IsActive.


Part 3 — Top 5 MCQs

1. What does a table store?

A. Multiple unrelated files

B. Related information arranged in rows and columns

C. Only numbers

D. Only text

Answer: B


2. Which of the following represents one complete record?

A. Column

B. Table

C. Row

D. Database

Answer: C


3. Which data type is best for storing a person's name?

A. INT

B. VARCHAR

C. BOOLEAN

D. DATE

Answer: B


4. Which data type is most appropriate for storing a product's price?

A. BOOLEAN

B. VARCHAR

C. DECIMAL

D. DATE

Answer: C


5. Why do databases use data types?

A. To make SQL syntax longer

B. To prevent invalid data and improve efficiency

C. To reduce the number of tables

D. To eliminate rows

Answer: B


🧠 Chapter 2 Summary

  • A table stores related information in an organized format.

  • Rows (records) represent individual entries.

  • Columns (fields) define the type of information stored.

  • Data types ensure each column stores appropriate values.

  • Choosing the right data type improves data quality, storage efficiency, and query performance.

  • Common SQL data types include INT, VARCHAR, CHAR, BOOLEAN, DATE, DECIMAL, and FLOAT.


🚀 Next Chapter: CRUD (Create, Read, Update, Delete)

You'll learn how to:

  • Insert new data into tables (CREATE/INSERT)

  • Read data using SELECT

  • Modify existing data with UPDATE

  • Remove data using DELETE

  • Understand how almost every application—from Instagram to banking apps—uses these four basic operations constantly.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.