Skip to content

Commit e5c9de2

Browse files
authored
Update datatypes.md
1.Refactored All Examples: Replaced the multiple, fragmented CREATE TABLE statements with a single, unified students table example. This makes the tutorial more practical and easier to follow, as all common data types are shown working together. Standardized Code Blocks: Replaced all broken :::info blocks with proper sql syntax-highlighted code blocks and clean, separate Markdown tables for query outputs. Expanded Date/Time Section: Added the essential DATETIME and TIMESTAMP types, which were missing, and explained their common use cases (like the last_login example). Improved Numeric Guidance: Changed the gpa example from FLOAT to DECIMAL(3, 2) to teach the best practice for handling precise values (like grades or money) and strengthened the warning against using FLOAT. Integrated UNSIGNED Concept: Removed the separate, confusing UNSIGNED example and integrated the concept directly into the main students table by making the student_id an INT UNSIGNED. Cleaned Up Formatting: Consolidated the redundant tags and keywords in the frontmatter, added horizontal rules (---) between sections for readability, and standardized all SQL data types (e.g., INT, VARCHAR) to UPPERCASE for consistency.
1 parent 8efd406 commit e5c9de2

File tree

1 file changed

+91
-162
lines changed

1 file changed

+91
-162
lines changed

docs/sql/SQL-basics/datatypes.md

Lines changed: 91 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -1,192 +1,121 @@
1+
2+
3+
14
---
25
id: sql-datatypes
36
title: SQL Data Types
47
sidebar_label: SQL Data Types
58
sidebar_position: 5
6-
tags: [sql, data-types, database, relational-databases]
9+
tags:
10+
- sql
11+
- data-types
12+
- database
13+
- relational-databases
14+
- queries
15+
- sql tutorial
16+
- sql basics
17+
- database management
18+
- relational databases
19+
- sql data types tutorial
20+
- sql for beginners
721
description: In this beginner-friendly tutorial, you will learn about SQL data types, which define what kind of data can be stored in a column, making your database organized and efficient.
8-
keywords: [sql, data types, sql tutorial, sql basics, database management, relational databases, sql data types tutorial, sql for beginners, sql in 2025]
922
---
1023

1124
## 📙 Welcome to SQL Data Types!
1225

13-
Hey there! If you're new to SQL or just getting started, this module is perfect for you. Data types are like labels that tell your database what kind of information (like numbers, text, or dates) can go into each column of a table. Think of them as rules to keep your data neat and safe! Let’s explore them step by step with easy examples.
26+
Hey there! If you're new to SQL, data types are a fundamental concept. They are like labels that tell your database what kind of information (like numbers, text, or dates) can go into each column of a table. Think of them as rules to keep your data organized, efficient, and error-free!
1427

15-
### 📘 What Are Data Types?
28+
## 📘 What Are Data Types?
1629

1730
Data types are super important because they:
18-
- Decide what kind of data you can store (e.g., numbers or words).
19-
- Help save space in your database.
20-
- Prevent mistakes, like putting letters in a number column.
21-
22-
Imagine you’re making a table for a school called `students` with columns like `id`, `name`, and `age`. Data types ensure `id` is a number and `name` is text. Let’s see how!
2331

24-
Here’s a handy table to get you started (check the image below for more details):
32+
* **Enforce Data Integrity:** Decide what kind of data you can store (e.g., numbers only or text only).
33+
* **Optimize Storage:** Help save space in your database by allocating only what's needed.
34+
* **Prevent Errors:** Stop you from putting letters in a number column or vice-versa.
2535

26-
![SQL Data Types Table](assets/31-datatypes.png)
36+
Imagine a `students` table. Data types ensure the `student_id` is a number, `first_name` is text, and `enrollment_date` is a date.
2737

28-
> **Pro Tip**: Always pick the right data type to avoid confusion. For example, don’t use a text type for numbers!
38+
Here’s a handy overview to get you started:
2939

30-
### 📘 Numeric Data Types (Numbers Made Simple)
3140

32-
Numeric types are for numbers—whole numbers or decimals. Let’s break them down:
33-
34-
- **TINYINT**: Small numbers, from -128 to 127 (or 0 to 255 if unsigned).
35-
- **INT**: Bigger numbers, from -2,147,483,648 to 2,147,483,647.
36-
- **FLOAT**: Decimals with up to 23 digits (e.g., 3.14).
37-
- **DOUBLE**: Decimals with up to 53 digits (for super precise numbers).
38-
- **BIGINT**: Huge numbers, from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
39-
40-
**Example**:
41-
:::info
42-
<Tabs>
43-
<TabItem value="SQL Code" label="SQL Code">
44-
```sql title="Creating a Table with Numeric Types"
45-
CREATE TABLE students (
46-
id TINYINT,
47-
age INT,
48-
gpa FLOAT
49-
);
50-
INSERT INTO students (id, age, gpa) VALUES (1, 20, 3.75);
51-
```
52-
</TabItem>
53-
54-
<TabItem value="Output" label="Output">
55-
| id | age | gpa |
56-
|-----|-----|------|
57-
| 1 | 20 | 3.75 |
58-
</TabItem>
59-
</Tabs>
41+
:::tip Pro Tip
42+
Always pick the smallest, most appropriate data type for your data. For example, don’t use a text type for numbers you want to do math on!
6043
:::
6144

62-
> **What NOT to Do**: Don’t use FLOAT for money (e.g., $5.99) because it can have tiny rounding errors. Use DECIMAL instead for exact values like `DECIMAL(5,2)` (5 digits total, 2 after the decimal).
45+
---
6346

64-
### 🔄 String Data Types (Text and More)
47+
## ## Example: Building a `students` Table
6548

66-
String types are for text or characters. Here’s what you need:
49+
Instead of looking at types one by one, let's create a single `students` table that uses all the most common data types. This is how you'd build a table in the real world!
6750

68-
- **CHAR(length)**: Fixed length (e.g., CHAR(50) always uses 50 spaces, even if text is shorter).
69-
- **VARCHAR(length)**: Variable length up to a limit (e.g., VARCHAR(50) uses only what’s needed, up to 50 characters).
70-
- **BLOB**: For binary data like images or files (up to 65,535 bytes).
51+
**SQL Code:**
7152

72-
**Example**:
73-
:::info
74-
<Tabs>
75-
<TabItem value="SQL Code" label="SQL Code">
76-
```sql title="Creating a Table with String Types"
53+
```sql
7754
CREATE TABLE students (
78-
id TINYINT,
79-
name VARCHAR(50)
55+
student_id INT UNSIGNED,
56+
first_name VARCHAR(50),
57+
last_name VARCHAR(50),
58+
gpa DECIMAL(3, 2),
59+
enrollment_date DATE,
60+
is_active BOOLEAN,
61+
last_login TIMESTAMP
8062
);
81-
INSERT INTO students (id, name) VALUES (2, 'Alice');
82-
```
83-
</TabItem>
84-
85-
<TabItem value="Output" label="Output">
86-
| id | name |
87-
|-----|-------|
88-
| 2 | Alice |
89-
</TabItem>
90-
</Tabs>
91-
:::
92-
93-
> **What NOT to Do**: Avoid using CHAR for names if lengths vary a lot—it wastes space. Use VARCHAR instead!
94-
95-
### 📘 Date and Time Data Types (Tracking Time)
9663

97-
These types help with dates and times:
98-
99-
- **DATE**: Stores dates like '2025-08-14' (from 1000-01-01 to 9999-12-31).
100-
- **YEAR**: Just the year, from 1901 to 2155 (e.g., 2025).
101-
102-
**Example**:
103-
:::info
104-
<Tabs>
105-
<TabItem value="SQL Code" label="SQL Code">
106-
```sql title="Creating a Table with Date Types"
107-
CREATE TABLE students (
108-
id TINYINT,
109-
enrollment_date DATE
110-
);
111-
INSERT INTO students (id, enrollment_date) VALUES (3, '2025-08-14');
112-
```
113-
</TabItem>
114-
115-
<TabItem value="Output" label="Output">
116-
| id | enrollment_date |
117-
|-----|-----------------|
118-
| 3 | 2025-08-14 |
119-
</TabItem>
120-
</Tabs>
64+
INSERT INTO students VALUES
65+
(1, 'Alice', 'Johnson', 3.75, '2023-08-14', TRUE, '2025-10-21 09:30:00'),
66+
(2, 'Bob', 'Smith', 3.20, '2022-08-14', TRUE, '2025-10-20 15:00:00'),
67+
(3, 'Charlie', 'Brown', 2.50, '2023-08-14', FALSE, NULL);
68+
69+
SELECT * FROM students;
70+
Now, let's break down the types we used.
71+
## Numeric Data Types (Numbers)
72+
Numeric types are for numbers. We used INT, DECIMAL, and BOOLEAN (which stores 0 or 1).
73+
TINYINT: A very small number (-128 to 127).
74+
INT (or INTEGER): A standard whole number (approx. -2.1 billion to 2.1 billion). We used INT UNSIGNED for student_id. (More on UNSIGNED later!)
75+
BIGINT: For huge whole numbers (up to 9 quintillion!).
76+
DECIMAL(p, s): A fixed-point number, perfect for precision. DECIMAL(3, 2) (used for gpa) means 3 total digits, with 2 after the decimal (e.g., 9.99).
77+
FLOAT / DOUBLE: "Floating-point" numbers, for scientific calculations where tiny rounding errors are acceptable.
78+
:::warning What NOT to Do
79+
Don't use FLOAT or DOUBLE for money or any value that must be exact (like GPA). They can cause small rounding errors. Always use DECIMAL for currency and other precise values.
12180
:::
122-
123-
> **What NOT to Do**: Don’t store dates as VARCHAR (e.g., '08-14-2025')—use DATE to enable date functions like comparisons!
124-
125-
### 🔄 Other Data Types (Special Cases)
126-
127-
- **BOOLEAN**: True (1) or False (0) values.
128-
- **BIT(x)**: Stores x bits (e.g., BIT(2) for 0 to 3).
129-
130-
**Example**:
131-
:::info
132-
<Tabs>
133-
<TabItem value="SQL Code" label="SQL Code">
134-
```sql title="Creating a Table with BOOLEAN"
135-
CREATE TABLE users (
136-
id TINYINT,
137-
is_active BOOLEAN
138-
);
139-
INSERT INTO users (id, is_active) VALUES (1, TRUE);
140-
```
141-
</TabItem>
142-
143-
<TabItem value="Output" label="Output">
144-
| id | is_active |
145-
|-----|-----------|
146-
| 1 | 1 |
147-
</TabItem>
148-
</Tabs>
81+
🔄 String Data Types (Text)
82+
String types are for text. We used VARCHAR.
83+
CHAR(length): Fixed length. CHAR(10) always uses 10 characters of space, even if you only store "Hi". It pads the rest with spaces. Good for data of a consistent length (e.g., state codes like 'NY', 'CA').
84+
VARCHAR(length): Variable length. VARCHAR(50) (used for first_name) stores only the characters you insert, up to a max of 50. This is efficient and the most common choice for text.
85+
TEXT: For long-form text, like blog posts or descriptions (up to 65,535 characters).
86+
BLOB: For binary data like images or files.
87+
:::warning What NOT to Do
88+
Avoid using CHAR for text that varies in length (like names or email addresses)—it wastes space. Use VARCHAR instead!
14989
:::
150-
151-
> **What NOT to Do**: Don’t use INT for true/false—BOOLEAN is clearer and safer.
152-
153-
### 🧹 Signed vs. Unsigned (Positive or Negative?)
154-
155-
For numeric types like TINYINT:
156-
- **Signed**: Allows negative numbers (e.g., -128 to 127).
157-
- **Unsigned**: Only positive numbers (e.g., 0 to 255).
158-
159-
**Example**:
160-
:::info
161-
<Tabs>
162-
<TabItem value="SQL Code" label="SQL Code">
163-
```sql title="Unsigned Example"
164-
CREATE TABLE products (
165-
stock TINYINT UNSIGNED
166-
);
167-
INSERT INTO products (stock) VALUES (200);
168-
```
169-
</TabItem>
170-
171-
<TabItem value="Output" label="Output">
172-
| stock |
173-
|-------|
174-
| 200 |
175-
</TabItem>
176-
</Tabs>
90+
## Date and Time Data Types
91+
These types are specifically for storing dates and times.
92+
DATE: Stores a date (Year, Month, Day). We used this for enrollment_date.
93+
Format: YYYY-MM-DD (e.g., 2025-08-14)
94+
TIME: Stores a time (Hours, Minutes, Seconds).
95+
Format: HH:MM:SS (e.g., 14:30:00)
96+
DATETIME: Stores both date and time.
97+
Format: YYYY-MM-DD HH:MM:SS
98+
TIMESTAMP: Also stores date and time. It's special because it's often used to track when a row was last changed and can auto-update. We used this for last_login.
99+
:::warning What NOT to Do
100+
Don't store dates as VARCHAR (e.g., '08-14-2025')! Using proper DATE or DATETIME types allows you to perform calculations, like finding all students who enrolled in the last 30 days.
177101
:::
178-
179-
> **What NOT to Do**: Don’t use signed TINYINT for quantities (e.g., stock) that can’t be negative—use UNSIGNED to maximize the range.
180-
181-
## ✅ What You’ve Learned
182-
183-
Great job! You’ve explored:
184-
- **Numeric Types**: TINYINT, INT, FLOAT, DOUBLE, BIGINT.
185-
- **String Types**: CHAR, VARCHAR, BLOB.
186-
- **Date/Time Types**: DATE, YEAR.
187-
- **Other Types**: BOOLEAN, BIT.
188-
- **Signed vs. Unsigned**: Choosing based on needs.
189-
190-
Try creating a table with these types and inserting data to practice. Avoid the "What NOT to Do" mistakes to keep your database happy!
191-
192-
---
102+
🔄 Other Data Types (Special Cases)
103+
BOOLEAN (or BOOL): Stores TRUE (1) or FALSE (0). We used this for is_active.
104+
ENUM: Lets you define a list of allowed values (e.g., ENUM('Small', 'Medium', 'Large')).
105+
BIT(x): Stores a specific number of bits (e.g., BIT(1) is similar to BOOLEAN).
106+
🧹 Signed vs. Unsigned (Positive or Negative?)
107+
For numeric types like INT, you have a choice:
108+
SIGNED (Default): Allows both positive and negative numbers. An INT ranges from approx. -2.1 billion to +2.1 billion.
109+
UNSIGNED: Allows only positive numbers (and zero). This shifts the range. An INT UNSIGNED ranges from 0 to approx. +4.2 billion.
110+
In our students table, we used INT UNSIGNED for student_id. This is perfect because a student ID will never be negative, and it effectively doubles our available positive IDs!
111+
:::tip Pro Tip
112+
Use UNSIGNED for any number column that can never be negative (like an ID, age, or stock quantity).
113+
:::
114+
✅ What You've Learned
115+
Great job! You’ve explored the most important SQL data types:
116+
Numeric Types: INT, DECIMAL (for precision), and FLOAT.
117+
String Types: CHAR (fixed) vs. VARCHAR (variable).
118+
Date/Time Types: DATE, DATETIME, and TIMESTAMP.
119+
Other Types: BOOLEAN for true/false values.
120+
SIGNED vs. UNSIGNED: How to optimize your number ranges.
121+
You've also seen how to build a single, logical table using a mix of these types. This is the foundation for building any good database!

0 commit comments

Comments
 (0)