There's More to Discuss!
Char(3) -> Only 3 Characters Allowed
The length of a CHAR column is fixed to the
length that you declare when you create the table.
The length can be any value from 0 to 255.
When CHAR values are stored, they are right-padded
with spaces to the specified length. When CHAR
values are retrieved, trailing spaces are removed
unless the PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.
Some Really Boring Explanatory Text
Sex: M/F
State Abbreviations: CA, NY
Yes/No Flags: Y/N
Value | Char(4) | Storage | Varchar(4) | Storage |
---|---|---|---|---|
' ' | ' ' | 4 bytes | ' ' | 1 byte |
'ab' | 'ab ' | 4 bytes | 'ab' | 3 bytes |
'abcd' | 'abcd' | 4 bytes | 'abcd' | 5 bytes |
'abcdefg' | 'abcd' | 4 bytes | 'abcdefg' | 5 bytes |
Check out this table!
WHOLE NUMBERS
DECIMAL(13, 2)
DECIMAL(5, 2)
Total Number Of Digits
Digits After Decimal
999.99
5 Digits
2 Digits
DECIMAL(5, 2)
Store larger numbers using less space
BUT.....
(and it's a big BUT)
It comes at the cost of precision
Data Type | Memory Needed | Precision Issues |
---|---|---|
FLOAT | 4 Bytes | ~7 digits |
DOUBLE | 8 Bytes | ~15 digits |
Look, I made a table!
Values With a Date But No Time
'YYYY-MM-DD' Format
Values With a Time But No Date
'HH:MM:SS' Format
Values With a Date AND Time
'YYYY-MM-DD HH:MM:SS' Format
What's a good use case for CHAR?
(just make one up)
CREATE TABLE inventory (
item_name ____________,
price ________________,
quantity _____________
);
Fill In The Blanks
(price is always < 1,000,000)
(The Number)
(The Day Name)
mm/dd/yyyy
January 2nd at 3:15
April 1st at 10:18
Pat Yourself On The Back