Data Types Flashcards

(30 cards)

1
Q

What is the main purpose of data types in SQL?

A

To control what kind of data is allowed in each column.

Data types ensure data integrity and proper storage.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do data types prevent?

A

Nonsense data like “19.99 bananas” entering your table.

This helps maintain the accuracy of the database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why are data types important for sorting and filtering?

A

Sorting only works correctly when types are correct (e.g., number vs text).

Incorrect types can lead to unexpected results.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What happens if data types are too large (e.g., VARCHAR(5000))?

A

Slow queries and wasted storage.

Efficient data types improve performance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s the correct type for storing book titles?

A

VARCHAR or NVARCHAR.

Use VARCHAR for standard text and NVARCHAR for international characters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s the correct data type for a published date?

A

DATE.

DATE type is specifically designed for date values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What data type should store price values?

A

DECIMAL(precision, scale).

DECIMAL ensures exact representation of monetary values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What data type should store “active/not active”?

A

BIT.

BIT is efficient for binary states.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is TINYINT used for?

A

Small counts (0–255), ratings, flags.

TINYINT is a space-efficient integer type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is SMALLINT used for?

A

Counts up to ~32,000 (stock levels, medium inventory).

SMALLINT is suitable for moderate range integer values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is INT used for?

A

Most general whole number tasks (IDs, counts).

INT is the standard integer type in SQL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is BIGINT used for?

A

Very large numbers — only when truly necessary.

Use BIGINT for values exceeding INT limits.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is DECIMAL used for?

A

Precise numbers like prices or money.

DECIMAL avoids rounding errors common in floating-point types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why is DECIMAL better than FLOAT for money?

A

FLOAT causes rounding errors; DECIMAL is exact.

Financial calculations require precision.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is FLOAT used for?

A

Scientific values, measurements, coordinates.

FLOAT is suitable for approximate values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Should FLOAT be used for money?

A

No — rounding is unreliable.

Use DECIMAL for financial data instead.

17
Q

When should you use DATE?

A

Birthdays, published dates, simple dates.

DATE type is ideal for storing date values.

18
Q

When should you use DATETIME?

A

Timestamps with hours/minutes/seconds.

DATETIME captures both date and time.

19
Q

What is TIME used for?

A

Storing only the time of day.

TIME type is focused solely on time values.

20
Q

When should you use VARCHAR?

A

Normal English text; lightweight and efficient.

VARCHAR is flexible for variable-length strings.

21
Q

When should you use NVARCHAR?

A

Any user input; supports accents and international characters.

NVARCHAR is essential for multilingual applications.

22
Q

Why is NVARCHAR safer?

A

It handles all human languages reliably.

NVARCHAR prevents data loss in character representation.

23
Q

What is the storage difference between VARCHAR vs NVARCHAR?

A

VARCHAR = 1 byte per character; NVARCHAR = 2 bytes per character.

Choose based on character set requirements.

24
Q

What are VARBINARY/BINARY used for?

A

Storing files, encrypted data, images, etc.

BINARY types are for binary data storage.

25
What is **XML** used for?
Structured text-based hierarchical data. ## Footnote XML is useful for data interchange.
26
What are **spatial types** used for?
Maps, GPS, coordinates. ## Footnote Spatial types handle geographical data.
27
What is **Rule One** for choosing a data type?
Pick the most specific type — don’t use VARCHAR for everything. ## Footnote Specific types enhance performance and clarity.
28
What is **Rule Two**?
Choose realistic sizes (avoid waste). ## Footnote Efficient sizing conserves resources.
29
What is **Rule Three**?
Use NVARCHAR for anything that might contain international text. ## Footnote This ensures compatibility with diverse languages.
30
What is **Rule Four**?
Money → always DECIMAL, never FLOAT. ## Footnote This rule prevents financial inaccuracies.