Mysql Flashcards
CHAR(N)
CHAR(n) is a string of a fixed length of n characters. If it is CHARACTER SET utf8mb4, that means it occupies exactly
4*n bytes, regardless of what text is in it.
Most use cases for CHAR(n) involve strings that contain English characters, hence should be CHARACTER SET ascii.
(latin1 will do just as good.)
DATE
The DATE datatype comprises the date but no time component. Its format is ‘YYYY-MM-DD’ with a range of
‘1000-01-01’ to ‘9999-12-31’
DATETIME
The DATE datatype comprises the date but no time component. Its format is ‘YYYY-MM-DD’ with a range of
‘1000-01-01’ to ‘9999-12-31’
TIMESTAMP
The TIMESTAMP type is an integer type comprising date and time with an effective range from ‘1970-01-01 00:00:01’
UTC to ‘2038-01-19 03:14:07’ UTC.
YEAR
The YEAR type represents a year and holds a range from 1901 to 2155
TIME
The TIME type represents a time with a format of ‘HH:MM:SS’ and holds a range from ‘-838:59:59’ to ‘838:59:59’
Why not simply varchar(255)?
-> When a complex SELECT needs to create temporary table (for a subquery, UNION, GROUP BY, etc), the
preferred choice is to use the MEMORY engine, which puts the data in RAM. But VARCHARs are turned into CHAR
in the process. This makes VARCHAR(255) CHARACTER SET utf8mb4 take 1020 bytes. That can lead to needing
to spill to disk, which is slower
-> In certain situations, InnoDB will look at the potential size of the columns in a table and decide that it will be
too big, aborting a CREATE TABLE
VARCHAR versus TEXT
->Never use TINYTEXT.
->Almost never use CHAR – it is fixed length; each character is the max length of the CHARACTER SET (eg, 4
bytes/character for utf8mb4).
->With CHAR, use CHARACTER SET ascii unless you know otherwise.
->VARCHAR(n) will truncate at n characters; TEXT will truncate at some number of bytes. (But, do you want
truncation?)
->*TEXT may slow down complex SELECTs due to how temp tables are handled
INT as AUTO_INCREMENT
Any size of INT may be used for AUTO_INCREMENT. UNSIGNED is always appropriate
INTEGER TYPES
TINYINT 1 Bytes, SMALLINT 2 Bytes, MEDIUMINT 3 Bytes, INTEGER, INT 4 Bytes, BIGINT 8 Bytes
Fixed Point Types
DECIMAL, NUMERIC
Floating Point Types
FLOAT 4 bytes, DOUBLE 8 bytes
Bit Value Type
BIT
The BIT type is useful for storing bit-field values. BIT(M) allows storage of up to M-bit values where M is in the
range of 1 to 64
You can also specify values with bit value notation.
b’111’ -> 7
b’10000000’ -> 128
Sometimes it is handy to use ‘shift’ to construct a single-bit value, for example (1 «_space;7) for 128.
The maximum combined size of all BIT columns in an NDB table is 4096.
Decimal
hese values are stored in binary format. In a column declaration, the precision and scale should be specified
Precision represents the number of significant digits that are stored for values.
Scale represents the number of digits stored after the decimal
salary DECIMAL(5,2)
Comments
This comment continues to the end of line
– This comment continues to the end of line
/* This is an in-line comment /
/
This is a
multiple-line comment
*/
INSERT, ON DUPLICATE KEY UPDATE
INSERT INTO table_name
(index_field
, other_field_1
, other_field_2
)
VALUES
(‘index_value’, ‘insert_value’, ‘other_value’)
ON DUPLICATE KEY UPDATEother_field_1
= ‘update_value’,other_field_2
= VALUES(other_field_2
);
See the example above where other_field_1 is set to insert_value on INSERT or to
update_value on UPDATE while other_field_2 is always set to other_value
INSERT with AUTO_INCREMENT +
LAST_INSERT_ID()
INSERT INTO t (this, that) VALUES (..., ...); SELECT LAST_INSERT_ID() INTO @id;
GROUP BY using HAVING
Using GROUP BY … HAVING to filter aggregate records is analogous to using SELECT … WHERE to filter individual
records
Group By using Group Concat
Group Concat is used in MySQL to get concatenated values of expressions with more than one result per column.
Meaning, there are many rows to be selected back for one column such as Name(1):Score(*)
ROUND
For exact numeric values (e.g. DECIMAL): If the first decimal place of a number is 5 or higher, this function will round a number to the next integer away from zero. If that decimal place is 4 or lower, this function will round to the next integer value closest to zero. SELECT ROUND(4.51) -> 5 SELECT ROUND(4.49) -> 4 SELECT ROUND(-4.51) -> -5
CEIL, CEILING
To round up a number use either the CEIL() or CEILING() function SELECT CEIL(1.23) -> 2 SELECT CEILING(4.83) -> 5
FLOOR
To round down a number, use the FLOOR() function SELECT FLOOR(1.99) -> 1
POW, POWER
To raise a number x to a power y, use either the POW() or POWER() functions SELECT POW(2,2); => 4 SELECT POW(4,2); => 16
SQRT
Use the SQRT() function. If the number is negative, NULL will be returned SELECT SQRT(16); -> 4 SELECT SQRT(-3); -> NULL
RAND
To generate a pseudorandom floating-point number between 0 and 1, use the RAND() function
ABS
Return the absolute value of a number SELECT ABS(2); -> 2 SELECT ABS(-46); -> 46
SIGN
The sign of a number compares it to 0 -1 n < 0 SELECT SIGN(42); -> 1 0 n = 0 SELECT SIGN(0); -> 0 1 n > 0 SELECT SIGN(-3); -> -1 SELECT SIGN(-423421); -> -1
ASCII()
Return numeric value of left-most character
BIN()
Return a string containing binary representation of a number