DavidSzczesniak
1/19/2018 - 5:32 PM

Column Types

\!h Most common ones:

\!h 1. INT[(width)] [UNSIGNED] [ZEROFILL]
-- Regular number range is -2,147,483,648 to 2,147,483,687.
-- 0 to 4,296,967,295 with the optional UNSIGNED.
-- The optional ZEROFILL keyword used to left-pad the values with zeroes up the the specified width. 

-- If you store a value higher than the width, the width is then ignored:
CREATE TABLE numbers (my_number INT(4) ZEROFILL);

INSERT INTO numbers VALUES(3), (33), (333), (3333), (33333), (333333);

SELECT * FROM numbers;
+-----------+
| my_number |
+-----------+
|      0003 |
|      0033 |
|      0333 |
|      3333 |
|     33333 |
|    333333 |
+-----------+
-- Numbers shorter than 4 digits are zero-padded to four.
-- Numbers longer than 4 are unaffected.
-- Using ZEROFILL automatically adds UNSIGNED.

\!h 2. DECIMAL[(width[,decimals])] [UNSIGNED] [ZEROFILL];
-- Stores a fixed-point number like a salary or distance, with a total of <width> digits of which some smaller number are decimals that follow a decimal point.

\!h 3. Date 
-- Stores and displays a date in the format YYYY-MM-DD, with a range of 1000-01-01 to 9999-12-31.
-- There is no difference between YYYY-MM-DD and YY-MM-DD. Using four digits is usually the better choice as there can be confusion between centuries.

\!h 4. Time
-- Stores a time in the format HHH:MM:SS for the range -838:59:59 to 838:59:59.
-- Must be input in order of days, hours, minutes and seconds:
-- DD HH:MM:SS, HH:MM:SS, DD HH:MM, HH:MM, DD HH, or SS.
-- DD = value of days in the range 0 to 34.
-- Maximum value for this format is 34 days, 22 hours, 59 minutes and 59 seconds. Any more than input at once generates an error (the value is still stored).
-- The zero time 00:00:00 is stored if an invalid value is given, or can be put in as a dummy value.

\!h 5. Timestamps 
-- Stores and displays date and time in the format YYYY-MM-DD HH:MM:SS for the range 1970-01-01 00:00:00 to sometime in 2037.
-- Automatically updating column:
-- DEFAULT CURRENT_TIMESTAMP   - set timestamp only when a new row is inserted into the table.
-- ON UPDATE CURRENT_TIMESTAMP - no default timestamp, but current time used whenever the data in a row is updated.
-- DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP - both of the above.

-- DEFAULT NULL for a TIMESTAMP column = DEFAULT CURRENT_TIMESTAMP for the first timestamp column, DEFAULT 0 for subsequent.

-- Example:
CREATE TABLE mytime(id INT NOT NULL, 
changetime TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP); -- create table with both of the timstamp options 

INSERT INTO mytime VALUES(1, ''), (2, '2006-7-16 1:2:3'), (3, NULL); -- sets time 3 different ways (first number is id)

SELECT * FROM mytime; 
+----+---------------------+
| id | changetime          |
+----+---------------------+
|  1 | 0000-00-00 00:00:00 | -- nothing entered, zero-time - will default to current timestamp only when the row is updated
|  2 | 2006-07-16 01:02:03 | -- specified date and time
|  3 | 2018-01-22 12:29:11 | -- current timestamp defaulted from NULL
+----+---------------------+

\!h 6. Most common string type - CHAR [(width)]
-- Stores a fixed-length string, like a name or an address, of length <width>. 
-- CHAR(1) is used when no width is specified, max width is 255.
-- CHAR(0) can be used as a dummy column for a placeholder. Can store NULL or the empty string ''.