Section 10 - Revisiting Data Types Flashcards
What might be one reason for working with CHAR instead of VARCHAR?
For a reminder, VARCHAR is a variable character length, whereas CHAR is a set character length.
- Maybe we’re working with something like a state field, where it always needs to be 2 characters long.
- If the field you need is a set character length always, sometimes it makes sense to set use CHAR, because CHAR speeds up data searches compared to VARCHAR. It’s not normally noticeable, though when working with massive amounts of data, it can become important.
What happens when you have a CHAR field, say with a length 5, and an entry is inserted that’s over that limit?
The entry is truncated.
If you have a CHAR field, say with a length of 10, and data is inserted under that length, what happens?
Additional spaces are added to the data to get the length to 10.
In this way, CHAR would allocate 10 characters to each piece of data inserted into the column.
If you have a CHAR field, and there are entries under the specified character length, does MySQL show you the additional spaces when the shorter-than-specified entries are printed?
NO, it does not.
That is, unless the PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.
What is the DECIMAL numeric type.
It is used for decimals. (duh)
How is the DECIMAL numeric type formatted?
DECIMAL(1st_number, 2nd_number)
The 1st number is the total number of digits allocated.
The 2nd number is the digits after the decimal point.
For example, the number 12.34 would be represented as DECIMAL(4, 2)
The number 1587.394 would be
DECIMAL(7, 3)
The number 12345 would be DECIMAL(5,0)
Recall the DECIMAL numeric type.
DECIMAL(4, 2)
What do the numbers 4 and 2 represent in the above example?
The 4 represents the total number of digits, whereas the 2 represents the number of digits after the period.
Write a command for creating a table.
- That table should be called animals, and in that table should be an animal_name column, and an animal_weight column, which should be a decimal with 4 digits before the period and 2 digits after the period.
CREATE TABLE animals(animal_name VARCHAR(100), animal_weight DECIMAL(6, 2));